Skip to content

Commit

Permalink
On PathSegment, derive Value instead of manually implementing it
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Dec 21, 2023
1 parent 541a921 commit 1dcb69a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: execution-plan/src/tests.rs
assertion_line: 170
expression: debug_dump_memory(&mem)
---
┌───────┬──────────┬──────────────────────────────────────┐
│ index │ val_type │ value │
├───────┼──────────┼──────────────────────────────────────┤
│ 0 │ Uuid │ 4cd175a3-e313-4c91-b624-368bea3c0483 │
│ 1 │ Float │ 40 │
│ 2 │ Bool │ true │
│ 3 │ String │ png │
│ 4 │ Float │ -20 │
│ 5 │ Float │ -20 │
│ 6 │ Float │ -20 │
│ 7 │ String │ Line │
│ 8 │ Float │ 20 │
│ 9 │ Float │ -20 │
│ 10 │ Float │ -20 │
│ 11 │ Bool │ false │
│ 12 │ String │ Line │
│ 13 │ Float │ 20 │
│ 14 │ Float │ 20 │
│ 15 │ Float │ -20 │
│ 16 │ Bool │ false │
│ 17 │ String │ Line │
│ 18 │ Float │ -20 │
│ 19 │ Float │ 20 │
│ 20 │ Float │ -20 │
│ 21 │ Bool │ false │
│ 22 │ String │ Line │
│ 23 │ Float │ -20 │
│ 24 │ Float │ -20 │
│ 25 │ Float │ -20 │
│ 26 │ Bool │ false │
└───────┴──────────┴──────────────────────────────────────┘
139 changes: 1 addition & 138 deletions modeling-cmds/src/kcep_value.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use kittycad_execution_plan_traits::{MemoryError, Primitive, Value};

use crate::{
ok_response::OkModelingCmdResponse,
output,
shared::{Angle, PathSegment, Point2d, Point3d},
};
use crate::{ok_response::OkModelingCmdResponse, output};

pub(crate) const EMPTY: &str = "EMPTY";
pub(crate) const TAKE_SNAPSHOT: &str = "TAKE_SNAPSHOT";
pub(crate) const ARC: &str = "arc";
pub(crate) const LINE: &str = "line";
pub(crate) const TAN_ARC: &str = "tan_arc";
pub(crate) const TAN_ARC_TO: &str = "tan_arc_to";
pub(crate) const BEZIER: &str = "bezier";

fn err() -> MemoryError {
MemoryError::MemoryWrongSize
Expand Down Expand Up @@ -78,131 +69,3 @@ where
{
values.next().ok_or_else(err)?.ok_or_else(err)?.try_into()
}

/// Layout:
/// - One memory address to store the variant name
/// - Following memory addresses to store the variant's fields.
impl Value for PathSegment {
fn into_parts(self) -> Vec<Primitive> {
let name: String = match &self {
PathSegment::Line { .. } => LINE.to_owned(),
PathSegment::Arc { .. } => ARC.to_owned(),
PathSegment::Bezier { .. } => BEZIER.to_owned(),
PathSegment::TangentialArc { .. } => TAN_ARC.to_owned(),
PathSegment::TangentialArcTo { .. } => TAN_ARC_TO.to_owned(),
};
let name = Primitive::from(name);
let data = match self {
PathSegment::Line { end, relative } => {
let mut parts = end.into_parts();
parts.push(relative.into());
parts
}
PathSegment::Arc {
center,
radius,
start,
end,
relative,
} => {
let mut parts = center.into_parts();
parts.push(radius.into());
parts.push(start.into());
parts.push(end.into());
parts.push(relative.into());
parts
}
PathSegment::Bezier {
control1,
control2,
end,
relative,
} => {
let mut parts = control1.into_parts();
parts.extend(control2.into_parts());
parts.extend(end.into_parts());
parts.push(relative.into());
parts
}
PathSegment::TangentialArc { radius, offset } => {
vec![radius.into(), offset.into()]
}
PathSegment::TangentialArcTo {
to,
angle_snap_increment,
} => {
let mut parts = to.into_parts();
parts.push(match angle_snap_increment {
Some(angle) => angle.into(),
None => Primitive::Nil,
});
parts
}
};
let mut parts = Vec::with_capacity(1 + data.len());
parts.push(name);
parts.extend(data);
parts
}

fn from_parts<I>(values: &mut I) -> Result<Self, MemoryError>
where
I: Iterator<Item = Option<Primitive>>,
{
let variant_name: String = next(values)?;
match variant_name.as_str() {
LINE => {
let end = Point3d::from_parts(values)?;
let relative = next(values)?;
Ok(Self::Line { end, relative })
}
ARC => {
let center = Point2d::from_parts(values)?;
let radius = Primitive::from_parts(values)?.try_into()?;
let start = Primitive::from_parts(values)?.try_into()?;
let end = Primitive::from_parts(values)?.try_into()?;
let relative = Primitive::from_parts(values)?.try_into()?;
Ok(Self::Arc {
center,
radius,
start,
end,
relative,
})
}
BEZIER => {
let control1 = Point3d::from_parts(values)?;
let control2 = Point3d::from_parts(values)?;
let end = Point3d::from_parts(values)?;
let relative = Primitive::from_parts(values)?.try_into()?;
Ok(Self::Bezier {
control1,
control2,
end,
relative,
})
}
TAN_ARC => {
let radius = Primitive::from_parts(values).and_then(f64::try_from)?;
let offset = Primitive::from_parts(values).and_then(Angle::try_from)?;
Ok(Self::TangentialArc { radius, offset })
}
TAN_ARC_TO => {
let to = Point3d::from_parts(values)?;
let angle_snap_increment = if let Some(Some(primitive)) = values.next() {
Some(Angle::try_from(primitive)?)
} else {
None
};
Ok(Self::TangentialArcTo {
to,
angle_snap_increment,
})
}
other => Err(MemoryError::InvalidEnumVariant {
expected_type: "line segment".to_owned(),
actual: other.to_owned(),
}),
}
}
}
2 changes: 1 addition & 1 deletion modeling-cmds/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub enum CameraDragInteractionType {

/// A segment of a path.
/// Paths are composed of many segments.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema, ExecutionPlanValue)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum PathSegment {
/// A straight line segment.
Expand Down

0 comments on commit 1dcb69a

Please sign in to comment.