Skip to main content

kcl_api/
numeric_type.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5use crate::UnitAngle;
6use crate::UnitLength;
7
8#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS, JsonSchema)]
9#[ts(export)]
10#[serde(tag = "type")]
11pub enum UnitType {
12    Count,
13    Length(UnitLength),
14    GenericLength,
15    Angle(UnitAngle),
16    GenericAngle,
17}
18
19impl UnitType {
20    pub fn to_suffix(self) -> Option<String> {
21        match self {
22            UnitType::Count => Some("_".to_owned()),
23            UnitType::GenericLength | UnitType::GenericAngle => None,
24            UnitType::Length(l) => Some(l.to_string()),
25            UnitType::Angle(a) => Some(a.to_string()),
26        }
27    }
28
29    pub fn degrees() -> Self {
30        Self::Angle(UnitAngle::Degrees)
31    }
32
33    pub fn radians() -> Self {
34        Self::Angle(UnitAngle::Radians)
35    }
36}
37
38impl std::fmt::Display for UnitType {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            UnitType::Count => write!(f, "Count"),
42            UnitType::Length(l) => l.fmt(f),
43            UnitType::GenericLength => write!(f, "Length"),
44            UnitType::Angle(a) => a.fmt(f),
45            UnitType::GenericAngle => write!(f, "Angle"),
46        }
47    }
48}
49
50#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, ts_rs::TS, JsonSchema)]
51#[ts(export)]
52#[serde(tag = "type")]
53pub enum NumericType {
54    // Specified by the user (directly or indirectly)
55    Known(UnitType),
56    // Unspecified, using defaults
57    Default { len: UnitLength, angle: UnitAngle },
58    // Exceeded the ability of the type system to track.
59    Unknown,
60    // Type info has been explicitly cast away.
61    Any,
62}
63
64impl Default for NumericType {
65    fn default() -> Self {
66        NumericType::Default {
67            len: UnitLength::Millimeters,
68            angle: UnitAngle::Degrees,
69        }
70    }
71}