Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup clones for upgrade/import/export file for v1.7 #1901

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions quadratic-core/src/grid/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ impl<B: BlockContent> ColumnData<B> {
self.0.values()
}

pub fn into_blocks(self) -> impl Iterator<Item = Block<B>> {
self.0.into_values()
}

pub fn has_blocks_in_range(&self, y_range: Range<i64>) -> bool {
self.blocks_covering_range(y_range).next().is_some()
}
Expand Down
120 changes: 60 additions & 60 deletions quadratic-core/src/grid/file/serialize/borders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -33,72 +33,72 @@ 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<SameValue<BorderStyleTimestamp>>,
data: ColumnData<SameValue<BorderStyleTimestamp>>,
) -> HashMap<i64, current::ColumnRepeatSchema<current::BorderStyleTimestampSchema>> {
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 })
})
.collect()
}

fn export_border_side(
data: &HashMap<i64, ColumnData<SameValue<BorderStyleTimestamp>>>,
data: HashMap<i64, ColumnData<SameValue<BorderStyleTimestamp>>>,
) -> HashMap<i64, HashMap<i64, current::ColumnRepeatSchema<current::BorderStyleTimestampSchema>>> {
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<i64, BorderStyleCell>,
data: HashMap<i64, BorderStyleCell>,
) -> HashMap<i64, current::BorderStyleCellSchema> {
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: &current::RgbaSchema) -> Rgba {
fn import_rgba(schema: current::RgbaSchema) -> Rgba {
Rgba {
red: schema.red,
green: schema.green,
Expand All @@ -107,7 +107,7 @@ fn import_rgba(schema: &current::RgbaSchema) -> Rgba {
}
}

fn import_border_line(schema: &current::CellBorderLineSchema) -> CellBorderLine {
fn import_border_line(schema: current::CellBorderLineSchema) -> CellBorderLine {
match schema {
current::CellBorderLineSchema::Line1 => CellBorderLine::Line1,
current::CellBorderLineSchema::Line2 => CellBorderLine::Line2,
Expand All @@ -123,31 +123,31 @@ fn import_timestamp(value: u32) -> SmallTimestamp {
SmallTimestamp::new(value)
}

fn import_border_style_cell(schema: &current::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: &current::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<i64, current::ColumnRepeatSchema<current::BorderStyleTimestampSchema>>,
schema: HashMap<i64, current::ColumnRepeatSchema<current::BorderStyleTimestampSchema>>,
) -> ColumnData<SameValue<BorderStyleTimestamp>> {
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);
});
Expand All @@ -156,36 +156,36 @@ fn import_column_repeat(
}

fn import_border_side(
schema: &HashMap<
schema: HashMap<
i64,
HashMap<i64, current::ColumnRepeatSchema<current::BorderStyleTimestampSchema>>,
>,
) -> HashMap<i64, ColumnData<SameValue<BorderStyleTimestamp>>> {
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<i64, current::BorderStyleCellSchema>,
schema: HashMap<i64, current::BorderStyleCellSchema>,
) -> HashMap<i64, BorderStyleCell> {
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: &current::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),
}
}

Expand Down Expand Up @@ -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);
}
}
60 changes: 29 additions & 31 deletions quadratic-core/src/grid/file/serialize/cell_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
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,
Expand All @@ -30,16 +30,16 @@
}
},
}),
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))

Check warning on line 40 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
}
CellValue::Image(image) => current::CellValueSchema::Image(image),
}
}

Expand All @@ -55,44 +55,42 @@
CellValue::Number(BigDecimal::from_str(&number).unwrap_or_default())
}

pub fn import_cell_value(value: &current::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 } => {

Check warning on line 70 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L70

Added line #L70 was not covered by tests
CodeCellLanguage::Connection {
kind: match kind {
current::ConnectionKindSchema::Postgres => ConnectionKind::Postgres,
current::ConnectionKindSchema::Mysql => ConnectionKind::Mysql,
current::ConnectionKindSchema::Mssql => ConnectionKind::Mssql,
},
id: id.clone(),
id,

Check warning on line 77 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L77

Added line #L77 was not covered by tests
}
}
},
}),
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())

Check warning on line 84 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L84

Added line #L84 was not covered by tests
}
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())

Check warning on line 87 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L87

Added line #L87 was not covered by tests
}
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())),

Check warning on line 92 in quadratic-core/src/grid/file/serialize/cell_value.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/grid/file/serialize/cell_value.rs#L92

Added line #L92 was not covered by tests
current::CellValueSchema::Image(text) => CellValue::Image(text),
}
}

Expand Down
Loading
Loading