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
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#"
(plane, center, radius) => {
  const sg = startSketchOn(plane)
    |> startProfileAt([center[0] + radius, center[1]], %)
    |> arc({
       angle_end: 360,
       angle_start: 0,
       radius: radius
     }, %)
    |> 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 {
        "unstable_stdlib_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> {
        Vec::new() // TODO
    }

    fn return_value(&self) -> Option<crate::docs::StdLibFnArg> {
        None
    }

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

    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())
    }
}