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