Skip to content

Commit 69f822c

Browse files
committed
remove old points serialization
1 parent 19befd3 commit 69f822c

1 file changed

Lines changed: 85 additions & 87 deletions

File tree

src/persistence.rs

Lines changed: 85 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ use crate::app_ui::GraphConfig;
1212
use crate::custom_rendering::fan_fill_renderer::FillRule;
1313
use crate::draw_buffer::DrawBuffer;
1414
use crate::entry::{
15-
FillAlpha,
16-
ColorEntry, ColorEntryType, EntryColor, EquationType, Expr, FunctionType, LabelConfig, LabelPosition, LabelSize, LineStyleConfig, MAX_IMPLICIT_RESOLUTION, MIN_IMPLICIT_RESOLUTION, PointDrag, PointDragType, PointStyle, PointsType, preprocess_ast
15+
ColorEntry, ColorEntryType, EntryColor, EquationType, Expr, FillAlpha, FunctionType, LabelConfig, LabelPosition, LabelSize, LineStyleConfig, MAX_IMPLICIT_RESOLUTION, MIN_IMPLICIT_RESOLUTION, PointDrag, PointDragType, PointStyle, PointsType, preprocess_ast
16+
};
17+
use crate::{
18+
ConstantType, Entry, EntryType, GraphState, IdGenerator, PointEntry, State, UiState, load_graph_state
1719
};
18-
use crate::{ConstantType, Entry, EntryType, GraphState, IdGenerator, PointEntry, State, UiState, load_graph_state};
1920

2021
pub fn default_true() -> bool { true }
2122

@@ -35,7 +36,7 @@ pub struct EntrySerialized {
3536
name: String,
3637
#[serde(default = "default_true")]
3738
visible: bool,
38-
#[serde(default)]
39+
#[serde(default)]
3940
color: EntryColor,
4041
ty: EntryTypeSerialized,
4142
}
@@ -89,7 +90,7 @@ pub enum EntryTypeSerialized {
8990
#[serde(default)]
9091
fill_rule: FillRule,
9192
#[serde(default)]
92-
fill_alpha: FillAlpha,
93+
fill_alpha: FillAlpha,
9394
},
9495
Constant {
9596
value: f64,
@@ -101,11 +102,11 @@ pub enum EntryTypeSerialized {
101102
range_end: ExprSer,
102103
},
103104
Points {
104-
// TODO: remove `points` and unwrap Option around `points_ty`
105-
#[serde(default)]
106-
points: Vec<EntryPointSerialized>,
107-
#[serde(default)]
108-
points_ty: Option<EntryPointTypeSerialized>,
105+
// // TODO: remove `points` and unwrap Option around `points_ty`
106+
// #[serde(default)]
107+
// points: Vec<EntryPointSerialized>,
108+
// #[serde(default)]
109+
points_ty: EntryPointTypeSerialized,
109110
#[serde(default)]
110111
style: PointStyleSerialized,
111112
},
@@ -116,8 +117,8 @@ pub enum EntryTypeSerialized {
116117
Color {
117118
#[serde(default)]
118119
expr: ExprSer,
119-
#[serde(default)]
120-
ty: ColorEntryType
120+
#[serde(default)]
121+
ty: ColorEntryType,
121122
},
122123
}
123124

@@ -187,8 +188,8 @@ pub struct PointStyleSerialized {
187188
fill_rule: FillRule,
188189
#[serde(default)]
189190
connect_first_and_last: bool,
190-
#[serde(default)]
191-
fill_alpha: FillAlpha,
191+
#[serde(default)]
192+
fill_alpha: FillAlpha,
192193
}
193194
impl PointStyleSerialized {
194195
fn from_point_style<T: EvalexprFloat>(point_style: &PointStyle<T>) -> Self {
@@ -203,7 +204,7 @@ impl PointStyleSerialized {
203204
.map(LabelConfigSerialized::from_label_config),
204205
fill: point_style.fill,
205206
fill_rule: point_style.fill_rule,
206-
fill_alpha: point_style.fill_alpha,
207+
fill_alpha: point_style.fill_alpha,
207208
connect_first_and_last: point_style.connect_first_and_last,
208209
}
209210
}
@@ -216,7 +217,7 @@ impl PointStyleSerialized {
216217
label_config: self.label_config.map(|label_config| label_config.into_label_config()),
217218
fill: self.fill,
218219
fill_rule: self.fill_rule,
219-
fill_alpha: self.fill_alpha,
220+
fill_alpha: self.fill_alpha,
220221
connect_first_and_last: self.connect_first_and_last,
221222
}
222223
}
@@ -240,7 +241,7 @@ pub fn serialize_entries<T: EvalexprFloat>(entries: &[Entry<T>]) -> Vec<EntrySer
240241
implicit_resolution,
241242
parametric_fill,
242243
fill_rule,
243-
fill_alpha,
244+
fill_alpha,
244245
..
245246
} => EntryTypeSerialized::Function {
246247
func: ExprSer::from_expr(func),
@@ -251,7 +252,7 @@ pub fn serialize_entries<T: EvalexprFloat>(entries: &[Entry<T>]) -> Vec<EntrySer
251252
fill_rule: *fill_rule,
252253
implicit_resolution: *implicit_resolution,
253254
parametric_fill: *parametric_fill,
254-
fill_alpha: *fill_alpha,
255+
fill_alpha: *fill_alpha,
255256
},
256257
EntryType::Constant { value, step, ty, istr_name: _, range_start, range_end } => {
257258
EntryTypeSerialized::Constant {
@@ -274,16 +275,14 @@ pub fn serialize_entries<T: EvalexprFloat>(entries: &[Entry<T>]) -> Vec<EntrySer
274275
points_serialized.push(point_serialized);
275276
}
276277
EntryTypeSerialized::Points {
277-
points_ty: Some(EntryPointTypeSerialized::Separate { points: points_serialized }),
278-
points: Vec::new(),
278+
points_ty: EntryPointTypeSerialized::Separate { points: points_serialized },
279+
// points: Vec::new(),
279280
style: PointStyleSerialized::from_point_style(style),
280281
}
281282
},
282283
PointsType::SingleExpr { expr, .. } => EntryTypeSerialized::Points {
283-
points_ty: Some(EntryPointTypeSerialized::SingleExpr {
284-
expr: ExprSer::from_expr(expr),
285-
}),
286-
points: Vec::new(),
284+
points_ty: EntryPointTypeSerialized::SingleExpr { expr: ExprSer::from_expr(expr) },
285+
// points: Vec::new(),
287286
style: PointStyleSerialized::from_point_style(style),
288287
},
289288
},
@@ -292,7 +291,7 @@ pub fn serialize_entries<T: EvalexprFloat>(entries: &[Entry<T>]) -> Vec<EntrySer
292291
EntryTypeSerialized::Folder { entries: ser_entries }
293292
},
294293
EntryType::Color(color) => {
295-
EntryTypeSerialized::Color { expr: ExprSer::from_expr(&color.expr) ,ty: color.ty}
294+
EntryTypeSerialized::Color { expr: ExprSer::from_expr(&color.expr), ty: color.ty }
296295
},
297296
},
298297
};
@@ -357,35 +356,36 @@ pub fn deserialize_from_url<T: EvalexprFloat>(
357356
// deserialize_from_json(decoded.as_bytes())
358357
}
359358

