From b7b68dbfc263e258ce69438965eb983c347f01ef Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Sat, 16 Dec 2023 01:11:01 -0600 Subject: [PATCH] Better names and comments --- execution-plan/src/api_endpoint.rs | 38 +++++++++++++++++------------- execution-plan/src/lib.rs | 12 +++++----- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/execution-plan/src/api_endpoint.rs b/execution-plan/src/api_endpoint.rs index 3f931d92..8e6c7d52 100644 --- a/execution-plan/src/api_endpoint.rs +++ b/execution-plan/src/api_endpoint.rs @@ -7,14 +7,20 @@ use uuid::Uuid; use crate::{primitive::Primitive, value::Value, Address, ExecutionError, Memory, Result}; +/// All API endpoints that can be executed must implement this trait. pub trait ApiEndpoint: ModelingCmdVariant + Sized { - fn from_values(values: &mut I, mem: &Memory) -> Result + /// Read the API call and its parameters from memory. + /// For each field in the API endpoint's body, + /// 1. Read that field's address from the `fields` iterator. + /// 2. Look up the value at that address + /// Then use those fields to reconstruct the entire struct. + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator; } impl ApiEndpoint for StartPath { - fn from_values(_values: &mut I, _mem: &Memory) -> Result + fn from_memory(_fields: &mut I, _mem: &Memory) -> Result where I: Iterator, { @@ -23,50 +29,50 @@ impl ApiEndpoint for StartPath { } impl ApiEndpoint for MovePathPen { - fn from_values(values: &mut I, mem: &Memory) -> Result + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator, { - let path: Uuid = read::(values.next(), mem)?.try_into()?; + let path: Uuid = read::(fields.next(), mem)?.try_into()?; let path = ModelingCmdId::from(path); - let to = read(values.next(), mem)?; + let to = read(fields.next(), mem)?; Ok(Self { path, to }) } } impl ApiEndpoint for ExtendPath { - fn from_values(values: &mut I, mem: &Memory) -> Result + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator, { - let path = read::(values.next(), mem) + let path = read::(fields.next(), mem) .and_then(Uuid::try_from) .map(ModelingCmdId::from)?; - let segment = read(values.next(), mem)?; + let segment = read(fields.next(), mem)?; Ok(Self { path, segment }) } } impl ApiEndpoint for Extrude { - fn from_values(values: &mut I, mem: &Memory) -> Result + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator, { - let target = read::(values.next(), mem) + let target = read::(fields.next(), mem) .and_then(Uuid::try_from) .map(ModelingCmdId)?; - let distance = read::(values.next(), mem).and_then(f64::try_from)?; - let cap = read::(values.next(), mem).and_then(bool::try_from)?; + let distance = read::(fields.next(), mem).and_then(f64::try_from)?; + let cap = read::(fields.next(), mem).and_then(bool::try_from)?; Ok(Self { target, distance, cap }) } } impl ApiEndpoint for TakeSnapshot { - fn from_values(values: &mut I, mem: &Memory) -> Result + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator, { - let format_str = read::(values.next(), mem).and_then(String::try_from)?; + let format_str = read::(fields.next(), mem).and_then(String::try_from)?; let format = format_str.parse().map_err(|_| ExecutionError::InvalidEnumVariant { expected_type: "image format".to_owned(), actual: format_str, @@ -76,11 +82,11 @@ impl ApiEndpoint for TakeSnapshot { } impl ApiEndpoint for ClosePath { - fn from_values(values: &mut I, mem: &Memory) -> Result + fn from_memory(fields: &mut I, mem: &Memory) -> Result where I: Iterator, { - let path_id = read::(values.next(), mem).and_then(Uuid::try_from)?; + let path_id = read::(fields.next(), mem).and_then(Uuid::try_from)?; Ok(Self { path_id }) } } diff --git a/execution-plan/src/lib.rs b/execution-plan/src/lib.rs index ccb1b106..4a5a9b45 100644 --- a/execution-plan/src/lib.rs +++ b/execution-plan/src/lib.rs @@ -149,27 +149,27 @@ impl ApiRequest { let mut arguments = arguments.into_iter(); let output = match endpoint { Endpoint::StartPath => { - let cmd = each_cmd::StartPath::from_values(&mut arguments, mem)?; + let cmd = each_cmd::StartPath::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } Endpoint::MovePathPen => { - let cmd = each_cmd::MovePathPen::from_values(&mut arguments, mem)?; + let cmd = each_cmd::MovePathPen::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } Endpoint::ExtendPath => { - let cmd = each_cmd::ExtendPath::from_values(&mut arguments, mem)?; + let cmd = each_cmd::ExtendPath::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } Endpoint::ClosePath => { - let cmd = each_cmd::ClosePath::from_values(&mut arguments, mem)?; + let cmd = each_cmd::ClosePath::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } Endpoint::Extrude => { - let cmd = each_cmd::Extrude::from_values(&mut arguments, mem)?; + let cmd = each_cmd::Extrude::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } Endpoint::TakeSnapshot => { - let cmd = each_cmd::TakeSnapshot::from_values(&mut arguments, mem)?; + let cmd = each_cmd::TakeSnapshot::from_memory(&mut arguments, mem)?; session.run_command(cmd_id, cmd).await? } };