Skip to content

Commit

Permalink
Impl Value and conversion with primitive for more types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Dec 19, 2023
1 parent 96394a8 commit 9bf6745
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 15 additions & 1 deletion execution-plan/src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use kittycad_modeling_cmds::shared::Angle;
use kittycad_modeling_cmds::{base64::Base64Data, shared::Angle};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand Down Expand Up @@ -58,6 +58,12 @@ impl From<Vec<u8>> for Primitive {
}
}

impl From<Base64Data> for Primitive {
fn from(value: Base64Data) -> Self {
Self::Bytes(value.into())
}
}

impl TryFrom<Primitive> for String {
type Error = ExecutionError;

Expand Down Expand Up @@ -142,6 +148,14 @@ impl TryFrom<Primitive> for Vec<u8> {
}
}

impl TryFrom<Primitive> for Base64Data {
type Error = ExecutionError;

fn try_from(value: Primitive) -> Result<Self, Self::Error> {
Vec::<u8>::try_from(value).map(Base64Data::from)
}
}

impl TryFrom<Primitive> for bool {
type Error = ExecutionError;

Expand Down
19 changes: 18 additions & 1 deletion execution-plan/src/value/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//! - This canonical ordering is the order of the struct's fields in its Rust source code definition.
//! - Enums get laid out by first putting the variant as a string, then putting the variant's fields.
use kittycad_modeling_cmds::{
base64::Base64Data,
ok_response::OkModelingCmdResponse,
output,
shared::{Angle, PathSegment, Point2d, Point3d},
shared::{Angle, PathSegment, Point2d, Point3d, Point4d},
};
use uuid::Uuid;

Expand All @@ -26,6 +27,9 @@ fn err() -> ExecutionError {
ExecutionError::MemoryWrongSize
}

/// Macro to generate an `impl Value` for the given type `$subject`.
/// The type `$subject` must be "primitive-ish",
/// i.e. something that can be converted Into a Primitive and TryFrom a primitive
macro_rules! impl_value_on_primitive_ish {
($subject:ident) => {
impl Value for $subject {
Expand All @@ -52,6 +56,7 @@ type VecU8 = Vec<u8>;
impl_value_on_primitive_ish!(VecU8);
impl_value_on_primitive_ish!(Angle);
impl_value_on_primitive_ish!(usize);
impl_value_on_primitive_ish!(Base64Data);

/// Macro to generate the methods of trait `Value` for the given fields.
/// Args:
Expand Down Expand Up @@ -100,10 +105,22 @@ where
impl_value_on_struct_fields!(x, y, z);
}

impl<T> Value for Point4d<T>
where
Primitive: From<T>,
T: Value,
{
impl_value_on_struct_fields!(x, y, z, w);
}

impl Value for kittycad_modeling_cmds::shared::Color {
impl_value_on_struct_fields!(r, g, b, a);
}

impl Value for kittycad_modeling_cmds::shared::ExportFile {
impl_value_on_struct_fields!(name, contents);
}

/// Layout:
/// - One memory address to store the variant name
/// - Following memory addresses to store the variant's single field.
Expand Down

0 comments on commit 9bf6745

Please sign in to comment.