EP instructions must be serializable (#1163)

We want to be able to extract the execution plan (EP) from a KCL program, copy it, and paste it into an engine unit test. Therefore they must be de/serializable.
This commit is contained in:
Adam Chalmers
2023-12-01 19:36:39 -06:00
committed by GitHub
parent 3b63632005
commit 173d50517c
3 changed files with 11 additions and 5 deletions

View File

@ -827,6 +827,7 @@ name = "execution-plan"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"kittycad",
"serde", "serde",
"thiserror", "thiserror",
] ]

View File

@ -8,5 +8,6 @@ description = "A DSL for composing KittyCAD API queries"
[dependencies] [dependencies]
bytes = "1.5" bytes = "1.5"
kittycad = { workspace = true, features = ["requests"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
thiserror = "1" thiserror = "1"

View File

@ -6,6 +6,7 @@
//! You can think of it as a domain-specific language for making KittyCAD API calls and using //! You can think of it as a domain-specific language for making KittyCAD API calls and using
//! the results to make other API calls. //! the results to make other API calls.
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt}; use std::{collections::HashMap, fmt};
#[cfg(test)] #[cfg(test)]
@ -16,7 +17,7 @@ mod tests;
pub struct Memory(HashMap<usize, Value>); pub struct Memory(HashMap<usize, Value>);
/// An address in KCEP's program memory. /// An address in KCEP's program memory.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Address(usize); pub struct Address(usize);
impl fmt::Display for Address { impl fmt::Display for Address {
@ -43,7 +44,7 @@ impl Memory {
} }
/// A value stored in KCEP program memory. /// A value stored in KCEP program memory.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Value { pub enum Value {
String(String), String(String),
NumericValue(NumericValue), NumericValue(NumericValue),
@ -63,18 +64,19 @@ impl From<usize> for Value {
} }
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum NumericValue { pub enum NumericValue {
Integer(usize), Integer(usize),
Float(f64), Float(f64),
} }
/// One step of the execution plan. /// One step of the execution plan.
#[derive(Serialize, Deserialize)]
pub enum Instruction { pub enum Instruction {
/// Call the KittyCAD API. /// Call the KittyCAD API.
ApiRequest { ApiRequest {
/// Which endpoint to call? /// Which endpoint to call?
endpoint: &'static str, endpoint: kittycad::types::ModelingCmd,
/// Which address should the response be stored in? /// Which address should the response be stored in?
store_response: Option<usize>, store_response: Option<usize>,
/// Look up each API request in this register number. /// Look up each API request in this register number.
@ -97,6 +99,7 @@ pub enum Instruction {
} }
/// Instruction to perform arithmetic on values in memory. /// Instruction to perform arithmetic on values in memory.
#[derive(Deserialize, Serialize)]
pub struct Arithmetic { pub struct Arithmetic {
/// Apply this operation /// Apply this operation
pub operation: Operation, pub operation: Operation,
@ -156,7 +159,7 @@ impl Arithmetic {
} }
/// Operations that can be applied to values in memory. /// Operations that can be applied to values in memory.
#[derive(Debug)] #[derive(Debug, Deserialize, Serialize)]
pub enum Operation { pub enum Operation {
Add, Add,
Mul, Mul,
@ -177,6 +180,7 @@ impl fmt::Display for Operation {
} }
/// Argument to an operation. /// Argument to an operation.
#[derive(Deserialize, Serialize)]
pub enum Operand { pub enum Operand {
Literal(Value), Literal(Value),
Reference(Address), Reference(Address),