Change LineData from an enum to a struct with optional tag

This commit is contained in:
Adam Chalmers
2023-12-19 11:47:28 -06:00
parent 185443a963
commit 04976d509c

View File

@ -151,17 +151,12 @@ async fn inner_y_line_to(
/// Data to draw a line. /// Data to draw a line.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase", untagged)] #[serde(rename_all = "camelCase")]
pub enum LineData { pub struct LineData {
/// A point with a tag.
PointWithTag {
/// The to point. /// The to point.
to: [f64; 2], to: [f64; 2],
/// The tag. /// The tag.
tag: String, tag: Option<String>,
},
/// A point.
Point([f64; 2]),
} }
/// Draw a line. /// Draw a line.
@ -178,10 +173,7 @@ pub async fn line(args: Args) -> Result<MemoryItem, KclError> {
}] }]
async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> { async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let inner_args = match &data { let inner_args = data.to;
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let delta = inner_args; let delta = inner_args;
let to = [from.x + inner_args[0], from.y + inner_args[1]]; let to = [from.x + inner_args[0], from.y + inner_args[1]];
@ -208,11 +200,7 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
base: BasePath { base: BasePath {
from: from.into(), from: from.into(),
to, to,
name: if let LineData::PointWithTag { tag, .. } = data { name: data.tag.unwrap_or_default(),
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -260,8 +248,14 @@ async fn inner_x_line(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data { let line_data = match data {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [length, 0.0], tag }, AxisLineData::LengthWithTag { length, tag } => LineData {
AxisLineData::Length(length) => LineData::Point([length, 0.0]), to: [length, 0.0],
tag: Some(tag),
},
AxisLineData::Length(length) => LineData {
to: [length, 0.0],
tag: None,
},
}; };
let new_sketch_group = inner_line(line_data, sketch_group, args).await?; let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
@ -286,8 +280,14 @@ async fn inner_y_line(
args: Args, args: Args,
) -> Result<Box<SketchGroup>, KclError> { ) -> Result<Box<SketchGroup>, KclError> {
let line_data = match data { let line_data = match data {
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [0.0, length], tag }, AxisLineData::LengthWithTag { length, tag } => LineData {
AxisLineData::Length(length) => LineData::Point([0.0, length]), to: [length, 0.0],
tag: Some(tag),
},
AxisLineData::Length(length) => LineData {
to: [length, 0.0],
tag: None,
},
}; };
let new_sketch_group = inner_line(line_data, sketch_group, args).await?; let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
@ -314,11 +314,12 @@ pub enum AngledLineData {
impl AngledLineData { impl AngledLineData {
pub fn into_inner_line(self, to: [f64; 2]) -> LineData { pub fn into_inner_line(self, to: [f64; 2]) -> LineData {
if let AngledLineData::AngleWithTag { tag, .. } = self { let tag = if let AngledLineData::AngleWithTag { tag, .. } = self {
LineData::PointWithTag { to, tag } Some(tag)
} else { } else {
LineData::Point(to) None
} };
LineData { to, tag }
} }
} }
@ -814,10 +815,7 @@ pub async fn start_profile_at(args: Args) -> Result<MemoryItem, KclError> {
name = "startProfileAt", name = "startProfileAt",
}] }]
async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -> Result<Box<SketchGroup>, KclError> { async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -> Result<Box<SketchGroup>, KclError> {
let to = match &data { let to = data.to;
LineData::PointWithTag { to, .. } => *to,
LineData::Point(to) => *to,
};
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
let path_id = uuid::Uuid::new_v4(); let path_id = uuid::Uuid::new_v4();
@ -827,11 +825,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
id, id,
ModelingCmd::MovePathPen { ModelingCmd::MovePathPen {
path: path_id, path: path_id,
to: Point3D { to: into_3d(to, 0.0),
x: to[0],
y: to[1],
z: 0.0,
},
}, },
) )
.await?; .await?;
@ -839,11 +833,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
let current_path = BasePath { let current_path = BasePath {
from: to, from: to,
to, to,
name: if let LineData::PointWithTag { tag, .. } = data { name: data.tag.unwrap_or_default(),
tag.to_string()
} else {
"".to_string()
},
geo_meta: GeoMeta { geo_meta: GeoMeta {
id, id,
metadata: args.source_range.into(), metadata: args.source_range.into(),
@ -1363,21 +1353,30 @@ mod tests {
#[test] #[test]
fn test_deserialize_line_data() { fn test_deserialize_line_data() {
let data = LineData::Point([0.0, 1.0]); let data = LineData {
to: [0.0, 1.0],
tag: None,
};
let mut str_json = serde_json::to_string(&data).unwrap(); let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "[0.0,1.0]"); assert_eq!(str_json, "[0.0,1.0]");
str_json = "[0, 1]".to_string(); str_json = "[0, 1]".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap(); let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, LineData::Point([0.0, 1.0])); assert_eq!(
data,
LineData {
to: [0.0, 1.0],
tag: None
}
);
str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string(); str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string();
let data: LineData = serde_json::from_str(&str_json).unwrap(); let data: LineData = serde_json::from_str(&str_json).unwrap();
assert_eq!( assert_eq!(
data, data,
LineData::PointWithTag { LineData {
to: [0.0, 1.0], to: [0.0, 1.0],
tag: "thing".to_string() tag: Some("thing".to_string()),
} }
); );
} }