Skip to main content

kcl_api/
numeric_type.rs

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