kittycad_modeling_cmds/
units.rs

1use kittycad_unit_conversion_derive::UnitConversion;
2use parse_display_derive::{Display, FromStr};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[cfg(feature = "cxx")]
7use crate::impl_extern_type;
8
9/// The valid types of length units.
10#[derive(
11    Default,
12    Display,
13    FromStr,
14    Copy,
15    Eq,
16    PartialEq,
17    Debug,
18    JsonSchema,
19    Deserialize,
20    Serialize,
21    Clone,
22    Ord,
23    PartialOrd,
24    UnitConversion,
25    Hash,
26)]
27#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
28#[display(style = "snake_case")]
29#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
30#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
31#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
32pub enum UnitLength {
33    /// Centimeters <https://en.wikipedia.org/wiki/Centimeter>
34    #[serde(rename = "cm")]
35    #[display("cm")]
36    Centimeters,
37    /// Feet <https://en.wikipedia.org/wiki/Foot_(unit)>
38    #[serde(rename = "ft")]
39    #[display("ft")]
40    Feet,
41    /// Inches <https://en.wikipedia.org/wiki/Inch>
42    #[serde(rename = "in")]
43    #[display("in")]
44    Inches,
45    /// Meters <https://en.wikipedia.org/wiki/Meter>
46    #[default]
47    #[serde(rename = "m")]
48    #[display("m")]
49    Meters,
50    /// Millimeters <https://en.wikipedia.org/wiki/Millimeter>
51    #[serde(rename = "mm")]
52    #[display("mm")]
53    Millimeters,
54    /// Yards <https://en.wikipedia.org/wiki/Yard>
55    #[serde(rename = "yd")]
56    #[display("yd")]
57    Yards,
58}
59
60impl UnitLength {
61    /// Convert to measurement.
62    pub fn as_measurement(self, value: f64) -> measurements::Length {
63        match self {
64            Self::Centimeters => measurements::Length::from_centimeters(value),
65            Self::Feet => measurements::Length::from_feet(value),
66            Self::Inches => measurements::Length::from_inches(value),
67            Self::Meters => measurements::Length::from_meters(value),
68            Self::Millimeters => measurements::Length::from_millimeters(value),
69            Self::Yards => measurements::Length::from_yards(value),
70        }
71    }
72}
73
74#[cfg(feature = "cxx")]
75impl_extern_type! {
76    [Trivial]
77    UnitLength = "Enums::_UnitLength"
78}
79
80/// The valid types of angle formats.
81#[derive(
82    Default,
83    Display,
84    FromStr,
85    Copy,
86    Eq,
87    PartialEq,
88    Debug,
89    JsonSchema,
90    Deserialize,
91    Serialize,
92    Clone,
93    Ord,
94    PartialOrd,
95    UnitConversion,
96    Hash,
97)]
98#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
99#[serde(rename_all = "snake_case")]
100#[display(style = "snake_case")]
101#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
102#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
103#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
104pub enum UnitAngle {
105    /// Degrees <https://en.wikipedia.org/wiki/Degree_(angle)>
106    #[default]
107    #[display("deg")]
108    Degrees,
109    /// Radians <https://en.wikipedia.org/wiki/Radian>
110    #[display("rad")]
111    Radians,
112}
113
114/// The valid types of area units.
115#[derive(
116    Display,
117    FromStr,
118    Copy,
119    Eq,
120    PartialEq,
121    Debug,
122    JsonSchema,
123    Deserialize,
124    Serialize,
125    Clone,
126    Ord,
127    PartialOrd,
128    UnitConversion,
129    Default,
130    Hash,
131)]
132#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
133#[serde(rename_all = "snake_case")]
134#[display(style = "snake_case")]
135#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
136#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
137#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
138pub enum UnitArea {
139    /// Square centimeters <https://en.wikipedia.org/wiki/Square_centimeter>
140    #[serde(rename = "cm2")]
141    #[display("cm2")]
142    SquareCentimeters,
143    /// Square decimeters <https://en.wikipedia.org/wiki/Square_decimeter>
144    #[serde(rename = "dm2")]
145    #[display("dm2")]
146    SquareDecimeters,
147    /// Square feet <https://en.wikipedia.org/wiki/Square_foot>
148    #[serde(rename = "ft2")]
149    #[display("ft2")]
150    SquareFeet,
151    /// Square inches <https://en.wikipedia.org/wiki/Square_inch>
152    #[serde(rename = "in2")]
153    #[display("in2")]
154    SquareInches,
155    /// Square kilometers <https://en.wikipedia.org/wiki/Square_kilometer>
156    #[serde(rename = "km2")]
157    #[display("km2")]
158    SquareKilometers,
159    /// Square meters <https://en.wikipedia.org/wiki/Square_meter>
160    #[default]
161    #[serde(rename = "m2")]
162    #[display("m2")]
163    SquareMeters,
164    /// Square millimeters <https://en.wikipedia.org/wiki/Square_millimeter>
165    #[serde(rename = "mm2")]
166    #[display("mm2")]
167    SquareMillimeters,
168    /// Square yards <https://en.wikipedia.org/wiki/Square_mile>
169    #[serde(rename = "yd2")]
170    #[display("yd2")]
171    SquareYards,
172}
173
174impl UnitArea {
175    /// Convert to measurement.
176    pub fn as_measurement(self, value: f64) -> measurements::Area {
177        match self {
178            Self::SquareCentimeters => measurements::Area::from_square_centimeters(value),
179            Self::SquareDecimeters => measurements::Area::from_square_decimeters(value),
180            Self::SquareFeet => measurements::Area::from_square_feet(value),
181            Self::SquareInches => measurements::Area::from_square_inches(value),
182            Self::SquareKilometers => measurements::Area::from_square_kilometers(value),
183            Self::SquareMeters => measurements::Area::from_square_meters(value),
184            Self::SquareMillimeters => measurements::Area::from_square_millimeters(value),
185            Self::SquareYards => measurements::Area::from_square_yards(value),
186        }
187    }
188}
189
190/// The valid types for density units.
191#[derive(
192    Display,
193    FromStr,
194    Copy,
195    Eq,
196    PartialEq,
197    Debug,
198    JsonSchema,
199    Deserialize,
200    Serialize,
201    Clone,
202    Ord,
203    PartialOrd,
204    Default,
205    UnitConversion,
206    Hash,
207)]
208#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
209#[display(style = "snake_case")]
210#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
211#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
212#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
213pub enum UnitDensity {
214    /// Pounds per cubic feet.
215    #[serde(rename = "lb:ft3")]
216    #[display("lb:ft3")]
217    PoundsPerCubicFeet,
218
219    /// Kilograms per cubic meter.
220    #[default]
221    #[serde(rename = "kg:m3")]
222    #[display("kg:m3")]
223    KilogramsPerCubicMeter,
224}
225
226impl UnitDensity {
227    /// Convert to measurement.
228    pub fn as_measurement(self, value: f64) -> measurements::Density {
229        match self {
230            Self::PoundsPerCubicFeet => measurements::Density::from_pounds_per_cubic_feet(value),
231            Self::KilogramsPerCubicMeter => measurements::Density::from_kilograms_per_cubic_meter(value),
232        }
233    }
234}
235
236/// The valid types of mass units.
237#[derive(
238    Default,
239    Display,
240    FromStr,
241    Copy,
242    Eq,
243    PartialEq,
244    Debug,
245    JsonSchema,
246    Deserialize,
247    Serialize,
248    Clone,
249    Ord,
250    PartialOrd,
251    UnitConversion,
252    Hash,
253)]
254#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
255#[serde(rename_all = "snake_case")]
256#[display(style = "snake_case")]
257#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
258#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
259#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
260pub enum UnitMass {
261    /// Grams <https://en.wikipedia.org/wiki/Gram>
262    #[default]
263    #[serde(rename = "g")]
264    #[display("g")]
265    Grams,
266    /// Kilograms <https://en.wikipedia.org/wiki/Kilogram>
267    #[serde(rename = "kg")]
268    #[display("kg")]
269    Kilograms,
270    /// Pounds <https://en.wikipedia.org/wiki/Pound_(mass)>
271    #[serde(rename = "lb")]
272    #[display("lb")]
273    Pounds,
274}
275
276impl UnitMass {
277    /// Convert to measurement.
278    pub fn as_measurement(self, value: f64) -> measurements::Mass {
279        match self {
280            Self::Grams => measurements::Mass::from_grams(value),
281            Self::Kilograms => measurements::Mass::from_kilograms(value),
282            Self::Pounds => measurements::Mass::from_pounds(value),
283        }
284    }
285}
286
287/// The valid types of volume units.
288#[derive(
289    Default,
290    Display,
291    FromStr,
292    Copy,
293    Eq,
294    PartialEq,
295    Debug,
296    JsonSchema,
297    Deserialize,
298    Serialize,
299    Clone,
300    Ord,
301    PartialOrd,
302    UnitConversion,
303    Hash,
304)]
305#[cfg_attr(feature = "tabled", derive(tabled::Tabled))]
306#[display(style = "snake_case")]
307#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
308#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
309#[cfg_attr(feature = "python", pyo3::pyclass, pyo3_stub_gen::derive::gen_stub_pyclass_enum)]
310pub enum UnitVolume {
311    /// Cubic centimeters (cc or cm³) <https://en.wikipedia.org/wiki/Cubic_centimeter>
312    #[serde(rename = "cm3")]
313    #[display("cm3")]
314    CubicCentimeters,
315    /// Cubic feet (ft³) <https://en.wikipedia.org/wiki/Cubic_foot>
316    #[serde(rename = "ft3")]
317    #[display("ft3")]
318    CubicFeet,
319    /// Cubic inches (cu in or in³) <https://en.wikipedia.org/wiki/Cubic_inch>
320    #[serde(rename = "in3")]
321    #[display("in3")]
322    CubicInches,
323    /// Cubic meters (m³) <https://en.wikipedia.org/wiki/Cubic_meter>
324    #[default]
325    #[serde(rename = "m3")]
326    #[display("m3")]
327    CubicMeters,
328    /// Cubic yards (yd³) <https://en.wikipedia.org/wiki/Cubic_yard>
329    #[serde(rename = "yd3")]
330    #[display("yd3")]
331    CubicYards,
332    /// US Fluid Ounces (fl oz) <https://en.wikipedia.org/wiki/Fluid_ounce>
333    #[serde(rename = "usfloz")]
334    #[display("usfloz")]
335    FluidOunces,
336    /// US Gallons (gal US) <https://en.wikipedia.org/wiki/Gallon>
337    #[serde(rename = "usgal")]
338    #[display("usgal")]
339    Gallons,
340    /// Liters (l) <https://en.wikipedia.org/wiki/Litre>
341    #[serde(rename = "l")]
342    #[display("l")]
343    Liters,
344    /// Milliliters (ml) <https://en.wikipedia.org/wiki/Litre>
345    #[serde(rename = "ml")]
346    #[display("ml")]
347    Milliliters,
348}
349
350impl UnitVolume {
351    /// Convert to measurement.
352    pub fn as_measurement(self, value: f64) -> measurements::Volume {
353        match self {
354            Self::CubicCentimeters => measurements::Volume::from_cubic_centimeters(value),
355            Self::CubicFeet => measurements::Volume::from_cubic_feet(value),
356            Self::CubicInches => measurements::Volume::from_cubic_inches(value),
357            Self::CubicMeters => measurements::Volume::from_cubic_meters(value),
358            Self::CubicYards => measurements::Volume::from_cubic_yards(value),
359            Self::FluidOunces => measurements::Volume::from_fluid_ounces(value),
360            Self::Gallons => measurements::Volume::from_gallons(value),
361            Self::Liters => measurements::Volume::from_liters(value),
362            Self::Milliliters => measurements::Volume::from_milliliters(value),
363        }
364    }
365}