diff --git a/quadratic-core/src/grid/column.rs b/quadratic-core/src/grid/column.rs index 15dd1261a3..4d26ddd7ae 100644 --- a/quadratic-core/src/grid/column.rs +++ b/quadratic-core/src/grid/column.rs @@ -254,6 +254,10 @@ impl ColumnData { self.0.values() } + pub fn into_blocks(self) -> impl Iterator> { + self.0.into_values() + } + pub fn has_blocks_in_range(&self, y_range: Range) -> bool { self.blocks_covering_range(y_range).next().is_some() } diff --git a/quadratic-core/src/grid/file/serialize/borders.rs b/quadratic-core/src/grid/file/serialize/borders.rs index b07337a48b..234582c08e 100644 --- a/quadratic-core/src/grid/file/serialize/borders.rs +++ b/quadratic-core/src/grid/file/serialize/borders.rs @@ -12,7 +12,7 @@ use crate::{ small_timestamp::SmallTimestamp, }; -fn export_rgba(color: &Rgba) -> current::RgbaSchema { +fn export_rgba(color: Rgba) -> current::RgbaSchema { current::RgbaSchema { red: color.red, green: color.green, @@ -21,7 +21,7 @@ fn export_rgba(color: &Rgba) -> current::RgbaSchema { } } -fn export_border_line(line: &CellBorderLine) -> current::CellBorderLineSchema { +fn export_border_line(line: CellBorderLine) -> current::CellBorderLineSchema { match line { CellBorderLine::Line1 => current::CellBorderLineSchema::Line1, CellBorderLine::Line2 => current::CellBorderLineSchema::Line2, @@ -33,36 +33,36 @@ fn export_border_line(line: &CellBorderLine) -> current::CellBorderLineSchema { } } -fn export_timestamp(timestamp: &SmallTimestamp) -> u32 { +fn export_timestamp(timestamp: SmallTimestamp) -> u32 { timestamp.value() } -fn export_border_style_cell(style: &BorderStyleCell) -> current::BorderStyleCellSchema { +fn export_border_style_cell(style: BorderStyleCell) -> current::BorderStyleCellSchema { current::BorderStyleCellSchema { - top: style.top.as_ref().map(export_border_style_timestamp), - bottom: style.bottom.as_ref().map(export_border_style_timestamp), - left: style.left.as_ref().map(export_border_style_timestamp), - right: style.right.as_ref().map(export_border_style_timestamp), + top: style.top.map(export_border_style_timestamp), + bottom: style.bottom.map(export_border_style_timestamp), + left: style.left.map(export_border_style_timestamp), + right: style.right.map(export_border_style_timestamp), } } fn export_border_style_timestamp( - style: &BorderStyleTimestamp, + style: BorderStyleTimestamp, ) -> current::BorderStyleTimestampSchema { current::BorderStyleTimestampSchema { - color: export_rgba(&style.color), - line: export_border_line(&style.line), - timestamp: export_timestamp(&style.timestamp), + color: export_rgba(style.color), + line: export_border_line(style.line), + timestamp: export_timestamp(style.timestamp), } } fn export_column_repeat( - data: &ColumnData>, + data: ColumnData>, ) -> HashMap> { data.blocks() .map(|block| { let start = block.start(); - let value = export_border_style_timestamp(&block.content.value); + let value = export_border_style_timestamp(block.content.value); let len = block.content.len as u32; (start, current::ColumnRepeatSchema { value, len }) }) @@ -70,35 +70,35 @@ fn export_column_repeat( } fn export_border_side( - data: &HashMap>>, + data: HashMap>>, ) -> HashMap>> { - data.iter() - .map(|(col, data)| (*col, export_column_repeat(data))) + data.into_iter() + .map(|(col, data)| (col, export_column_repeat(data))) .collect() } fn export_hash_map_border_style_cell( - data: &HashMap, + data: HashMap, ) -> HashMap { - data.iter() - .map(|(i, style)| (*i, export_border_style_cell(style))) + data.into_iter() + .map(|(i, style)| (i, export_border_style_cell(style))) .collect() } -pub fn export_borders(borders: &Borders) -> current::BordersSchema { +pub fn export_borders(borders: Borders) -> current::BordersSchema { current::BordersSchema { - all: export_border_style_cell(&borders.all), - columns: export_hash_map_border_style_cell(&borders.columns), - rows: export_hash_map_border_style_cell(&borders.rows), - - left: export_border_side(&borders.left), - right: export_border_side(&borders.right), - top: export_border_side(&borders.top), - bottom: export_border_side(&borders.bottom), + all: export_border_style_cell(borders.all), + columns: export_hash_map_border_style_cell(borders.columns), + rows: export_hash_map_border_style_cell(borders.rows), + + left: export_border_side(borders.left), + right: export_border_side(borders.right), + top: export_border_side(borders.top), + bottom: export_border_side(borders.bottom), } } -fn import_rgba(schema: ¤t::RgbaSchema) -> Rgba { +fn import_rgba(schema: current::RgbaSchema) -> Rgba { Rgba { red: schema.red, green: schema.green, @@ -107,7 +107,7 @@ fn import_rgba(schema: ¤t::RgbaSchema) -> Rgba { } } -fn import_border_line(schema: ¤t::CellBorderLineSchema) -> CellBorderLine { +fn import_border_line(schema: current::CellBorderLineSchema) -> CellBorderLine { match schema { current::CellBorderLineSchema::Line1 => CellBorderLine::Line1, current::CellBorderLineSchema::Line2 => CellBorderLine::Line2, @@ -123,31 +123,31 @@ fn import_timestamp(value: u32) -> SmallTimestamp { SmallTimestamp::new(value) } -fn import_border_style_cell(schema: ¤t::BorderStyleCellSchema) -> BorderStyleCell { +fn import_border_style_cell(schema: current::BorderStyleCellSchema) -> BorderStyleCell { BorderStyleCell { - top: schema.top.as_ref().map(import_border_style_timestamp), - bottom: schema.bottom.as_ref().map(import_border_style_timestamp), - left: schema.left.as_ref().map(import_border_style_timestamp), - right: schema.right.as_ref().map(import_border_style_timestamp), + top: schema.top.map(import_border_style_timestamp), + bottom: schema.bottom.map(import_border_style_timestamp), + left: schema.left.map(import_border_style_timestamp), + right: schema.right.map(import_border_style_timestamp), } } fn import_border_style_timestamp( - schema: ¤t::BorderStyleTimestampSchema, + schema: current::BorderStyleTimestampSchema, ) -> BorderStyleTimestamp { BorderStyleTimestamp { - color: import_rgba(&schema.color), - line: import_border_line(&schema.line), + color: import_rgba(schema.color), + line: import_border_line(schema.line), timestamp: import_timestamp(schema.timestamp), } } fn import_column_repeat( - schema: &HashMap>, + schema: HashMap>, ) -> ColumnData> { let mut data = ColumnData::new(); - schema.iter().for_each(|(&start, repeat_schema)| { - let value = import_border_style_timestamp(&repeat_schema.value); + schema.into_iter().for_each(|(start, repeat_schema)| { + let value = import_border_style_timestamp(repeat_schema.value); let len = repeat_schema.len as usize; data.insert_block(start, len, value); }); @@ -156,36 +156,36 @@ fn import_column_repeat( } fn import_border_side( - schema: &HashMap< + schema: HashMap< i64, HashMap>, >, ) -> HashMap>> { schema - .iter() - .map(|(&col, repeat_schema)| (col, import_column_repeat(repeat_schema))) + .into_iter() + .map(|(col, repeat_schema)| (col, import_column_repeat(repeat_schema))) .collect() } fn import_hash_map_border_style_cell( - schema: &HashMap, + schema: HashMap, ) -> HashMap { schema - .iter() - .map(|(i, schema)| (*i, import_border_style_cell(schema))) + .into_iter() + .map(|(i, schema)| (i, import_border_style_cell(schema))) .collect() } -pub fn import_borders(borders: ¤t::BordersSchema) -> Borders { +pub fn import_borders(borders: current::BordersSchema) -> Borders { Borders { - all: import_border_style_cell(&borders.all), - columns: import_hash_map_border_style_cell(&borders.columns), - rows: import_hash_map_border_style_cell(&borders.rows), - - left: import_border_side(&borders.left), - right: import_border_side(&borders.right), - top: import_border_side(&borders.top), - bottom: import_border_side(&borders.bottom), + all: import_border_style_cell(borders.all), + columns: import_hash_map_border_style_cell(borders.columns), + rows: import_hash_map_border_style_cell(borders.rows), + + left: import_border_side(borders.left), + right: import_border_side(borders.right), + top: import_border_side(borders.top), + bottom: import_border_side(borders.bottom), } } @@ -218,8 +218,8 @@ mod tests { let sheet = gc.sheet(sheet_id); let borders = sheet.borders.clone(); - let exported = export_borders(&sheet.borders); - let imported = import_borders(&exported); - assert_eq!(borders, imported); + let exported = export_borders(borders); + let imported = import_borders(exported); + assert_eq!(sheet.borders, imported); } } diff --git a/quadratic-core/src/grid/file/serialize/cell_value.rs b/quadratic-core/src/grid/file/serialize/cell_value.rs index 93e34f3b68..3064a70770 100644 --- a/quadratic-core/src/grid/file/serialize/cell_value.rs +++ b/quadratic-core/src/grid/file/serialize/cell_value.rs @@ -6,15 +6,15 @@ use crate::{ use bigdecimal::BigDecimal; use std::str::FromStr; -pub fn export_cell_value(cell_value: &CellValue) -> current::CellValueSchema { +pub fn export_cell_value(cell_value: CellValue) -> current::CellValueSchema { match cell_value { CellValue::Blank => current::CellValueSchema::Blank, - CellValue::Text(text) => current::CellValueSchema::Text(text.to_owned()), - CellValue::Number(number) => export_cell_value_number(number.clone()), - CellValue::Html(html) => current::CellValueSchema::Html(html.to_owned()), + CellValue::Text(text) => current::CellValueSchema::Text(text), + CellValue::Number(number) => export_cell_value_number(number), + CellValue::Html(html) => current::CellValueSchema::Html(html), CellValue::Code(cell_code) => current::CellValueSchema::Code(current::CodeCellSchema { - code: cell_code.code.to_owned(), - language: match cell_code.language.clone() { + code: cell_code.code, + language: match cell_code.language { CodeCellLanguage::Python => current::CodeCellLanguageSchema::Python, CodeCellLanguage::Formula => current::CodeCellLanguageSchema::Formula, CodeCellLanguage::Javascript => current::CodeCellLanguageSchema::Javascript, @@ -30,16 +30,16 @@ pub fn export_cell_value(cell_value: &CellValue) -> current::CellValueSchema { } }, }), - CellValue::Logical(logical) => current::CellValueSchema::Logical(*logical), + CellValue::Logical(logical) => current::CellValueSchema::Logical(logical), CellValue::Instant(instant) => current::CellValueSchema::Instant(instant.to_string()), CellValue::Duration(duration) => current::CellValueSchema::Duration(duration.to_string()), - CellValue::Date(d) => current::CellValueSchema::Date(*d), - CellValue::Time(t) => current::CellValueSchema::Time(*t), - CellValue::DateTime(dt) => current::CellValueSchema::DateTime(*dt), - CellValue::Error(error) => current::CellValueSchema::Error( - current::RunErrorSchema::from_grid_run_error(*error.clone()), - ), - CellValue::Image(image) => current::CellValueSchema::Image(image.clone()), + CellValue::Date(d) => current::CellValueSchema::Date(d), + CellValue::Time(t) => current::CellValueSchema::Time(t), + CellValue::DateTime(dt) => current::CellValueSchema::DateTime(dt), + CellValue::Error(error) => { + current::CellValueSchema::Error(current::RunErrorSchema::from_grid_run_error(*error)) + } + CellValue::Image(image) => current::CellValueSchema::Image(image), } } @@ -55,44 +55,42 @@ pub fn import_cell_value_number(number: String) -> CellValue { CellValue::Number(BigDecimal::from_str(&number).unwrap_or_default()) } -pub fn import_cell_value(value: ¤t::CellValueSchema) -> CellValue { +pub fn import_cell_value(value: current::CellValueSchema) -> CellValue { match value { current::CellValueSchema::Blank => CellValue::Blank, - current::CellValueSchema::Text(text) => CellValue::Text(text.to_owned()), - current::CellValueSchema::Number(number) => import_cell_value_number(number.to_owned()), - current::CellValueSchema::Html(html) => CellValue::Html(html.to_owned()), + current::CellValueSchema::Text(text) => CellValue::Text(text), + current::CellValueSchema::Number(number) => import_cell_value_number(number), + current::CellValueSchema::Html(html) => CellValue::Html(html), current::CellValueSchema::Code(code_cell) => CellValue::Code(CodeCellValue { - code: code_cell.code.to_owned(), + code: code_cell.code, language: match code_cell.language { current::CodeCellLanguageSchema::Python => CodeCellLanguage::Python, current::CodeCellLanguageSchema::Formula => CodeCellLanguage::Formula, current::CodeCellLanguageSchema::Javascript => CodeCellLanguage::Javascript, - current::CodeCellLanguageSchema::Connection { ref kind, ref id } => { + current::CodeCellLanguageSchema::Connection { kind, id } => { CodeCellLanguage::Connection { kind: match kind { current::ConnectionKindSchema::Postgres => ConnectionKind::Postgres, current::ConnectionKindSchema::Mysql => ConnectionKind::Mysql, current::ConnectionKindSchema::Mssql => ConnectionKind::Mssql, }, - id: id.clone(), + id, } } }, }), - current::CellValueSchema::Logical(logical) => CellValue::Logical(*logical), + current::CellValueSchema::Logical(logical) => CellValue::Logical(logical), current::CellValueSchema::Instant(instant) => { - CellValue::Instant(serde_json::from_str(instant).unwrap_or_default()) + CellValue::Instant(serde_json::from_str(&instant).unwrap_or_default()) } current::CellValueSchema::Duration(duration) => { - CellValue::Duration(serde_json::from_str(duration).unwrap_or_default()) - } - current::CellValueSchema::Date(date) => CellValue::Date(*date), - current::CellValueSchema::Time(time) => CellValue::Time(*time), - current::CellValueSchema::DateTime(dt) => CellValue::DateTime(*dt), - current::CellValueSchema::Error(error) => { - CellValue::Error(Box::new((*error).clone().into())) + CellValue::Duration(serde_json::from_str(&duration).unwrap_or_default()) } - current::CellValueSchema::Image(text) => CellValue::Image(text.to_owned()), + current::CellValueSchema::Date(date) => CellValue::Date(date), + current::CellValueSchema::Time(time) => CellValue::Time(time), + current::CellValueSchema::DateTime(dt) => CellValue::DateTime(dt), + current::CellValueSchema::Error(error) => CellValue::Error(Box::new(error.into())), + current::CellValueSchema::Image(text) => CellValue::Image(text), } } diff --git a/quadratic-core/src/grid/file/serialize/code_cell.rs b/quadratic-core/src/grid/file/serialize/code_cell.rs index 5dde9f47bf..583224c02f 100644 --- a/quadratic-core/src/grid/file/serialize/code_cell.rs +++ b/quadratic-core/src/grid/file/serialize/code_cell.rs @@ -1,9 +1,10 @@ use anyhow::Result; use chrono::Utc; use indexmap::IndexMap; +use itertools::Itertools; use crate::{ - grid::{CodeRun, CodeRunResult, Sheet}, + grid::{CodeRun, CodeRunResult}, Pos, Value, }; @@ -13,60 +14,61 @@ use super::{ }; pub(crate) fn import_code_cell_builder( - sheet: ¤t::SheetSchema, + code_runs: Vec<(current::PosSchema, current::CodeRunSchema)>, ) -> Result> { - let mut code_runs = IndexMap::new(); + let mut new_code_runs = IndexMap::new(); - sheet.code_runs.iter().for_each(|(pos, code_run)| { + code_runs.into_iter().for_each(|(pos, code_run)| { let cells_accessed = code_run .cells_accessed - .iter() - .map(|sheet_rect| crate::SheetRect::from(sheet_rect.clone())) + .into_iter() + .map(crate::SheetRect::from) .collect(); - let result = match &code_run.result { + let result = match code_run.result { current::CodeRunResultSchema::Ok(output) => CodeRunResult::Ok(match output { current::OutputValueSchema::Single(value) => { - Value::Single(import_cell_value(value)) + Value::Single(import_cell_value(value.to_owned())) } current::OutputValueSchema::Array(current::OutputArraySchema { size, values }) => { Value::Array(crate::Array::from( values + .into_iter() .chunks(size.w as usize) - .map(|row| row.iter().map(import_cell_value).collect::>()) + .into_iter() + .map(|row| row.into_iter().map(import_cell_value).collect::>()) .collect::>>(), )) } }), - current::CodeRunResultSchema::Err(error) => CodeRunResult::Err(error.clone().into()), + current::CodeRunResultSchema::Err(error) => CodeRunResult::Err(error.to_owned().into()), }; - code_runs.insert( + new_code_runs.insert( Pos { x: pos.x, y: pos.y }, CodeRun { - formatted_code_string: code_run.formatted_code_string.to_owned(), + formatted_code_string: code_run.formatted_code_string, last_modified: code_run.last_modified.unwrap_or(Utc::now()), // this is required but fall back to now if failed - std_out: code_run.std_out.to_owned(), - std_err: code_run.std_err.to_owned(), + std_out: code_run.std_out, + std_err: code_run.std_err, spill_error: code_run.spill_error, cells_accessed, result, - return_type: code_run.return_type.to_owned(), - line_number: code_run.line_number.to_owned(), - output_type: code_run.output_type.to_owned(), + return_type: code_run.return_type, + line_number: code_run.line_number, + output_type: code_run.output_type, }, ); }); - Ok(code_runs) + Ok(new_code_runs) } pub(crate) fn export_rows_code_runs( - sheet: &Sheet, + code_runs: IndexMap, ) -> Vec<(current::PosSchema, current::CodeRunSchema)> { - sheet - .code_runs - .iter() + code_runs + .into_iter() .map(|(pos, code_run)| { - let result = match &code_run.result { + let result = match code_run.result { CodeRunResult::Ok(output) => match output { Value::Single(cell_value) => current::CodeRunResultSchema::Ok( current::OutputValueSchema::Single(export_cell_value(cell_value)), @@ -79,7 +81,9 @@ pub(crate) fn export_rows_code_runs( }, values: array .rows() - .flat_map(|row| row.iter().map(export_cell_value)) + .flat_map(|row| { + row.iter().map(|value| export_cell_value(value.to_owned())) + }) .collect(), }), ), @@ -89,27 +93,27 @@ pub(crate) fn export_rows_code_runs( }), }, CodeRunResult::Err(error) => current::CodeRunResultSchema::Err( - current::RunErrorSchema::from_grid_run_error(error.to_owned()), + current::RunErrorSchema::from_grid_run_error(error), ), }; ( - current::PosSchema::from(*pos), + current::PosSchema::from(pos), current::CodeRunSchema { - formatted_code_string: code_run.formatted_code_string.clone(), + formatted_code_string: code_run.formatted_code_string, last_modified: Some(code_run.last_modified), - std_out: code_run.std_out.clone(), - std_err: code_run.std_err.clone(), + std_out: code_run.std_out, + std_err: code_run.std_err, spill_error: code_run.spill_error, cells_accessed: code_run .cells_accessed - .iter() - .map(|sheet_rect| current::SheetRectSchema::from(*sheet_rect)) + .into_iter() + .map(current::SheetRectSchema::from) .collect(), result, - return_type: code_run.return_type.clone(), + return_type: code_run.return_type, line_number: code_run.line_number, - output_type: code_run.output_type.clone(), + output_type: code_run.output_type, }, ) }) diff --git a/quadratic-core/src/grid/file/serialize/column.rs b/quadratic-core/src/grid/file/serialize/column.rs index e3650a8219..34483e416d 100644 --- a/quadratic-core/src/grid/file/serialize/column.rs +++ b/quadratic-core/src/grid/file/serialize/column.rs @@ -5,7 +5,7 @@ use anyhow::Result; use crate::{ grid::{ block::SameValue, CellAlign, CellVerticalAlign, CellWrap, Column, ColumnData, - NumericFormat, NumericFormatKind, RenderSize, Sheet, + NumericFormat, NumericFormatKind, RenderSize, }, CellValue, }; @@ -17,9 +17,9 @@ use super::{ pub(crate) fn set_column_format_align( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -37,9 +37,9 @@ pub(crate) fn set_column_format_align( pub(crate) fn set_column_format_vertical_align( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -57,9 +57,9 @@ pub(crate) fn set_column_format_vertical_align( pub(crate) fn set_column_format_wrap( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -77,11 +77,11 @@ pub(crate) fn set_column_format_wrap( pub(crate) fn set_column_format_numeric_format( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... - let y = (*y).parse::().unwrap(); + let y = (y).parse::().unwrap(); for y in y..(y + format.len as i64) { column_data.set( y, @@ -96,7 +96,7 @@ pub(crate) fn set_column_format_numeric_format( NumericFormatKind::Exponential } }, - symbol: format.value.symbol.to_owned(), + symbol: format.value.symbol.clone(), }), ); } @@ -105,9 +105,9 @@ pub(crate) fn set_column_format_numeric_format( pub(crate) fn set_column_format_i16( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -118,9 +118,9 @@ pub(crate) fn set_column_format_i16( pub(crate) fn set_column_format_string( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -131,9 +131,9 @@ pub(crate) fn set_column_format_string( pub(crate) fn set_column_format_bool( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -144,9 +144,9 @@ pub(crate) fn set_column_format_bool( pub(crate) fn set_column_format_render_size( column_data: &mut ColumnData>, - column: &HashMap>, + column: HashMap>, ) { - for (y, format) in column.iter() { + for (y, format) in column.into_iter() { // there's probably a better way to do this... let y = (*y).parse::().unwrap(); for y in y..(y + format.len as i64) { @@ -162,43 +162,43 @@ pub(crate) fn set_column_format_render_size( } pub(crate) fn import_column_builder( - columns: &[(i64, current::ColumnSchema)], + columns: Vec<(i64, current::ColumnSchema)>, ) -> Result> { columns - .iter() + .into_iter() .map(|(x, column)| { - let mut col = Column::new(*x); - set_column_format_align(&mut col.align, &column.align); - set_column_format_vertical_align(&mut col.vertical_align, &column.vertical_align); - set_column_format_wrap(&mut col.wrap, &column.wrap); - set_column_format_i16(&mut col.numeric_decimals, &column.numeric_decimals); - set_column_format_numeric_format(&mut col.numeric_format, &column.numeric_format); - set_column_format_bool(&mut col.numeric_commas, &column.numeric_commas); - set_column_format_bool(&mut col.bold, &column.bold); - set_column_format_bool(&mut col.italic, &column.italic); - set_column_format_string(&mut col.text_color, &column.text_color); - set_column_format_string(&mut col.fill_color, &column.fill_color); - set_column_format_render_size(&mut col.render_size, &column.render_size); - set_column_format_string(&mut col.date_time, &column.date_time); + let mut col = Column::new(x); + set_column_format_align(&mut col.align, column.align); + set_column_format_vertical_align(&mut col.vertical_align, column.vertical_align); + set_column_format_wrap(&mut col.wrap, column.wrap); + set_column_format_i16(&mut col.numeric_decimals, column.numeric_decimals); + set_column_format_numeric_format(&mut col.numeric_format, column.numeric_format); + set_column_format_bool(&mut col.numeric_commas, column.numeric_commas); + set_column_format_bool(&mut col.bold, column.bold); + set_column_format_bool(&mut col.italic, column.italic); + set_column_format_string(&mut col.text_color, column.text_color); + set_column_format_string(&mut col.fill_color, column.fill_color); + set_column_format_render_size(&mut col.render_size, column.render_size); + set_column_format_string(&mut col.date_time, column.date_time); // todo: there's probably a better way of doing this - for (y, value) in column.values.iter() { + for (y, value) in column.values.into_iter() { let cell_value = import_cell_value(value); if let Ok(y) = y.parse::() { col.values.insert(y, cell_value); } } - Ok((*x, col)) + Ok((x, col)) }) .collect::>>() } pub(crate) fn export_column_data_bool( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -212,10 +212,10 @@ pub(crate) fn export_column_data_bool( } pub(crate) fn export_column_data_string( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -229,10 +229,10 @@ pub(crate) fn export_column_data_string( } pub(crate) fn export_column_data_i16( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -246,10 +246,10 @@ pub(crate) fn export_column_data_i16( } pub(crate) fn export_column_data_numeric_format( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -277,10 +277,10 @@ pub(crate) fn export_column_data_numeric_format( } pub(crate) fn export_column_data_render_size( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -297,10 +297,10 @@ pub(crate) fn export_column_data_render_size( } pub(crate) fn export_column_data_align( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -318,10 +318,10 @@ pub(crate) fn export_column_data_align( } pub(crate) fn export_column_data_vertical_align( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -339,10 +339,10 @@ pub(crate) fn export_column_data_vertical_align( } pub(crate) fn export_column_data_wrap( - column_data: &ColumnData>, + column_data: ColumnData>, ) -> HashMap> { column_data - .blocks() + .into_blocks() .map(|block| { ( block.y.to_string(), @@ -360,35 +360,36 @@ pub(crate) fn export_column_data_wrap( } pub(crate) fn export_values( - values: &BTreeMap, + values: BTreeMap, ) -> HashMap { values - .iter() + .into_iter() .map(|(y, value)| (y.to_string(), export_cell_value(value))) .collect() } -pub(crate) fn export_column_builder(sheet: &Sheet) -> Vec<(i64, current::ColumnSchema)> { - sheet - .columns - .iter() +pub(crate) fn export_column_builder( + columns: BTreeMap, +) -> Vec<(i64, current::ColumnSchema)> { + columns + .into_iter() .map(|(x, column)| { ( - *x, + x, current::ColumnSchema { - align: export_column_data_align(&column.align), - vertical_align: export_column_data_vertical_align(&column.vertical_align), - wrap: export_column_data_wrap(&column.wrap), - numeric_decimals: export_column_data_i16(&column.numeric_decimals), - numeric_format: export_column_data_numeric_format(&column.numeric_format), - numeric_commas: export_column_data_bool(&column.numeric_commas), - bold: export_column_data_bool(&column.bold), - italic: export_column_data_bool(&column.italic), - text_color: export_column_data_string(&column.text_color), - fill_color: export_column_data_string(&column.fill_color), - render_size: export_column_data_render_size(&column.render_size), - date_time: export_column_data_string(&column.date_time), - values: export_values(&column.values), + align: export_column_data_align(column.align), + vertical_align: export_column_data_vertical_align(column.vertical_align), + wrap: export_column_data_wrap(column.wrap), + numeric_decimals: export_column_data_i16(column.numeric_decimals), + numeric_format: export_column_data_numeric_format(column.numeric_format), + numeric_commas: export_column_data_bool(column.numeric_commas), + bold: export_column_data_bool(column.bold), + italic: export_column_data_bool(column.italic), + text_color: export_column_data_string(column.text_color), + fill_color: export_column_data_string(column.fill_color), + render_size: export_column_data_render_size(column.render_size), + date_time: export_column_data_string(column.date_time), + values: export_values(column.values), }, ) }) diff --git a/quadratic-core/src/grid/file/serialize/format.rs b/quadratic-core/src/grid/file/serialize/format.rs index f95028b459..d77cfc2ac1 100644 --- a/quadratic-core/src/grid/file/serialize/format.rs +++ b/quadratic-core/src/grid/file/serialize/format.rs @@ -4,72 +4,68 @@ use std::collections::BTreeMap; use crate::grid::{ formats::format::Format, resize::{Resize, ResizeMap}, - CellAlign, CellVerticalAlign, CellWrap, NumericFormat, NumericFormatKind, RenderSize, Sheet, + CellAlign, CellVerticalAlign, CellWrap, NumericFormat, NumericFormatKind, RenderSize, }; use super::current; -pub(crate) fn import_format(format: ¤t::FormatSchema) -> Format { +pub(crate) fn import_format(format: current::FormatSchema) -> Format { Format { - align: format.align.as_ref().map(|align| match align { + align: format.align.map(|align| match align { current::CellAlignSchema::Left => CellAlign::Left, current::CellAlignSchema::Center => CellAlign::Center, current::CellAlignSchema::Right => CellAlign::Right, }), vertical_align: format .vertical_align - .as_ref() .map(|vertical_align| match vertical_align { current::CellVerticalAlignSchema::Top => CellVerticalAlign::Top, current::CellVerticalAlignSchema::Middle => CellVerticalAlign::Middle, current::CellVerticalAlignSchema::Bottom => CellVerticalAlign::Bottom, }), - wrap: format.wrap.as_ref().map(|wrap| match wrap { + wrap: format.wrap.map(|wrap| match wrap { current::CellWrapSchema::Wrap => CellWrap::Wrap, current::CellWrapSchema::Overflow => CellWrap::Overflow, current::CellWrapSchema::Clip => CellWrap::Clip, }), - numeric_format: format - .numeric_format - .as_ref() - .map(|numeric_format| NumericFormat { - kind: match numeric_format.kind { - current::NumericFormatKindSchema::Number => NumericFormatKind::Number, - current::NumericFormatKindSchema::Currency => NumericFormatKind::Currency, - current::NumericFormatKindSchema::Percentage => NumericFormatKind::Percentage, - current::NumericFormatKindSchema::Exponential => NumericFormatKind::Exponential, - }, - symbol: numeric_format.symbol.to_owned(), - }), + numeric_format: format.numeric_format.map(|numeric_format| NumericFormat { + kind: match numeric_format.kind { + current::NumericFormatKindSchema::Number => NumericFormatKind::Number, + current::NumericFormatKindSchema::Currency => NumericFormatKind::Currency, + current::NumericFormatKindSchema::Percentage => NumericFormatKind::Percentage, + current::NumericFormatKindSchema::Exponential => NumericFormatKind::Exponential, + }, + symbol: numeric_format.symbol, + }), numeric_decimals: format.numeric_decimals, numeric_commas: format.numeric_commas, bold: format.bold, italic: format.italic, - text_color: format.text_color.to_owned(), - fill_color: format.fill_color.to_owned(), - render_size: format.render_size.as_ref().map(|render_size| RenderSize { - w: render_size.w.to_owned(), - h: render_size.h.to_owned(), + text_color: format.text_color, + fill_color: format.fill_color, + render_size: format.render_size.map(|render_size| RenderSize { + w: render_size.w, + h: render_size.h, }), - date_time: format.date_time.to_owned(), + date_time: format.date_time, } } pub(crate) fn import_formats( - format: &[(i64, (current::FormatSchema, i64))], + format: Vec<(i64, (current::FormatSchema, i64))>, ) -> BTreeMap { format - .iter() - .map(|(i, (format, timestamp))| (*i, (import_format(format), *timestamp))) + .into_iter() + .map(|(i, (format, timestamp))| (i, (import_format(format), timestamp))) .collect() } -pub(crate) fn import_rows_size(row_sizes: &[(i64, current::ResizeSchema)]) -> Result { +pub(crate) fn import_rows_size(row_sizes: Vec<(i64, current::ResizeSchema)>) -> Result { row_sizes - .iter() + .into_iter() .try_fold(ResizeMap::default(), |mut sizes, (y, size)| { sizes.set_resize( - *y, + y, match size { current::ResizeSchema::Auto => Resize::Auto, current::ResizeSchema::Manual => Resize::Manual, @@ -79,7 +75,7 @@ pub(crate) fn import_rows_size(row_sizes: &[(i64, current::ResizeSchema)]) -> Re }) } -pub(crate) fn export_format(format: &Format) -> Option { +pub(crate) fn export_format(format: Format) -> Option { if format.is_default() { None } else { @@ -101,7 +97,7 @@ pub(crate) fn export_format(format: &Format) -> Option { CellWrap::Overflow => current::CellWrapSchema::Overflow, CellWrap::Clip => current::CellWrapSchema::Clip, }), - numeric_format: format.numeric_format.as_ref().map(|numeric_format| { + numeric_format: format.numeric_format.map(|numeric_format| { current::NumericFormatSchema { kind: match numeric_format.kind { NumericFormatKind::Number => current::NumericFormatKindSchema::Number, @@ -113,42 +109,41 @@ pub(crate) fn export_format(format: &Format) -> Option { current::NumericFormatKindSchema::Exponential } }, - symbol: numeric_format.symbol.to_owned(), + symbol: numeric_format.symbol, } }), numeric_decimals: format.numeric_decimals, numeric_commas: format.numeric_commas, bold: format.bold, italic: format.italic, - text_color: format.text_color.to_owned(), - fill_color: format.fill_color.to_owned(), + text_color: format.text_color, + fill_color: format.fill_color, render_size: format .render_size - .as_ref() .map(|render_size| current::RenderSizeSchema { - w: render_size.w.to_owned(), - h: render_size.h.to_owned(), + w: render_size.w, + h: render_size.h, }), - date_time: format.date_time.to_owned(), + date_time: format.date_time, }) } } pub(crate) fn export_formats( - formats: &BTreeMap, + formats: BTreeMap, ) -> Vec<(i64, (current::FormatSchema, i64))> { formats - .iter() + .into_iter() .filter_map(|(y, (format, timestamp))| { let f = export_format(format)?; - Some((*y, (f, timestamp.to_owned()))) + Some((y, (f, timestamp))) }) .collect() } -pub(crate) fn export_rows_size(sheet: &Sheet) -> Vec<(i64, current::ResizeSchema)> { - sheet - .iter_row_resize() +pub(crate) fn export_rows_size(rows_resize: ResizeMap) -> Vec<(i64, current::ResizeSchema)> { + rows_resize + .into_iter_resize() .map(|(y, resize)| { ( y, diff --git a/quadratic-core/src/grid/file/serialize/selection.rs b/quadratic-core/src/grid/file/serialize/selection.rs index a0ddb2d4f2..8a2a54c94d 100644 --- a/quadratic-core/src/grid/file/serialize/selection.rs +++ b/quadratic-core/src/grid/file/serialize/selection.rs @@ -1,42 +1,43 @@ use super::current; use crate::selection::Selection; -pub fn import_selection(selection: ¤t::SelectionSchema) -> Selection { +pub fn import_selection(selection: current::SelectionSchema) -> Selection { Selection { sheet_id: selection.sheet_id.to_owned(), x: selection.x, y: selection.y, rects: selection .rects - .as_ref() .map(|rects| rects.iter().map(|r| r.into()).collect()), - rows: selection.rows.clone(), - columns: selection.columns.clone(), + rows: selection.rows, + columns: selection.columns, all: selection.all, } } -pub fn export_selection(selection: &Selection) -> current::SelectionSchema { +pub fn export_selection(selection: Selection) -> current::SelectionSchema { current::SelectionSchema { sheet_id: selection.sheet_id, x: selection.x, y: selection.y, rects: selection .rects - .as_ref() .map(|rects| rects.iter().map(|r| r.into()).collect()), - rows: selection.rows.clone(), - columns: selection.columns.clone(), + rows: selection.rows, + columns: selection.columns, all: selection.all, } } #[cfg(test)] mod tests { + use serial_test::parallel; + use super::*; use crate::{grid::SheetId, Rect}; #[test] + #[parallel] fn import_export_selection() { let selection = Selection { sheet_id: SheetId::test(), @@ -47,7 +48,7 @@ mod tests { columns: Some(vec![4, 5, 6]), all: true, }; - let imported = import_selection(&export_selection(&selection)); + let imported = import_selection(export_selection(selection.clone())); assert_eq!(selection, imported); } } diff --git a/quadratic-core/src/grid/file/serialize/sheets.rs b/quadratic-core/src/grid/file/serialize/sheets.rs index 3be4a600f0..276da152a2 100644 --- a/quadratic-core/src/grid/file/serialize/sheets.rs +++ b/quadratic-core/src/grid/file/serialize/sheets.rs @@ -22,24 +22,24 @@ use super::{ pub fn import_sheet(sheet: current::SheetSchema) -> Result { let mut new_sheet = Sheet { id: SheetId::from_str(&sheet.id.id)?, - name: sheet.name.to_owned(), - color: sheet.color.to_owned(), - order: sheet.order.to_owned(), - offsets: SheetOffsets::import(&sheet.offsets), - columns: import_column_builder(&sheet.columns)?, + name: sheet.name, + color: sheet.color, + order: sheet.order, + offsets: SheetOffsets::import(sheet.offsets), + columns: import_column_builder(sheet.columns)?, - code_runs: import_code_cell_builder(&sheet)?, + code_runs: import_code_cell_builder(sheet.code_runs)?, data_bounds: GridBounds::Empty, format_bounds: GridBounds::Empty, - format_all: sheet.formats_all.as_ref().map(import_format), - formats_columns: import_formats(&sheet.formats_columns), - formats_rows: import_formats(&sheet.formats_rows), + format_all: sheet.formats_all.map(import_format), + formats_columns: import_formats(sheet.formats_columns), + formats_rows: import_formats(sheet.formats_rows), - validations: import_validations(&sheet.validations), - rows_resize: import_rows_size(&sheet.rows_resize)?, + validations: import_validations(sheet.validations), + rows_resize: import_rows_size(sheet.rows_resize)?, - borders: import_borders(&sheet.borders), + borders: import_borders(sheet.borders), }; new_sheet.recalculate_bounds(); Ok(new_sheet) @@ -50,17 +50,17 @@ pub(crate) fn export_sheet(sheet: Sheet) -> current::SheetSchema { id: current::IdSchema { id: sheet.id.to_string(), }, - name: sheet.name.to_owned(), - color: sheet.color.to_owned(), - order: sheet.order.to_owned(), + name: sheet.name, + color: sheet.color, + order: sheet.order, offsets: sheet.offsets.export(), - formats_all: sheet.format_all.as_ref().and_then(export_format), - formats_columns: export_formats(&sheet.formats_columns), - formats_rows: export_formats(&sheet.formats_rows), - validations: export_validations(&sheet.validations), - rows_resize: export_rows_size(&sheet), - borders: export_borders(&sheet.borders), - code_runs: export_rows_code_runs(&sheet), - columns: export_column_builder(&sheet), + formats_all: sheet.format_all.and_then(export_format), + formats_columns: export_formats(sheet.formats_columns), + formats_rows: export_formats(sheet.formats_rows), + validations: export_validations(sheet.validations), + rows_resize: export_rows_size(sheet.rows_resize), + borders: export_borders(sheet.borders), + code_runs: export_rows_code_runs(sheet.code_runs), + columns: export_column_builder(sheet.columns), } } diff --git a/quadratic-core/src/grid/file/serialize/validations.rs b/quadratic-core/src/grid/file/serialize/validations.rs index f6f2a8cb3c..4aa69dc243 100644 --- a/quadratic-core/src/grid/file/serialize/validations.rs +++ b/quadratic-core/src/grid/file/serialize/validations.rs @@ -20,20 +20,20 @@ use crate::Pos; use super::selection::{export_selection, import_selection}; -pub fn import_validations(validations: ¤t::ValidationsSchema) -> Validations { +pub fn import_validations(validations: current::ValidationsSchema) -> Validations { Validations { validations: validations .validations - .iter() + .into_iter() .map(|validation| { Validation { - id: validation.id.to_owned(), - selection: import_selection(&validation.selection), - rule: import_validation_rule(&validation.rule), + id: validation.id, + selection: import_selection(validation.selection), + rule: import_validation_rule(validation.rule), message: crate::grid::sheet::validations::validation::ValidationMessage { show: validation.message.show, - title: validation.message.title.to_owned(), - message: validation.message.message.to_owned(), + title: validation.message.title, + message: validation.message.message, }, error: crate::grid::sheet::validations::validation::ValidationError { show: validation.error.show, @@ -48,31 +48,29 @@ pub fn import_validations(validations: ¤t::ValidationsSchema) -> Validatio crate::grid::sheet::validations::validation::ValidationStyle::Information } }, - title: validation.error.title.to_owned(), - message: validation.error.message.to_owned(), + title: validation.error.title, + message: validation.error.message, }, } }) .collect(), warnings: validations .warnings - .iter() - .map(|(pos, id)| (Pos { x: pos.x, y: pos.y }, *id)) + .into_iter() + .map(|(pos, id)| (Pos { x: pos.x, y: pos.y }, id)) .collect(), } } -fn import_validation_rule(rule: ¤t::ValidationRuleSchema) -> ValidationRule { +fn import_validation_rule(rule: current::ValidationRuleSchema) -> ValidationRule { match rule { current::ValidationRuleSchema::None => ValidationRule::None, current::ValidationRuleSchema::List(list) => ValidationRule::List(ValidationList { - source: match &list.source { + source: match list.source { current::ValidationListSourceSchema::Selection(selection) => { ValidationListSource::Selection(import_selection(selection)) } - current::ValidationListSourceSchema::List(list) => { - ValidationListSource::List(list.iter().map(|s| s.to_owned()).collect()) - } + current::ValidationListSourceSchema::List(list) => ValidationListSource::List(list), }, ignore_blank: list.ignore_blank, drop_down: list.drop_down, @@ -87,42 +85,29 @@ fn import_validation_rule(rule: ¤t::ValidationRuleSchema) -> ValidationRul ignore_blank: text.ignore_blank, text_match: text .text_match - .iter() + .into_iter() .map(|m| match m { current::TextMatchSchema::Exactly( current::TextCaseSchema::CaseInsensitive(cases), - ) => TextMatch::Exactly(TextCase::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), + ) => TextMatch::Exactly(TextCase::CaseInsensitive(cases)), current::TextMatchSchema::Exactly(current::TextCaseSchema::CaseSensitive( cases, - )) => TextMatch::Exactly(TextCase::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), + )) => TextMatch::Exactly(TextCase::CaseSensitive(cases)), current::TextMatchSchema::Contains( current::TextCaseSchema::CaseInsensitive(cases), - ) => TextMatch::Contains(TextCase::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), + ) => TextMatch::Contains(TextCase::CaseInsensitive(cases)), current::TextMatchSchema::Contains(current::TextCaseSchema::CaseSensitive( cases, - )) => TextMatch::Contains(TextCase::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), + )) => TextMatch::Contains(TextCase::CaseSensitive(cases)), current::TextMatchSchema::NotContains( current::TextCaseSchema::CaseInsensitive(cases), - ) => TextMatch::NotContains(TextCase::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), + ) => TextMatch::NotContains(TextCase::CaseInsensitive(cases)), current::TextMatchSchema::NotContains( current::TextCaseSchema::CaseSensitive(cases), - ) => TextMatch::NotContains(TextCase::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - )), - current::TextMatchSchema::TextLength { min, max } => TextMatch::TextLength { - min: min.to_owned(), - max: max.to_owned(), - }, + ) => TextMatch::NotContains(TextCase::CaseSensitive(cases)), + current::TextMatchSchema::TextLength { min, max } => { + TextMatch::TextLength { min, max } + } }) .collect(), }), @@ -130,17 +115,11 @@ fn import_validation_rule(rule: ¤t::ValidationRuleSchema) -> ValidationRul ignore_blank: number.ignore_blank, ranges: number .ranges - .iter() + .into_iter() .map(|range| match range { - current::NumberRangeSchema::Range(min, max) => { - NumberRange::Range(min.to_owned(), max.to_owned()) - } - current::NumberRangeSchema::Equal(entry) => { - NumberRange::Equal(entry.to_owned()) - } - current::NumberRangeSchema::NotEqual(entry) => { - NumberRange::NotEqual(entry.to_owned()) - } + current::NumberRangeSchema::Range(min, max) => NumberRange::Range(min, max), + current::NumberRangeSchema::Equal(entry) => NumberRange::Equal(entry), + current::NumberRangeSchema::NotEqual(entry) => NumberRange::NotEqual(entry), }) .collect(), }), @@ -153,25 +132,25 @@ fn import_validation_rule(rule: ¤t::ValidationRuleSchema) -> ValidationRul prohibit_time: dt.prohibit_time, ranges: dt .ranges - .iter() + .into_iter() .map(|range| match range { current::DateTimeRangeSchema::DateRange(min, max) => { - DateTimeRange::DateRange(min.to_owned(), max.to_owned()) + DateTimeRange::DateRange(min, max) } current::DateTimeRangeSchema::DateEqual(entry) => { - DateTimeRange::DateEqual(entry.to_owned()) + DateTimeRange::DateEqual(entry) } current::DateTimeRangeSchema::DateNotEqual(entry) => { - DateTimeRange::DateNotEqual(entry.to_owned()) + DateTimeRange::DateNotEqual(entry) } current::DateTimeRangeSchema::TimeRange(min, max) => { - DateTimeRange::TimeRange(min.to_owned(), max.to_owned()) + DateTimeRange::TimeRange(min, max) } current::DateTimeRangeSchema::TimeEqual(entry) => { - DateTimeRange::TimeEqual(entry.to_owned()) + DateTimeRange::TimeEqual(entry) } current::DateTimeRangeSchema::TimeNotEqual(entry) => { - DateTimeRange::TimeNotEqual(entry.to_owned()) + DateTimeRange::TimeNotEqual(entry) } }) .collect(), @@ -180,18 +159,18 @@ fn import_validation_rule(rule: ¤t::ValidationRuleSchema) -> ValidationRul } } -fn export_validation_rule(rule: &ValidationRule) -> current::ValidationRuleSchema { +fn export_validation_rule(rule: ValidationRule) -> current::ValidationRuleSchema { match rule { ValidationRule::None => current::ValidationRuleSchema::None, ValidationRule::List(list) => { current::ValidationRuleSchema::List(current::ValidationListSchema { - source: match &list.source { + source: match list.source { ValidationListSource::Selection(selection) => { current::ValidationListSourceSchema::Selection(export_selection(selection)) } - ValidationListSource::List(list) => current::ValidationListSourceSchema::List( - list.iter().map(|s| s.to_owned()).collect(), - ), + ValidationListSource::List(list) => { + current::ValidationListSourceSchema::List(list) + } }, ignore_blank: list.ignore_blank, drop_down: list.drop_down, @@ -208,55 +187,40 @@ fn export_validation_rule(rule: &ValidationRule) -> current::ValidationRuleSchem ignore_blank: text.ignore_blank, text_match: text .text_match - .iter() + .into_iter() .map(|m| match m { TextMatch::Exactly(TextCase::CaseInsensitive(cases)) => { current::TextMatchSchema::Exactly( - current::TextCaseSchema::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseInsensitive(cases), ) } TextMatch::Exactly(TextCase::CaseSensitive(cases)) => { current::TextMatchSchema::Exactly( - current::TextCaseSchema::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseSensitive(cases), ) } TextMatch::Contains(TextCase::CaseInsensitive(cases)) => { current::TextMatchSchema::Contains( - current::TextCaseSchema::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseInsensitive(cases), ) } TextMatch::Contains(TextCase::CaseSensitive(cases)) => { current::TextMatchSchema::Contains( - current::TextCaseSchema::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseSensitive(cases), ) } TextMatch::NotContains(TextCase::CaseInsensitive(cases)) => { current::TextMatchSchema::NotContains( - current::TextCaseSchema::CaseInsensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseInsensitive(cases), ) } TextMatch::NotContains(TextCase::CaseSensitive(cases)) => { current::TextMatchSchema::NotContains( - current::TextCaseSchema::CaseSensitive( - cases.iter().map(|case| case.to_owned()).collect(), - ), + current::TextCaseSchema::CaseSensitive(cases), ) } TextMatch::TextLength { min, max } => { - current::TextMatchSchema::TextLength { - min: min.to_owned(), - max: max.to_owned(), - } + current::TextMatchSchema::TextLength { min, max } } }) .collect(), @@ -267,17 +231,11 @@ fn export_validation_rule(rule: &ValidationRule) -> current::ValidationRuleSchem ignore_blank: number.ignore_blank, ranges: number .ranges - .iter() + .into_iter() .map(|range| match range { - NumberRange::Range(min, max) => { - current::NumberRangeSchema::Range(*min, *max) - } - NumberRange::Equal(entry) => { - current::NumberRangeSchema::Equal(entry.clone()) - } - NumberRange::NotEqual(entry) => { - current::NumberRangeSchema::NotEqual(entry.clone()) - } + NumberRange::Range(min, max) => current::NumberRangeSchema::Range(min, max), + NumberRange::Equal(entry) => current::NumberRangeSchema::Equal(entry), + NumberRange::NotEqual(entry) => current::NumberRangeSchema::NotEqual(entry), }) .collect(), }) @@ -291,25 +249,25 @@ fn export_validation_rule(rule: &ValidationRule) -> current::ValidationRuleSchem prohibit_time: dt.prohibit_time, ranges: dt .ranges - .iter() + .into_iter() .map(|range| match range { DateTimeRange::DateRange(min, max) => { - current::DateTimeRangeSchema::DateRange(*min, *max) + current::DateTimeRangeSchema::DateRange(min, max) } DateTimeRange::DateEqual(entry) => { - current::DateTimeRangeSchema::DateEqual(entry.clone()) + current::DateTimeRangeSchema::DateEqual(entry) } DateTimeRange::DateNotEqual(entry) => { - current::DateTimeRangeSchema::DateNotEqual(entry.clone()) + current::DateTimeRangeSchema::DateNotEqual(entry) } DateTimeRange::TimeRange(min, max) => { - current::DateTimeRangeSchema::TimeRange(*min, *max) + current::DateTimeRangeSchema::TimeRange(min, max) } DateTimeRange::TimeEqual(entry) => { - current::DateTimeRangeSchema::TimeEqual(entry.clone()) + current::DateTimeRangeSchema::TimeEqual(entry) } DateTimeRange::TimeNotEqual(entry) => { - current::DateTimeRangeSchema::TimeNotEqual(entry.clone()) + current::DateTimeRangeSchema::TimeNotEqual(entry) } }) .collect(), @@ -318,19 +276,19 @@ fn export_validation_rule(rule: &ValidationRule) -> current::ValidationRuleSchem } } -pub fn export_validations(validations: &Validations) -> current::ValidationsSchema { +pub fn export_validations(validations: Validations) -> current::ValidationsSchema { current::ValidationsSchema { validations: validations .validations - .iter() + .into_iter() .map(|validation| current::ValidationSchema { - selection: export_selection(&validation.selection), - id: validation.id.to_owned(), - rule: export_validation_rule(&validation.rule), + selection: export_selection(validation.selection), + id: validation.id, + rule: export_validation_rule(validation.rule), message: current::ValidationMessageSchema { show: validation.message.show, - title: validation.message.title.to_owned(), - message: validation.message.message.to_owned(), + title: validation.message.title, + message: validation.message.message, }, error: current::ValidationErrorSchema { show: validation.error.show, @@ -339,15 +297,15 @@ pub fn export_validations(validations: &Validations) -> current::ValidationsSche ValidationStyle::Stop => current::ValidationStyleSchema::Stop, ValidationStyle::Information => current::ValidationStyleSchema::Information, }, - title: validation.error.title.to_owned(), - message: validation.error.message.to_owned(), + title: validation.error.title, + message: validation.error.message, }, }) .collect(), warnings: validations .warnings - .iter() - .map(|(pos, id)| (current::PosSchema { x: pos.x, y: pos.y }, *id)) + .into_iter() + .map(|(pos, id)| (current::PosSchema { x: pos.x, y: pos.y }, id)) .collect(), } } @@ -356,6 +314,7 @@ pub fn export_validations(validations: &Validations) -> current::ValidationsSche mod tests { use std::collections::HashMap; + use serial_test::parallel; use uuid::Uuid; use crate::{grid::SheetId, selection::Selection, Rect}; @@ -363,6 +322,7 @@ mod tests { use super::*; #[test] + #[parallel] fn import_export_validations() { let validation_id = Uuid::new_v4(); let mut warnings = HashMap::new(); @@ -407,11 +367,12 @@ mod tests { }], warnings, }; - let imported = import_validations(&export_validations(&validations)); + let imported = import_validations(export_validations(validations.clone())); assert_eq!(validations, imported); } #[test] + #[parallel] fn import_export_validation_numbers() { let validation_id = Uuid::new_v4(); let mut warnings = HashMap::new(); @@ -450,11 +411,12 @@ mod tests { }], warnings, }; - let imported = import_validations(&export_validations(&validations)); + let imported = import_validations(export_validations(validations.clone())); assert_eq!(validations, imported); } #[test] + #[parallel] fn import_export_validation_text() { let validation_id = Uuid::new_v4(); let mut warnings = HashMap::new(); @@ -507,11 +469,12 @@ mod tests { }], warnings, }; - let imported = import_validations(&export_validations(&validations)); + let imported = import_validations(export_validations(validations.clone())); assert_eq!(validations, imported); } #[test] + #[parallel] fn import_export_validation_date_time() { let validation_id = Uuid::new_v4(); let mut warnings = HashMap::new(); @@ -556,7 +519,7 @@ mod tests { }], warnings, }; - let imported = import_validations(&export_validations(&validations)); + let imported = import_validations(export_validations(validations.clone())); assert_eq!(validations, imported); } } diff --git a/quadratic-core/src/grid/file/serialize/values.rs b/quadratic-core/src/grid/file/serialize/values.rs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/quadratic-core/src/grid/file/sheet_schema.rs b/quadratic-core/src/grid/file/sheet_schema.rs index fa08fcd02c..2f15284e5d 100644 --- a/quadratic-core/src/grid/file/sheet_schema.rs +++ b/quadratic-core/src/grid/file/sheet_schema.rs @@ -18,7 +18,7 @@ impl SheetSchema { match self { SheetSchema::V1_7(sheet) => super::serialize::sheets::import_sheet(sheet), SheetSchema::V1_6(sheet) => { - super::serialize::sheets::import_sheet(v1_6::file::upgrade_sheet(&sheet)?) + super::serialize::sheets::import_sheet(v1_6::file::upgrade_sheet(sheet)?) } } } diff --git a/quadratic-core/src/grid/file/v1_4/file.rs b/quadratic-core/src/grid/file/v1_4/file.rs index 91c7761c54..19923169e8 100644 --- a/quadratic-core/src/grid/file/v1_4/file.rs +++ b/quadratic-core/src/grid/file/v1_4/file.rs @@ -2,9 +2,7 @@ use std::collections::HashMap; use std::str::FromStr; use crate::grid::file::v1_4::schema as v1_4; -use crate::grid::file::v1_5::schema::{ - self as v1_5, CellAlign, CodeCellLanguage, ColumnRepeat, -}; +use crate::grid::file::v1_5::schema::{self as v1_5, CellAlign, CodeCellLanguage, ColumnRepeat}; use anyhow::Result; use chrono::DateTime; diff --git a/quadratic-core/src/grid/file/v1_5/file.rs b/quadratic-core/src/grid/file/v1_5/file.rs index 19788e4d8b..57df544be4 100644 --- a/quadratic-core/src/grid/file/v1_5/file.rs +++ b/quadratic-core/src/grid/file/v1_5/file.rs @@ -4,26 +4,24 @@ use crate::grid::file::v1_5::schema as v1_5; use crate::grid::file::v1_6::schema as v1_6; use crate::grid::file::v1_6::schema_validation::Validations; -fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { +fn upgrade_column(x: i64, column: v1_5::Column) -> (i64, v1_6::Column) { ( - *x, + x, v1_6::Column { values: column .values - .iter() + .into_iter() .map(|(k, v)| { ( - k.into(), - match &v { - v1_5::CellValue::Text(value) => v1_6::CellValue::Text(value.clone()), - v1_5::CellValue::Number(value) => { - v1_6::CellValue::Number(value.to_owned()) - } - v1_5::CellValue::Html(value) => v1_6::CellValue::Html(value.clone()), + k, + match v { + v1_5::CellValue::Text(value) => v1_6::CellValue::Text(value), + v1_5::CellValue::Number(value) => v1_6::CellValue::Number(value), + v1_5::CellValue::Html(value) => v1_6::CellValue::Html(value), v1_5::CellValue::Blank => v1_6::CellValue::Blank, v1_5::CellValue::Code(code_cell) => { v1_6::CellValue::Code(v1_6::CodeCell { - language: match &code_cell.language { + language: match code_cell.language { v1_5::CodeCellLanguage::Python => { v1_6::CodeCellLanguage::Python } @@ -33,7 +31,7 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { v1_5::CodeCellLanguage::Javascript => { v1_6::CodeCellLanguage::Javascript } - v1_5::CodeCellLanguage::Connection { kind, ref id } => { + v1_5::CodeCellLanguage::Connection { kind, id } => { v1_6::CodeCellLanguage::Connection { kind: match kind { v1_5::ConnectionKind::Postgres => { @@ -43,32 +41,28 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { v1_6::ConnectionKind::Mysql } }, - id: id.clone(), + id, } } }, - code: code_cell.code.clone(), + code: code_cell.code, }) } - v1_5::CellValue::Logical(value) => v1_6::CellValue::Logical(*value), - v1_5::CellValue::Error(value) => v1_6::CellValue::Error(value.clone()), - v1_5::CellValue::Duration(value) => { - v1_6::CellValue::Duration(value.clone()) - } - v1_5::CellValue::Instant(value) => { - v1_6::CellValue::Instant(value.clone()) - } - v1_5::CellValue::Image(value) => v1_6::CellValue::Image(value.clone()), + v1_5::CellValue::Logical(value) => v1_6::CellValue::Logical(value), + v1_5::CellValue::Error(value) => v1_6::CellValue::Error(value), + v1_5::CellValue::Duration(value) => v1_6::CellValue::Duration(value), + v1_5::CellValue::Instant(value) => v1_6::CellValue::Instant(value), + v1_5::CellValue::Image(value) => v1_6::CellValue::Image(value), }, ) }) .collect(), vertical_align: column .vertical_align - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: match v.value { v1_5::CellVerticalAlign::Top => v1_6::CellVerticalAlign::Top, @@ -82,10 +76,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), align: column .align - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: match v.value { v1_5::CellAlign::Left => v1_6::CellAlign::Left, @@ -99,10 +93,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), wrap: column .wrap - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: match v.value { v1_5::CellWrap::Wrap => v1_6::CellWrap::Wrap, @@ -116,10 +110,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), numeric_format: column .numeric_format - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v1_6::NumericFormat { kind: match v.value.kind { @@ -136,7 +130,7 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { v1_6::NumericFormatKind::Exponential } }, - symbol: v.value.symbol.clone(), + symbol: v.value.symbol, }, len: v.len, }, @@ -145,10 +139,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), numeric_decimals: column .numeric_decimals - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v.value, len: v.len, @@ -158,10 +152,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), numeric_commas: column .numeric_commas - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v.value, len: v.len, @@ -171,10 +165,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), bold: column .bold - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v.value, len: v.len, @@ -184,10 +178,10 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), italic: column .italic - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v.value, len: v.len, @@ -197,12 +191,12 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), text_color: column .text_color - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { - value: v.value.clone(), + value: v.value, len: v.len, }, ) @@ -210,12 +204,12 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), fill_color: column .fill_color - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { - value: v.value.clone(), + value: v.value, len: v.len, }, ) @@ -223,14 +217,14 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { .collect(), render_size: column .render_size - .iter() + .into_iter() .map(|(k, v)| { ( - k.clone(), + k, v1_6::ColumnRepeat { value: v1_6::RenderSize { - w: v.value.w.clone(), - h: v.value.h.clone(), + w: v.value.w, + h: v.value.h, }, len: v.len, }, @@ -242,54 +236,54 @@ fn upgrade_column(x: &i64, column: &v1_5::Column) -> (i64, v1_6::Column) { ) } -fn upgrade_columns(sheet: &v1_5::Sheet) -> Vec<(i64, v1_6::Column)> { - sheet - .columns - .iter() +fn upgrade_columns(columns: Vec<(i64, v1_5::Column)>) -> Vec<(i64, v1_6::Column)> { + columns + .into_iter() .map(|(x, column)| upgrade_column(x, column)) .collect() } -fn upgrade_code_runs(sheet: &v1_5::Sheet) -> Vec<(v1_6::Pos, v1_6::CodeRun)> { - sheet - .code_runs - .iter() +fn upgrade_code_runs( + code_runs: Vec<(v1_5::Pos, v1_5::CodeRun)>, +) -> Vec<(v1_6::Pos, v1_6::CodeRun)> { + code_runs + .into_iter() .map(|(pos, old_code_run)| { ( v1_6::Pos { x: pos.x, y: pos.y }, v1_6::CodeRun { - formatted_code_string: old_code_run.formatted_code_string.clone(), + formatted_code_string: old_code_run.formatted_code_string, last_modified: old_code_run.last_modified, - std_err: old_code_run.std_err.clone(), - std_out: old_code_run.std_out.clone(), + std_err: old_code_run.std_err, + std_out: old_code_run.std_out, spill_error: old_code_run.spill_error, - cells_accessed: old_code_run.cells_accessed.clone(), - return_type: old_code_run.return_type.clone(), + cells_accessed: old_code_run.cells_accessed, + return_type: old_code_run.return_type, line_number: old_code_run.line_number, - output_type: old_code_run.output_type.clone(), - result: match &old_code_run.result { + output_type: old_code_run.output_type, + result: match old_code_run.result { v1_5::CodeRunResult::Ok(output_value) => { v1_6::CodeRunResult::Ok(match output_value { v1_5::OutputValue::Single(value) => { if value.type_field.to_lowercase() == "text" { v1_6::OutputValue::Single(v1_6::CellValue::Text( - value.value.to_owned(), + value.value, )) } else if value.type_field.to_lowercase() == "number" { v1_6::OutputValue::Single(v1_6::CellValue::Number( - value.value.to_owned(), + value.value, )) } else if value.type_field.to_lowercase() == "logical" { v1_6::OutputValue::Single(v1_6::CellValue::Logical( - v1_5::string_bool(&value.value), + v1_5::string_bool(value.value), )) } else if value.type_field.to_lowercase() == "html" { v1_6::OutputValue::Single(v1_6::CellValue::Html( - value.value.to_owned(), + value.value, )) } else if value.type_field.to_lowercase() == "image" { v1_6::OutputValue::Single(v1_6::CellValue::Image( - value.value.to_owned(), + value.value, )) } else if value.type_field.to_lowercase() == "blank" { v1_6::OutputValue::Single(v1_6::CellValue::Blank) @@ -302,28 +296,28 @@ fn upgrade_code_runs(sheet: &v1_5::Sheet) -> Vec<(v1_6::Pos, v1_6::CodeRun)> { v1_6::OutputValue::Array(v1_6::OutputArray { values: array .values - .iter() + .into_iter() .map(|value| { if value.type_field.to_lowercase().as_str() == "text" { - v1_6::CellValue::Text(value.value.to_owned()) + v1_6::CellValue::Text(value.value) } else if value.type_field.to_lowercase().as_str() == "number" { - v1_6::CellValue::Number(value.value.to_owned()) + v1_6::CellValue::Number(value.value) } else if value.type_field.to_lowercase() == "logical" { v1_6::CellValue::Logical(v1_5::string_bool( - &value.value, + value.value, )) } else if value.type_field.to_lowercase() == "html" { - v1_6::CellValue::Html(value.value.to_owned()) + v1_6::CellValue::Html(value.value) } else if value.type_field.to_lowercase() == "image" { - v1_6::CellValue::Image(value.value.to_owned()) + v1_6::CellValue::Image(value.value) } else if value.type_field.to_lowercase() == "blank" { v1_6::CellValue::Blank @@ -349,8 +343,8 @@ fn upgrade_code_runs(sheet: &v1_5::Sheet) -> Vec<(v1_6::Pos, v1_6::CodeRun)> { } v1_5::CodeRunResult::Err(error) => { v1_6::CodeRunResult::Err(v1_6::RunError { - span: error.span.clone(), - msg: error.msg.clone(), + span: error.span, + msg: error.msg, }) } }, @@ -360,24 +354,16 @@ fn upgrade_code_runs(sheet: &v1_5::Sheet) -> Vec<(v1_6::Pos, v1_6::CodeRun)> { .collect() } -fn upgrade_borders(sheet: &v1_5::Sheet) -> v1_6::Borders { - sheet - .borders - .iter() - .map(|(x, borders)| (x.clone(), borders.clone())) - .collect() -} - -fn upgrade_sheet(sheet: &v1_5::Sheet) -> v1_6::Sheet { +fn upgrade_sheet(sheet: v1_5::Sheet) -> v1_6::Sheet { v1_6::Sheet { - id: v1_6::Id::from(sheet.id.id.clone()), - name: sheet.name.clone(), - color: sheet.color.clone(), - order: sheet.order.clone(), - offsets: sheet.offsets.clone(), - columns: upgrade_columns(sheet), - borders: upgrade_borders(sheet), - code_runs: upgrade_code_runs(sheet), + id: v1_6::Id::from(sheet.id.id), + name: sheet.name, + color: sheet.color, + order: sheet.order, + offsets: sheet.offsets, + columns: upgrade_columns(sheet.columns), + borders: sheet.borders, + code_runs: upgrade_code_runs(sheet.code_runs), formats_all: None, formats_columns: vec![], formats_rows: vec![], @@ -389,7 +375,7 @@ fn upgrade_sheet(sheet: &v1_5::Sheet) -> v1_6::Sheet { pub(crate) fn upgrade(schema: v1_5::GridSchema) -> Result { let schema = v1_6::GridSchema { version: Some("1.6".into()), - sheets: schema.sheets.iter().map(upgrade_sheet).collect(), + sheets: schema.sheets.into_iter().map(upgrade_sheet).collect(), }; Ok(schema) } diff --git a/quadratic-core/src/grid/file/v1_5/schema.rs b/quadratic-core/src/grid/file/v1_5/schema.rs index 7ecff1b37c..67151f6629 100644 --- a/quadratic-core/src/grid/file/v1_5/schema.rs +++ b/quadratic-core/src/grid/file/v1_5/schema.rs @@ -206,7 +206,7 @@ pub enum CellValue { Image(String), } -pub fn string_bool(s: &str) -> bool { +pub fn string_bool(s: String) -> bool { match s.to_ascii_lowercase().as_str() { "true" => true, _ => false, diff --git a/quadratic-core/src/grid/file/v1_6/file.rs b/quadratic-core/src/grid/file/v1_6/file.rs index f0b189bf91..2c88c68544 100644 --- a/quadratic-core/src/grid/file/v1_6/file.rs +++ b/quadratic-core/src/grid/file/v1_6/file.rs @@ -21,13 +21,13 @@ use crate::{ // } fn upgrade_borders(borders: current::Borders) -> Result { - fn convert_border_style(border_style: ¤t::CellBorder) -> Result { + fn convert_border_style(border_style: current::CellBorder) -> Result { let mut color = Rgba::color_from_str(&border_style.color)?; // the alpha was set incorrectly to 1; should be 255 color.alpha = 255; - let line = match border_style.line.as_ref() { + let line = match border_style.line.as_str() { "line1" => CellBorderLine::Line1, "line2" => CellBorderLine::Line2, "line3" => CellBorderLine::Line3, @@ -47,23 +47,23 @@ fn upgrade_borders(borders: current::Borders) -> Result { let col: i64 = col_id .parse::() .expect("Failed to parse col_id as i64"); - for (row, row_borders) in sheet_borders { - if let Some(left_old) = row_borders[0].as_ref() { + for (row, mut row_borders) in sheet_borders { + if let Some(left_old) = row_borders[0].take() { if let Ok(style) = convert_border_style(left_old) { borders_new.set(col, row, None, None, Some(style), None); } } - if let Some(right_old) = row_borders[2].as_ref() { + if let Some(right_old) = row_borders[2].take() { if let Ok(style) = convert_border_style(right_old) { borders_new.set(col, row, None, None, None, Some(style)); } } - if let Some(top_old) = row_borders[1].as_ref() { + if let Some(top_old) = row_borders[1].take() { if let Ok(style) = convert_border_style(top_old) { borders_new.set(col, row, Some(style), None, None, None); } } - if let Some(bottom_old) = row_borders[3].as_ref() { + if let Some(bottom_old) = row_borders[3].take() { if let Ok(style) = convert_border_style(bottom_old) { borders_new.set(col, row, None, Some(style), None, None); } @@ -71,25 +71,25 @@ fn upgrade_borders(borders: current::Borders) -> Result { } } - let borders = export_borders(&borders_new); + let borders = export_borders(borders_new); Ok(borders) } -pub fn upgrade_sheet(sheet: ¤t::Sheet) -> Result { +pub fn upgrade_sheet(sheet: current::Sheet) -> Result { Ok(v1_7::SheetSchema { - id: sheet.id.clone(), - name: sheet.name.clone(), - color: sheet.color.clone(), - order: sheet.order.clone(), - offsets: sheet.offsets.clone(), - columns: sheet.columns.clone(), - code_runs: sheet.code_runs.clone(), - formats_all: sheet.formats_all.clone(), - formats_columns: sheet.formats_columns.clone(), - formats_rows: sheet.formats_rows.clone(), - rows_resize: sheet.rows_resize.clone(), - validations: sheet.validations.clone(), - borders: upgrade_borders(sheet.borders.clone())?, + id: sheet.id, + name: sheet.name, + color: sheet.color, + order: sheet.order, + offsets: sheet.offsets, + columns: sheet.columns, + code_runs: sheet.code_runs, + formats_all: sheet.formats_all, + formats_columns: sheet.formats_columns, + formats_rows: sheet.formats_rows, + rows_resize: sheet.rows_resize, + validations: sheet.validations, + borders: upgrade_borders(sheet.borders)?, }) } @@ -98,7 +98,7 @@ pub fn upgrade(grid: current::GridSchema) -> Result { version: Some("1.7".to_string()), sheets: grid .sheets - .iter() + .into_iter() .map(upgrade_sheet) .collect::>()?, }; diff --git a/quadratic-core/src/grid/resize.rs b/quadratic-core/src/grid/resize.rs index a8e860f593..cdae7c01a3 100644 --- a/quadratic-core/src/grid/resize.rs +++ b/quadratic-core/src/grid/resize.rs @@ -45,6 +45,10 @@ impl ResizeMap { pub fn iter_resize(&self) -> impl '_ + Iterator { self.resize_map.iter().map(|(&k, &v)| (k, v)) } + + pub fn into_iter_resize(self) -> impl Iterator { + self.resize_map.into_iter() + } } #[cfg(test)] diff --git a/quadratic-core/src/sheet_offsets/mod.rs b/quadratic-core/src/sheet_offsets/mod.rs index b48bb9a10a..4da67fd378 100644 --- a/quadratic-core/src/sheet_offsets/mod.rs +++ b/quadratic-core/src/sheet_offsets/mod.rs @@ -38,15 +38,15 @@ pub type OffsetWidthHeight = (Vec<(i64, f64)>, Vec<(i64, f64)>); impl SheetOffsets { /// exports offsets to a GridFile - pub fn export(&self) -> OffsetWidthHeight { + pub fn export(self) -> OffsetWidthHeight { ( - self.column_widths.iter_sizes().collect(), - self.row_heights.iter_sizes().collect(), + self.column_widths.into_iter_sizes().collect(), + self.row_heights.into_iter_sizes().collect(), ) } /// import offsets from a GridFile - pub fn import(offsets: &OffsetWidthHeight) -> Self { + pub fn import(offsets: OffsetWidthHeight) -> Self { let mut offsets = SheetOffsets { column_widths: Offsets::from_iter( crate::DEFAULT_COLUMN_WIDTH, diff --git a/quadratic-core/src/sheet_offsets/offsets.rs b/quadratic-core/src/sheet_offsets/offsets.rs index b694e3f83a..b7b084b86e 100644 --- a/quadratic-core/src/sheet_offsets/offsets.rs +++ b/quadratic-core/src/sheet_offsets/offsets.rs @@ -130,6 +130,11 @@ impl Offsets { self.sizes.iter().map(|(&k, &v)| (k, v)) } + /// Iterates over the sizes of all columns/rows - owned. + pub fn into_iter_sizes(self) -> impl Iterator { + self.sizes.into_iter() + } + /// Gets a list of changes between this and another `Offsets` structure. /// This is used by TS to rapidly change positioning of CellSheets after an offsets change. ///