360-
pub fn deserialize_graph_state<T: EvalexprFloat>(name: String, ser: StateSerialized) -> Result<GraphState<T>,String> {
361-
let entries = deserialize_entries::<T>(ser.entries);
362-
let mut unique_ids = FxHashSet::default();
363-
let mut max_id = 0;
364-
for entry in entries.iter() {
365-
max_id = entry.id.max(max_id);
366-
if !unique_ids.insert(entry.id) {
367-
return Err(format!("Error deserializing graph state: Duplicate id: {}", entry.id));
368-
}
369-
if let EntryType::Folder { entries } = &entry.ty {
370-
for sub_entry in entries.iter() {
371-
max_id = sub_entry.id.max(max_id);
372-
if !unique_ids.insert(sub_entry.id) {
373-
return Err(format!("Error deserializing graph state: Duplicate id: {}", sub_entry.id));
374-
}
375-
}
376-
}
377-
}
378-
379-
let max_id = max_id + 1;
359+
pub fn deserialize_graph_state<T: EvalexprFloat>(
360+
name: String, ser: StateSerialized,
361+
) -> Result<GraphState<T>, String> {
362+
let entries = deserialize_entries::<T>(ser.entries);
363+
let mut unique_ids = FxHashSet::default();
364+
let mut max_id = 0;
365+
for entry in entries.iter() {
366+
max_id = entry.id.max(max_id);
367+
if !unique_ids.insert(entry.id) {
368+
return Err(format!("Error deserializing graph state: Duplicate id: {}", entry.id));
369+
}
370+
if let EntryType::Folder { entries } = &entry.ty {
371+
for sub_entry in entries.iter() {
372+
max_id = sub_entry.id.max(max_id);
373+
if !unique_ids.insert(sub_entry.id) {
374+
return Err(format!("Error deserializing graph state: Duplicate id: {}", sub_entry.id));
375+
}
376+
}
377+
}
378+
}
380379

380+
let max_id = max_id + 1;
381381

382382
Ok(GraphState {
383383
entries,
384384
saved_graph_config: ser.graph_config.clone(),
385385
current_graph_config: ser.graph_config,
386386
name,
387387
id_gen: IdGenerator::new(max_id),
388-
prev_plot_transform: None,
388+
prev_plot_transform: None,
389389
})
390390
}
391391
pub fn deserialize_entries<T: EvalexprFloat>(entries: Vec<EntrySerialized>) -> Vec<Entry<T>> {
@@ -406,13 +406,13 @@ pub fn deserialize_entries<T: EvalexprFloat>(entries: Vec<EntrySerialized>) -> V
406406
implicit_resolution,
407407
fill_rule,
408408
parametric_fill,
409-
fill_alpha
409+
fill_alpha,
410410
} => EntryType::Function {
411411
parametric: ranged,
412412
parametric_fill,
413413
fill_rule,
414414
identifier: istr_empty(),
415-
fill_alpha,
415+
fill_alpha,
416416
func: func.into_expr(true),
417417
// Actual type will be set in compilation step later
418418
ty: FunctionType::Expression,
@@ -436,51 +436,49 @@ pub fn deserialize_entries<T: EvalexprFloat>(entries: Vec<EntrySerialized>) -> V
436436
range_end: range_end.into_expr(false),
437437
}
438438
},
439-
EntryTypeSerialized::Points { points, points_ty, style } => {
440-
let points = if points_ty.is_none() {
441-
Some(&points)
442-
} else if let Some(EntryPointTypeSerialized::Separate { points }) = &points_ty {
443-
Some(points)
444-
} else {
445-
None
446-
};
447-
if let Some(points) = points {
448-
let mut points_deserialized = Vec::new();
449-
for point in points {
450-
let point_deserialized = PointEntry {
451-
x: point.x.clone().into_expr(false),
452-
y: point.y.clone().into_expr(false),
453-
drag: PointDrag {
454-
drag_point: None,
455-
drag_type: point.drag_type,
456-
both_drag_dirs_available: true,
457-
},
458-
val: None,
459-
};
460-
points_deserialized.push(point_deserialized);
461-
}
462-
EntryType::Points {
463-
points_ty: PointsType::Separate(points_deserialized),
439+
EntryTypeSerialized::Points { points_ty, style } => {
440+
// let points = if points_ty.is_none() {
441+
// Some(&points)
442+
// } else if let Some(EntryPointTypeSerialized::Separate { points }) = &points_ty {
443+
// Some(points)
444+
// } else {
445+
// None
446+
// };
447+
match points_ty {
448+
EntryPointTypeSerialized::Separate { points } => {
449+
let mut points_deserialized = Vec::new();
450+
for point in points {
451+
let point_deserialized = PointEntry {
452+
x: point.x.clone().into_expr(false),
453+
y: point.y.clone().into_expr(false),
454+
drag: PointDrag {
455+
drag_point: None,
456+
drag_type: point.drag_type,
457+
both_drag_dirs_available: true,
458+
},
459+
val: None,
460+
};
461+
points_deserialized.push(point_deserialized);
462+
}
463+
EntryType::Points {
464+
points_ty: PointsType::Separate(points_deserialized),
464465

465-
style: PointStyleSerialized::into_point_style(style),
466-
identifier: istr_empty(),
467-
}
468-
} else {
469-
let Some(EntryPointTypeSerialized::SingleExpr { expr }) = points_ty else {
470-
unreachable!()
471-
};
472-
EntryType::Points {
466+
style: PointStyleSerialized::into_point_style(style),
467+
identifier: istr_empty(),
468+
}
469+
},
470+
EntryPointTypeSerialized::SingleExpr { expr } => EntryType::Points {
473471
points_ty: PointsType::SingleExpr { expr: expr.into_expr(false), val: vec![] },
474472
style: PointStyleSerialized::into_point_style(style),
475473
identifier: istr_empty(),
476-
}
474+
},
477475
}
478476
},
479477
EntryTypeSerialized::Folder { entries } => {
480478
let entries = deserialize_entries(entries);
481479
EntryType::Folder { entries }
482480
},
483-
EntryTypeSerialized::Color { expr ,ty} => {
481+
EntryTypeSerialized::Color { expr, ty } => {
484482
EntryType::Color(ColorEntry { expr: expr.into_expr(false), ty })
485483
},
486484
},
@@ -618,10 +616,10 @@ pub fn persistence_ui<T: EvalexprFloat>(
618616
for file_name in ui_state.serialized_states.keys() {
619617
ui.label(file_name);
620618
if ui.button("Load").clicked() {
621-
let graph_state_res = load_file(&ui_state.cur_dir, &ui_state.serialized_states, file_name);
622-
load_graph_state(ui_state,state, graph_state_res);
623-
// todo: hack for borrow checker, fix later
624-
break;
619+
let graph_state_res = load_file(&ui_state.cur_dir, &ui_state.serialized_states, file_name);
620+
load_graph_state(ui_state, state, graph_state_res);
621+
// todo: hack for borrow checker, fix later
622+
break;
625623
}
626624
if ui.button("Delete").clicked() {
627625
ui_state.file_to_remove = Some(file_name.clone());

0 commit comments

Comments
 (0)