Skip to content

Commit

Permalink
Move execution-plan types Value and Primitive into a new crate
Browse files Browse the repository at this point in the history
This way, modeling-cmds can import those types and implement the traits. In the future, I will write a derive macro for deriving Value on modeling-cmd types.
  • Loading branch information
adamchalmers committed Dec 20, 2023
1 parent b34a5db commit 0199c84
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 210 deletions.
63 changes: 37 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"execution-plan",
"execution-plan-traits",
"modeling-cmds",
"modeling-session",
"unit-conversion-derive",
Expand Down
14 changes: 14 additions & 0 deletions execution-plan-traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "kittycad-execution-plan-traits"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.193", features = ["derive"] }
thiserror = "1.0.51"
uuid = "1.6.1"

[lints]
workspace = true
70 changes: 70 additions & 0 deletions execution-plan-traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! TODO
// pub use impl_value_on_primitive_ish;

pub use self::primitive::{NumericPrimitive, Primitive};

#[macro_use]
mod primitive;

/// Types that can be written to or read from KCEP program memory.
/// If they require multiple memory addresses, they will be laid out
/// into multiple consecutive memory addresses.
pub trait Value: Sized {
/// Store the value in memory.
fn into_parts(self) -> Vec<Primitive>;
/// Read the value from memory.
fn from_parts<I>(values: &mut I) -> Result<Self, MemoryError>
where
I: Iterator<Item = Option<Primitive>>;
}

/// TODO
#[derive(Debug, thiserror::Error, Default)]
pub enum MemoryError {
/// Something went wrong
#[error("Something went wrong")]
#[default]
MemoryWrongSize,
/// Type error, memory contained the wrong type.
#[error("Tried to read a '{expected}' from KCEP program memory, found an '{actual}' instead")]
MemoryWrongType {
/// What the KittyCAD executor expected memory to contain
expected: &'static str,
/// What was actually in memory
actual: String,
},
/// When trying to read an enum from memory, found a variant tag which is not valid for this enum.
#[error("Found an unexpected tag '{actual}' when trying to read an enum of type {expected_type} from memory")]
InvalidEnumVariant {
/// What type of enum was being read from memory.
expected_type: String,
/// The actual enum tag found in memory.
actual: String,
},
}

/// 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_export]
macro_rules! impl_value_on_primitive_ish {
($trait:ident, $subject:ident) => {
impl $trait for $subject {
fn into_parts(self) -> Vec<Primitive> {
vec![self.into()]
}

fn from_parts<I>(values: &mut I) -> Result<Self, MemoryError>
where
I: Iterator<Item = Option<Primitive>>,
{
values
.next()
.ok_or(MemoryError::MemoryWrongSize)?
.to_owned()
.ok_or(MemoryError::MemoryWrongSize)?
.try_into()
}
}
};
}
Loading

0 comments on commit 0199c84

Please sign in to comment.