diff --git a/Cargo.lock b/Cargo.lock index 66dd6b8c..a462ed03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + [[package]] name = "cc" version = "1.0.83" @@ -122,6 +128,35 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "cxx" +version = "1.0.110" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7129e341034ecb940c9072817cd9007974ea696844fc4dd582dc1653a7fbe2e8" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.110" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06fdd177fc61050d63f67f5bd6351fac6ab5526694ea8e359cd9cd3b75857f44" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.110" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "587663dd5fb3d10932c8aecfe7c844db1bcf0aee93eeab08fac13dc1212c2e7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + [[package]] name = "data-encoding" version = "2.5.0" @@ -213,6 +248,12 @@ dependencies = [ "cgmath", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -228,6 +269,17 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +[[package]] +name = "http" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -287,12 +339,14 @@ name = "kittycad-modeling-cmds" version = "0.1.0" dependencies = [ "chrono", + "cxx", "data-encoding", "diesel", "diesel_derives", "enum-iterator", "enum-iterator-derive", "euler", + "http", "kittycad-unit-conversion-derive", "measurements", "parse-display", @@ -326,6 +380,15 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "link-cplusplus" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9" +dependencies = [ + "cc", +] + [[package]] name = "lock_api" version = "0.4.11" diff --git a/modeling-cmds/Cargo.toml b/modeling-cmds/Cargo.toml index a99eb50c..2661d32b 100644 --- a/modeling-cmds/Cargo.toml +++ b/modeling-cmds/Cargo.toml @@ -12,12 +12,14 @@ license = "MIT" [dependencies] chrono = "0.4.31" +cxx = "1.0" data-encoding = "2.5.0" diesel = { version = "2.1.1", features = ["serde_json", "mysql", "chrono", "r2d2", "uuid", "numeric"] } diesel_derives = "2.1.2" enum-iterator = "1.4.1" enum-iterator-derive = "1.2.1" euler = "0.4.1" +http = "0.2.9" kittycad-unit-conversion-derive = { path = "../unit-conversion-derive" } measurements = "0.11.0" parse-display = "0.8.2" diff --git a/modeling-cmds/src/impl_extern_type.rs b/modeling-cmds/src/impl_extern_type.rs new file mode 100644 index 00000000..05ea6885 --- /dev/null +++ b/modeling-cmds/src/impl_extern_type.rs @@ -0,0 +1,18 @@ +//! A little macro for easy implementation of cxx::ExternType + +/// This tells the c++ interop what the native c++ names are +/// from https://docs.rs/cxx/latest/src/cxx/extern_type.rs.html +#[macro_export] +macro_rules! impl_extern_type { + ($([$kind:ident] $($(#[$($attr:tt)*])* $ty:path = $cxxpath:literal)*)*) => { + $($( + $(#[$($attr)*])* + unsafe impl cxx::ExternType for $ty { + #[allow(unused_attributes)] // incorrect lint; this doc(hidden) attr *is* respected by rustdoc + #[doc(hidden)] + type Id = cxx::type_id!($cxxpath); + type Kind = cxx::kind::$kind; + } + )*)* + }; +} diff --git a/modeling-cmds/src/lib.rs b/modeling-cmds/src/lib.rs index 6e299894..52ae0736 100644 --- a/modeling-cmds/src/lib.rs +++ b/modeling-cmds/src/lib.rs @@ -12,6 +12,7 @@ pub mod format; /// Modeling command IDs, used to associated requests and responses. /// Also used to construct commands which refer to previous commands. pub mod id; +pub mod impl_extern_type; mod impl_traits; /// When a modeling command is successful, these responses could be returned. pub mod ok_response; diff --git a/modeling-cmds/src/shared.rs b/modeling-cmds/src/shared.rs index ea4b4f6a..09004d9f 100644 --- a/modeling-cmds/src/shared.rs +++ b/modeling-cmds/src/shared.rs @@ -7,7 +7,7 @@ use parse_display_derive::{Display, FromStr}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use crate::units::UnitAngle; +use crate::{impl_extern_type, units::UnitAngle}; // A helper macro for allowing enums of only strings to be saved to the database. macro_rules! impl_string_enum_sql { @@ -700,4 +700,53 @@ pub enum FileImportFormat { Stl, } +/// The type of error sent by the KittyCAD graphics engine. +/// A subset of [`ErrorCode`]. +#[derive(Display, FromStr, Copy, Eq, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone, Ord, PartialOrd)] +#[serde(rename_all = "snake_case")] +pub enum EngineErrorCode { + /// User requested something geometrically or graphically impossible. + /// Don't retry this request, as it's inherently impossible. Instead, read the error message + /// and change your request. + BadRequest = 1, + /// Graphics engine failed to complete request, consider retrying + InternalEngine, +} + +impl From for http::StatusCode { + fn from(e: EngineErrorCode) -> Self { + match e { + EngineErrorCode::BadRequest => Self::BAD_REQUEST, + EngineErrorCode::InternalEngine => Self::INTERNAL_SERVER_ERROR, + } + } +} + impl_string_enum_sql! {FileImportFormat} + +// Enum: Connect Rust Enums to Cpp +// add our native c++ names for our cxx::ExternType implementation +impl_extern_type! { + [Trivial] + // File + FileImportFormat = "Enums::_FileImportFormat" + FileExportFormat = "Enums::_FileExportFormat" + // Camera + CameraDragInteractionType = "Enums::_CameraDragInteractionType" + // Scene + SceneSelectionType = "Enums::_SceneSelectionType" + SceneToolType = "Enums::_SceneToolType" + EntityType = "Enums::_EntityType" + AnnotationType = "Enums::_AnnotationType" + AnnotationTextAlignmentX = "Enums::_AnnotationTextAlignmentX" + AnnotationTextAlignmentY = "Enums::_AnnotationTextAlignmentY" + AnnotationLineEnd = "Enums::_AnnotationLineEnd" + + CurveType = "Enums::_CurveType" + PathCommand = "Enums::_PathCommand" + PathComponentConstraintBound = "Enums::_PathComponentConstraintBound" + PathComponentConstraintType = "Enums::_PathComponentConstraintType" + + // Utils + EngineErrorCode = "Enums::_ErrorCode" +}