-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move execution-plan types Value and Primitive into a new crate
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
1 parent
b34a5db
commit 0199c84
Showing
16 changed files
with
262 additions
and
210 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.