1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::kcl_stdlib::KclStdLibFn;
use crate::{
    ast::types::{FunctionExpression, Program},
    docs::StdLibFn,
};

pub const CIRCLE_FN: &str = r#"
(center, radius, surface, tag?) => {
const sg = startProfileAt([center[0] + radius, center[1]], surface)
    |> arc({
       angle_end: 360,
       angle_start: 0,
       radius: radius,
       tag: tag
     }, %)
    |> close(%)
  return sg
}
    "#;

#[derive(Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
pub struct Circle {
    function: FunctionExpression,
    program: Program,
}

impl Default for Circle {
    fn default() -> Self {
        // TODO in https://github.com/KittyCAD/modeling-app/issues/1018
        // Don't unwrap here, parse it at compile-time.
        let (src, function) = super::kcl_stdlib::extract_function(CIRCLE_FN).unwrap();
        Self {
            function: *function,
            program: src,
        }
    }
}

impl std::fmt::Debug for Circle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        "circle".fmt(f)
    }
}

/// TODO: Parse the KCL in a macro and generate these
impl StdLibFn for Circle {
    fn name(&self) -> String {
        "circle".to_owned()
    }

    fn summary(&self) -> String {
        "Sketch a circle on the given plane".to_owned()
    }

    fn description(&self) -> String {
        String::new()
    }

    fn tags(&self) -> Vec<String> {
        Vec::new()
    }

    fn args(&self) -> Vec<crate::docs::StdLibFnArg> {
        let mut settings = schemars::gen::SchemaSettings::openapi3();
        settings.inline_subschemas = true;
        let mut generator = schemars::gen::SchemaGenerator::new(settings);
        let mut args = Vec::new();
        for parameter in &self.function.params {
            match parameter.identifier.name.as_str() {
                "center" => {
                    args.push(crate::docs::StdLibFnArg {
                        name: parameter.identifier.name.to_owned(),
                        type_: "[number, number]".to_string(),
                        schema: <[f64; 2]>::json_schema(&mut generator),
                        required: true,
                    });
                }
                "radius" => {
                    args.push(crate::docs::StdLibFnArg {
                        name: parameter.identifier.name.to_owned(),
                        type_: "number".to_string(),
                        schema: <f64>::json_schema(&mut generator),
                        required: true,
                    });
                }
                "surface" => {
                    args.push(crate::docs::StdLibFnArg {
                        name: parameter.identifier.name.to_owned(),
                        type_: "SketchSurface".to_string(),
                        schema: <crate::executor::SketchSurface>::json_schema(&mut generator),
                        required: true,
                    });
                }
                "tag" => {
                    args.push(crate::docs::StdLibFnArg {
                        name: parameter.identifier.name.to_owned(),
                        type_: "String".to_string(),
                        schema: <String>::json_schema(&mut generator),
                        required: false,
                    });
                }
                _ => panic!("Unknown parameter: {:?}", parameter.identifier.name),
            }
        }
        args
    }

    fn return_value(&self) -> Option<crate::docs::StdLibFnArg> {
        let mut settings = schemars::gen::SchemaSettings::openapi3();
        settings.inline_subschemas = true;
        let mut generator = schemars::gen::SchemaGenerator::new(settings);
        Some(crate::docs::StdLibFnArg {
            name: "SketchGroup".to_owned(),
            type_: "SketchGroup".to_string(),
            schema: <crate::executor::SketchGroup>::json_schema(&mut generator),
            required: true,
        })
    }

    fn unpublished(&self) -> bool {
        false
    }

    fn deprecated(&self) -> bool {
        false
    }

    fn std_lib_fn(&self) -> crate::std::StdFn {
        todo!()
    }

    fn clone_box(&self) -> Box<dyn StdLibFn> {
        Box::new(self.to_owned())
    }
}

impl KclStdLibFn for Circle {
    fn function(&self) -> &FunctionExpression {
        &self.function
    }
    fn program(&self) -> &Program {
        &self.program
    }

    fn kcl_clone_box(&self) -> Box<dyn KclStdLibFn> {
        Box::new(self.clone())
    }
}