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 c9e0a5a
Show file tree
Hide file tree
Showing 17 changed files with 290 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.

20 changes: 20 additions & 0 deletions execution-plan-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "kittycad-execution-plan-macros"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/KittyCAD/execution-plan"
rust-version = "1.73"
description = "A DSL for composing KittyCAD API queries"

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

[dependencies]
proc-macro2 = "1.0.70"
quote = "1.0.33"
syn = "2.0.41"

[lints]
workspace = true

[lib]
proc-macro = true
9 changes: 9 additions & 0 deletions execution-plan-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Proc-macros for implementing execution-plan traits.
use proc_macro::TokenStream;

/// Test.
#[proc_macro]
pub fn impl_value_trait(_input: TokenStream) -> TokenStream {
todo!()
}
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 c9e0a5a

Please sign in to comment.