Skip to main content

kittycad_modeling_cmds/
exec_kcl.rs

1use bon::Builder;
2use kcl_error::{CompilationIssue, KclError};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::shared::safe_filepath::SafeFilepath;
7
8/// A KCL project that can be executed.
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default, Builder)]
10#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
11#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
12#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
13#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
14pub struct KclProject {
15    /// All files in the project.
16    pub files: Vec<KclFile>,
17    /// Which file is the entrypoint?
18    /// This is the first KCL file to be executed,
19    /// the root of the KCL module tree.
20    pub entrypoint: SafeFilepath,
21}
22
23/// Region-creation algorithm version.
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default, Builder)]
25#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
26#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
27#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
28#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
29pub struct KclFile {
30    /// Where is the file, relative to the project directory?
31    pub path: SafeFilepath,
32    /// Contents of the file, as UTF-8 encoded bytes.
33    #[serde(
34        serialize_with = "serde_bytes::serialize",
35        deserialize_with = "serde_bytes::deserialize"
36    )]
37    pub contents: Vec<u8>,
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Builder)]
41#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
42#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
43#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
44/// Successful KCL project execution response.
45pub struct ExecKclProjectOk {
46    // TODO: Add fields to this as we make KCL data serializable.
47    // Should be a usable subset of `SceneGraphDelta`.
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Builder)]
51#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
52#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
53#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
54/// Failed KCL project execution response.
55pub struct ExecKclProjectErr {
56    /// Fatal KCL errors that prevented your geometry from being created.
57    pub error: Option<KclError>,
58    /// Nonfatal KCL errors that need to be fixed.
59    pub non_fatal: Vec<CompilationIssue>,
60    // TODO: Add fields to this as we make KCL data serializable.
61    // Should be a usable subset of `KclErrorWithOutputs`.
62}
63
64#[cfg(feature = "arbitrary")]
65impl<'a> arbitrary::Arbitrary<'a> for ExecKclProjectOk {
66    fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
67        Ok(Self {})
68    }
69}
70
71#[cfg(feature = "arbitrary")]
72impl<'a> arbitrary::Arbitrary<'a> for ExecKclProjectErr {
73    fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
74        Ok(Self {
75            error: Default::default(),
76            non_fatal: Default::default(),
77        })
78    }
79}