Skip to content

Use DeserializeOwned instead of Deserialize for ModelingCmdVariant #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
- In `def_enum.rs` add a new variant of `ModelingCmd` with your type, e.g. `MyNewCommand(MyNewCommand)`.
- If your command responds with data:
- In `output.rs`, add a `struct MyNewCommand` following the existing examples.
- Then scroll to the end of the file and `impl<'de> ModelingCmdOutput<'de> for MyNewCommand {}`
- Then scroll to the end of the file and `impl ModelingCmdOutput for MyNewCommand {}`
- In `ok_response.rs` add your new type to the `build_enum!` macro.
- In `impl_traits.rs` follow the examples to implement `ModelingCmdVariant<'de>` for your type `MyNewCommand` using either the `impl_variant_output!` or the `impl_variant_empty!` macro. If your command responds with data, use the former. If your command has no response, use the latter.
- In `impl_traits.rs` follow the examples to implement `ModelingCmdVariant` for your type `MyNewCommand` using either the `impl_variant_output!` or the `impl_variant_empty!` macro. If your command responds with data, use the former. If your command has no response, use the latter.
4 changes: 2 additions & 2 deletions modeling-cmds/src/impl_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{each_cmd::*, output as out, ModelingCmd, ModelingCmdVariant};

macro_rules! impl_variant_output {
($struct:ident) => {
impl<'de> ModelingCmdVariant<'de> for $struct {
impl ModelingCmdVariant for $struct {
type Output = out::$struct;
fn into_enum(self) -> ModelingCmd {
ModelingCmd::$struct(self)
Expand All @@ -16,7 +16,7 @@ macro_rules! impl_variant_output {

macro_rules! impl_variant_empty {
($struct:ident) => {
impl<'de> ModelingCmdVariant<'de> for $struct {
impl ModelingCmdVariant for $struct {
type Output = ();
fn into_enum(self) -> ModelingCmd {
ModelingCmd::$struct(self)
Expand Down
64 changes: 32 additions & 32 deletions modeling-cmds/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,35 +256,35 @@ pub struct EntityGetDistance {
pub max_distance: f64,
}

impl<'de> ModelingCmdOutput<'de> for Export {}
impl<'de> ModelingCmdOutput<'de> for SelectWithPoint {}
impl<'de> ModelingCmdOutput<'de> for HighlightSetEntity {}
impl<'de> ModelingCmdOutput<'de> for EntityGetChildUuid {}
impl<'de> ModelingCmdOutput<'de> for EntityGetNumChildren {}
impl<'de> ModelingCmdOutput<'de> for EntityGetParentId {}
impl<'de> ModelingCmdOutput<'de> for EntityGetAllChildUuids {}
impl<'de> ModelingCmdOutput<'de> for EntityGetDistance {}
impl<'de> ModelingCmdOutput<'de> for SelectGet {}
impl<'de> ModelingCmdOutput<'de> for GetEntityType {}
impl<'de> ModelingCmdOutput<'de> for Solid3dGetAllEdgeFaces {}
impl<'de> ModelingCmdOutput<'de> for Solid3dGetAllOppositeEdges {}
impl<'de> ModelingCmdOutput<'de> for Solid3dGetOppositeEdge {}
impl<'de> ModelingCmdOutput<'de> for Solid3dGetPrevAdjacentEdge {}
impl<'de> ModelingCmdOutput<'de> for Solid3dGetNextAdjacentEdge {}
impl<'de> ModelingCmdOutput<'de> for MouseClick {}
impl<'de> ModelingCmdOutput<'de> for CurveGetType {}
impl<'de> ModelingCmdOutput<'de> for CurveGetControlPoints {}
impl<'de> ModelingCmdOutput<'de> for TakeSnapshot {}
impl<'de> ModelingCmdOutput<'de> for PathGetInfo {}
impl<'de> ModelingCmdOutput<'de> for PathGetCurveUuidsForVertices {}
impl<'de> ModelingCmdOutput<'de> for PathGetVertexUuids {}
impl<'de> ModelingCmdOutput<'de> for PlaneIntersectAndProject {}
impl<'de> ModelingCmdOutput<'de> for CurveGetEndPoints {}
impl<'de> ModelingCmdOutput<'de> for ImportFiles {}
impl<'de> ModelingCmdOutput<'de> for Mass {}
impl<'de> ModelingCmdOutput<'de> for Volume {}
impl<'de> ModelingCmdOutput<'de> for Density {}
impl<'de> ModelingCmdOutput<'de> for SurfaceArea {}
impl<'de> ModelingCmdOutput<'de> for CenterOfMass {}
impl<'de> ModelingCmdOutput<'de> for GetSketchModePlane {}
impl<'de> ModelingCmdOutput<'de> for () {}
impl ModelingCmdOutput for Export {}
impl ModelingCmdOutput for SelectWithPoint {}
impl ModelingCmdOutput for HighlightSetEntity {}
impl ModelingCmdOutput for EntityGetChildUuid {}
impl ModelingCmdOutput for EntityGetNumChildren {}
impl ModelingCmdOutput for EntityGetParentId {}
impl ModelingCmdOutput for EntityGetAllChildUuids {}
impl ModelingCmdOutput for EntityGetDistance {}
impl ModelingCmdOutput for SelectGet {}
impl ModelingCmdOutput for GetEntityType {}
impl ModelingCmdOutput for Solid3dGetAllEdgeFaces {}
impl ModelingCmdOutput for Solid3dGetAllOppositeEdges {}
impl ModelingCmdOutput for Solid3dGetOppositeEdge {}
impl ModelingCmdOutput for Solid3dGetPrevAdjacentEdge {}
impl ModelingCmdOutput for Solid3dGetNextAdjacentEdge {}
impl ModelingCmdOutput for MouseClick {}
impl ModelingCmdOutput for CurveGetType {}
impl ModelingCmdOutput for CurveGetControlPoints {}
impl ModelingCmdOutput for TakeSnapshot {}
impl ModelingCmdOutput for PathGetInfo {}
impl ModelingCmdOutput for PathGetCurveUuidsForVertices {}
impl ModelingCmdOutput for PathGetVertexUuids {}
impl ModelingCmdOutput for PlaneIntersectAndProject {}
impl ModelingCmdOutput for CurveGetEndPoints {}
impl ModelingCmdOutput for ImportFiles {}
impl ModelingCmdOutput for Mass {}
impl ModelingCmdOutput for Volume {}
impl ModelingCmdOutput for Density {}
impl ModelingCmdOutput for SurfaceArea {}
impl ModelingCmdOutput for CenterOfMass {}
impl ModelingCmdOutput for GetSketchModePlane {}
impl ModelingCmdOutput for () {}
13 changes: 7 additions & 6 deletions modeling-cmds/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use serde::Serialize;
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Serialize};

use crate::ModelingCmd;

/// Some modeling command executed on the KittyCAD engine.
pub trait ModelingCmdVariant<'de>: Serialize {
pub trait ModelingCmdVariant: Serialize {
/// What the command responds with
type Output: ModelingCmdOutput<'de>;
type Output: ModelingCmdOutput;
/// Take this specific enum variant, and create the general enum.
fn into_enum(self) -> ModelingCmd;
/// The command's name.
fn name() -> &'static str;
}

/// Anything that can be a ModelingCmd output.
pub trait ModelingCmdOutput<'de>: std::fmt::Debug + Serialize + serde::Deserialize<'de> + schemars::JsonSchema {}
pub trait ModelingCmdOutput: std::fmt::Debug + Serialize + DeserializeOwned + JsonSchema {}

impl<'de, CmdVariant> From<CmdVariant> for ModelingCmd
impl<CmdVariant> From<CmdVariant> for ModelingCmd
where
CmdVariant: ModelingCmdVariant<'de>,
CmdVariant: ModelingCmdVariant,
{
fn from(value: CmdVariant) -> Self {
value.into_enum()
Expand Down
2 changes: 1 addition & 1 deletion modeling-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Session {
cmd: Cmd,
) -> Result<OkModelingCmdResponse, RunCommandError>
where
Cmd: kittycad_modeling_cmds::ModelingCmdVariant<'de>,
Cmd: kittycad_modeling_cmds::ModelingCmdVariant,
{
// All messages to the KittyCAD Modeling API will be sent over the WebSocket as Text.
// The text will contain JSON representing a `ModelingCmdReq`.
Expand Down