Skip to content

Commit f21dae5

Browse files
authored
Merge pull request #139 from Rustfahrtagentur/pat/units
Add density to Units
2 parents e34debe + 832d268 commit f21dae5

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

lang/eval/call/call_method.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl CallMethod for Value {
6868
Value::Volume(_) => todo!(),
6969
Value::Angle(_) => todo!(),
7070
Value::Weight(_) => todo!(),
71+
Value::Density(_) => todo!(),
7172
Value::Bool(_) => todo!(),
7273
Value::String(_) => todo!(),
7374
Value::Color(_) => todo!(),

lang/syntax/literal/units.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ pub enum Unit {
9292
Milliliter,
9393
/// Microliter
9494
Microliter,
95+
96+
/// Density
97+
KilogramPerMeter2,
9598
}
9699

97100
impl std::fmt::Display for Unit {
@@ -145,6 +148,9 @@ impl std::fmt::Display for Unit {
145148
Self::Centiliter => write!(f, "cl"),
146149
Self::Liter => write!(f, "l"),
147150
Self::Microliter => write!(f, "µl"),
151+
152+
// Density
153+
Self::KilogramPerMeter2 => write!(f, "kg/m2"),
148154
}
149155
}
150156
}
@@ -181,6 +187,7 @@ impl Unit {
181187
| Self::Centiliter
182188
| Self::Milliliter
183189
| Self::Microliter => Type::Volume,
190+
Self::KilogramPerMeter2 => Type::Density,
184191
}
185192
}
186193
/// Normalize value to mm, rad or gram
@@ -232,6 +239,9 @@ impl Unit {
232239
Self::Centiliter => x * 10_000.0_f64,
233240
Self::Milliliter => x * 1_000.0_f64,
234241
Self::Microliter => x * 1_000_000.0_f64,
242+
243+
// Densities
244+
Self::KilogramPerMeter2 => 1_f64,
235245
}
236246
}
237247
}

lang/ty/type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub enum Type {
2828
Angle,
2929
/// A physical weight, e.g. 4.0kg
3030
Weight,
31+
/// A physical density, e.g. 1.0kg/m³
32+
Density,
3133
/// A boolean: true, false
3234
Bool,
3335
/// A list of elements of the same type: `[scalar]`
@@ -82,6 +84,7 @@ impl std::fmt::Display for Type {
8284
Self::Volume => write!(f, "Volume"),
8385
Self::Angle => write!(f, "Angle"),
8486
Self::Weight => write!(f, "Weight"),
87+
Self::Density => write!(f, "Density"),
8588
Self::Bool => write!(f, "Bool"),
8689
Self::List(t) => write!(f, "{}", t),
8790
Self::Map(t) => write!(f, "{}", t),

lang/value/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub enum Value {
4949
Area(Scalar),
5050
/// Volume in mm³.
5151
Volume(Scalar),
52+
/// Density in g/mm³
53+
Density(Scalar),
5254
/// An angle in radians.
5355
Angle(Scalar),
5456
/// Weight of a specific volume of material.
@@ -222,6 +224,7 @@ impl crate::ty::Ty for Value {
222224
Value::Volume(_) => Type::Volume,
223225
Value::Angle(_) => Type::Angle,
224226
Value::Weight(_) => Type::Weight,
227+
Value::Density(_) => Type::Density,
225228
Value::Bool(_) => Type::Bool,
226229
Value::String(_) => Type::String,
227230
Value::Color(_) => Type::Color,
@@ -418,7 +421,8 @@ impl std::fmt::Display for Value {
418421
| Value::Angle(n)
419422
| Value::Area(n)
420423
| Value::Volume(n)
421-
| Value::Weight(n) => {
424+
| Value::Weight(n)
425+
| Value::Density(n) => {
422426
write!(f, "{n}{}", self.ty().default_unit())
423427
}
424428
Value::Bool(b) => write!(f, "{b}"),
@@ -444,6 +448,7 @@ impl std::fmt::Debug for Value {
444448
Self::Volume(arg0) => write!(f, "Volume: {arg0}"),
445449
Self::Angle(arg0) => write!(f, "Angle: {arg0}"),
446450
Self::Weight(arg0) => write!(f, "Weight: {arg0}"),
451+
Self::Density(arg0) => write!(f, "Density: {arg0}"),
447452
Self::Bool(arg0) => write!(f, "Bool: {arg0}"),
448453
Self::String(arg0) => write!(f, "String: {arg0}"),
449454
Self::Color(arg0) => write!(f, "Color: {arg0}"),

0 commit comments

Comments
 (0)