diman_unit_system 0.5.1

Internal procedural macros for diman.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::collections::{BTreeMap, HashMap, HashSet};

use proc_macro2::Ident;

use crate::{
    dimension_math::{BaseDimensions, DimensionsAndMagnitude},
    types::{
        base_dimension::BaseDimension,
        expression::{self, Expr},
    },
    types::{
        Constant, ConstantEntry, Definition, Dimension, DimensionEntry, Exponent, Factor, Unit,
        UnitEntry,
    },
};

use super::error::{
    Emit, KindNotAllowedError, MultipleDefinitionsError, UndefinedAnnotationDimensionError,
    UndefinedError, UnresolvableError, ViolatedAnnotationError, WrongTypeInAnnotationError,
};

/// The kind of an identifier
#[derive(Clone, PartialEq, Copy)]
pub enum Kind {
    Dimension,
    BaseUnit,
    Unit,
    Constant,
}

impl Kind {
    fn kind_is_allowed_in_definition(&self, kind: Kind) -> bool {
        match self {
            Kind::Dimension => kind == Kind::Dimension,
            Kind::Unit => kind == Kind::Unit || kind == Kind::BaseUnit || kind == Kind::Constant,
            Kind::BaseUnit => kind == Kind::Dimension,
            Kind::Constant => {
                kind == Kind::Constant || kind == Kind::Unit || kind == Kind::BaseUnit
            }
        }
    }
}

#[derive(Clone)]
pub struct Item {
    expr: Expr<Factor<DimensionsAndMagnitude>, Exponent>,
    type_: ItemType,
}

#[derive(Clone)]
pub enum ItemType {
    Dimension(DimensionEntry),
    Unit(UnitEntry),
    Constant(ConstantEntry),
}

impl Item {
    fn kind(&self) -> Kind {
        match &self.type_ {
            ItemType::Dimension(_) => Kind::Dimension,
            ItemType::Unit(entry) => match entry.definition {
                Definition::Base(_) => Kind::BaseUnit,
                Definition::Expression(_) => Kind::Unit,
            },
            ItemType::Constant(_) => Kind::Constant,
        }
    }

    fn ident(&self) -> &Ident {
        match &self.type_ {
            ItemType::Dimension(dim) => &dim.name,
            ItemType::Unit(unit) => &unit.name,
            ItemType::Constant(constant) => &constant.name,
        }
    }

    fn annotation(&self) -> Option<&Ident> {
        match &self.type_ {
            ItemType::Dimension(_) => None,
            ItemType::Unit(entry) => entry.dimension_annotation.as_ref(),
            ItemType::Constant(entry) => entry.dimension_annotation.as_ref(),
        }
    }
}

impl ItemType {
    fn unwrap_dimension(self) -> DimensionEntry {
        match self {
            Self::Dimension(entry) => entry,
            _ => panic!("unwrap_dimension called on non-dimension entry"),
        }
    }

    fn unwrap_unit(self) -> UnitEntry {
        match self {
            Self::Unit(entry) => entry,
            _ => panic!("unwrap_unit called on non-unit entry"),
        }
    }

    fn unwrap_constant(self) -> ConstantEntry {
        match self {
            Self::Constant(entry) => entry,
            _ => panic!("unwrap_constant called on non-constant entry"),
        }
    }
}

pub struct ResolvedItem {
    pub item: Item,
    pub dimensions: DimensionsAndMagnitude,
}

#[derive(Default)]
pub struct IdentStorage {
    unresolved: Vec<Item>,
    // We use BTreeMap here to make sure the generated types are deterministic
    resolved: BTreeMap<Ident, ResolvedItem>,
}

impl IdentStorage {
    fn is_resolvable(&self, item: &Item) -> bool {
        let all_factors_concrete_or_given = item.expr.iter_vals().all(|val| match val {
            Factor::Concrete(_) => true,
            Factor::Other(ident) => self.resolved.contains_key(ident),
        });
        all_factors_concrete_or_given
    }

    fn resolve_dimensions(&self, item: &Item) -> DimensionsAndMagnitude {
        item.expr
            .clone()
            .map(|val_or_expr| match val_or_expr {
                Factor::Concrete(factor) => factor,
                Factor::Other(ident) => self.resolved[&ident].dimensions.clone(),
            })
            .eval()
    }

    pub fn add<I: Into<Item>>(&mut self, items: Vec<I>) {
        self.unresolved.extend(items.into_iter().map(|t| t.into()));
    }

    pub fn resolve(&mut self) {
        // TODO(minor): This is a very inefficient topological sort.
        while !self.unresolved.is_empty() {
            let next_resolvable_index = self
                .unresolved
                .iter()
                .enumerate()
                .find(|(_, x)| self.is_resolvable(x))
                .map(|(i, _)| i);
            if let Some(index) = next_resolvable_index {
                let next_resolvable = self.unresolved.remove(index);
                let name = next_resolvable.ident().clone();
                let dimensions = self.resolve_dimensions(&next_resolvable);
                self.resolved.insert(
                    name,
                    ResolvedItem {
                        dimensions,
                        item: next_resolvable,
                    },
                );
            } else {
                UnresolvableError(
                    self.unresolved
                        .drain(..)
                        .map(|x| x.ident().clone())
                        .collect(),
                )
                .emit();
                return;
            }
        }
    }

    pub fn get_items<I: FromItem>(&self) -> Vec<I> {
        self.resolved
            .values()
            .filter(|resolved| I::is_correct_kind(resolved.item.kind()))
            .map(|resolved| {
                I::from_item_and_dimensions(resolved.item.clone(), resolved.dimensions.clone())
            })
            .collect()
    }

    fn unresolved_idents(&self) -> HashSet<Ident> {
        self.unresolved
            .iter()
            .map(|item| item.ident().clone())
            .collect()
    }

    /// Filter out all the autogenerated units that depend on a
    /// now invalid unit in order to prevent generating lots of
    /// error messages for each of the prefixed/aliased units.
    pub(crate) fn filter_autogenerated_invalid(&mut self) {
        // TODO(minor): This code clones quite a lot.
        let defined = self.unresolved_idents();
        (self.unresolved, _) = self.unresolved.drain(..).partition(|item| {
            let autogenerated_from = match &item.type_ {
                ItemType::Unit(unit) => unit.autogenerated_from.as_ref(),
                _ => None,
            };
            autogenerated_from
                .map(|ident| defined.contains(ident))
                .unwrap_or(true)
        })
    }

    pub(crate) fn filter_undefined(&mut self) {
        // TODO(minor): This code clones quite a lot.
        let defined_idents = self.unresolved_idents();
        let mut undefined_lhs = vec![];
        let (defined, undefined): (Vec<_>, Vec<_>) = self.unresolved.drain(..).partition(|item| {
            item.expr.iter_vals().all(|x| match x {
                Factor::Concrete(_) => true,
                Factor::Other(ident) => {
                    let defined = defined_idents.contains(ident);
                    if !defined {
                        undefined_lhs.push(ident.clone());
                    }
                    defined
                }
            })
        });
        self.unresolved = defined;
        if !undefined.is_empty() {
            UndefinedError(undefined_lhs).emit();
        }
    }

    pub(crate) fn filter_multiply_defined(&mut self) {
        let num_definitions: HashMap<_, usize> =
            self.unresolved
                .iter()
                .fold(HashMap::default(), |mut acc, item| {
                    *acc.entry(item.ident()).or_insert(0) += 1;
                    acc
                });
        let mut v: Vec<Vec<_>> = vec![];
        for (ident, count) in num_definitions {
            if count > 1 {
                v.push(
                    self.unresolved
                        .iter()
                        .filter(|item| item.ident() == ident)
                        .map(|x| x.ident().clone())
                        .collect(),
                );
            }
        }
        if !v.is_empty() {
            MultipleDefinitionsError(v).emit();
        }
    }

    pub(crate) fn check_type_annotations(&self) {
        for item in self.resolved.values() {
            if let Some(annotation_ident) = item.item.annotation() {
                match self.resolved.get(annotation_ident) {
                    Some(annotation) => {
                        if !matches!(annotation.item.kind(), Kind::Dimension) {
                            WrongTypeInAnnotationError {
                                annotation_ident,
                                annotation_kind: annotation.item.kind(),
                            }
                            .emit()
                        } else if annotation.dimensions.dimensions != item.dimensions.dimensions {
                            ViolatedAnnotationError {
                                annotation: annotation_ident,
                                annotation_dims: &annotation.dimensions.dimensions,
                                expr_dims: &item.dimensions.dimensions,
                            }
                            .emit()
                        }
                    }
                    None => UndefinedAnnotationDimensionError(annotation_ident).emit(),
                }
            }
        }
    }

    pub(crate) fn check_kinds_in_definitions(&self) {
        // TODO(minor): Having to collect into a HashMap here is annoying.
        let kinds: HashMap<_, _> = self
            .unresolved
            .iter()
            .map(|item| (item.ident(), item.kind()))
            .collect();
        for lhs in self.unresolved.iter() {
            let lhs_kind = lhs.kind();
            for rhs_factor in lhs.expr.iter_vals() {
                if let Factor::Other(rhs_ident) = rhs_factor {
                    let rhs_kind = kinds.get(rhs_ident);
                    // If we cannot not find the rhs_ident, it means that
                    // it was previously filtered out due to be undefined / multiply defined.
                    // In that case, we'll skip this check for this identifier.
                    if let Some(rhs_kind) = rhs_kind {
                        if !lhs_kind.kind_is_allowed_in_definition(*rhs_kind) {
                            KindNotAllowedError {
                                lhs_ident: lhs.ident(),
                                rhs_ident,
                                lhs_kind,
                                rhs_kind: *rhs_kind,
                            }
                            .emit();
                        }
                    }
                }
            }
        }
    }
}

impl From<DimensionEntry> for Item {
    fn from(entry: DimensionEntry) -> Self {
        let expr = match &entry.rhs {
            Definition::Expression(expr) => expr.clone().map(|f| {
                f.map_concrete(|_| DimensionsAndMagnitude::dimensions(BaseDimensions::none()))
            }),
            Definition::Base(()) => Expr::Value(expression::Factor::Value(Factor::Concrete(
                DimensionsAndMagnitude::dimensions(BaseDimensions::for_base_dimension(
                    BaseDimension::from_dimension(&entry.name),
                )),
            ))),
        };
        Item {
            type_: ItemType::Dimension(entry),
            expr,
        }
    }
}

impl From<UnitEntry> for Item {
    fn from(entry: UnitEntry) -> Self {
        let expr = match &entry.definition {
            Definition::Expression(rhs) => rhs
                .clone()
                .map(|f| f.map_concrete(DimensionsAndMagnitude::magnitude)),
            Definition::Base(dimension) => {
                Expr::Value(expression::Factor::Value(Factor::Other(dimension.clone())))
            }
        };
        Item {
            type_: ItemType::Unit(entry),
            expr,
        }
    }
}

impl From<ConstantEntry> for Item {
    fn from(entry: ConstantEntry) -> Self {
        let expr = entry
            .rhs
            .clone()
            .map(|f| f.map_concrete(DimensionsAndMagnitude::magnitude));
        Item {
            type_: ItemType::Constant(entry),
            expr,
        }
    }
}

pub trait FromItem {
    fn from_item_and_dimensions(item: Item, dimensions: DimensionsAndMagnitude) -> Self;
    fn is_correct_kind(kind: Kind) -> bool;
}

impl FromItem for Dimension {
    fn is_correct_kind(kind: Kind) -> bool {
        kind == Kind::Dimension
    }

    fn from_item_and_dimensions(item: Item, dimensions: DimensionsAndMagnitude) -> Self {
        let dimension_entry = item.type_.unwrap_dimension();
        Dimension {
            dimensions: dimensions.dimensions,
            name: dimension_entry.name,
        }
    }
}

impl FromItem for Unit {
    fn is_correct_kind(kind: Kind) -> bool {
        kind == Kind::Unit || kind == Kind::BaseUnit
    }

    fn from_item_and_dimensions(item: Item, dimensions: DimensionsAndMagnitude) -> Self {
        let unit_entry = item.type_.unwrap_unit();
        Unit {
            dimensions: dimensions.dimensions,
            name: unit_entry.name,
            magnitude: dimensions.magnitude,
            symbol: unit_entry.symbol,
            is_base_unit: matches!(unit_entry.definition, Definition::Base(_)),
        }
    }
}

impl FromItem for Constant {
    fn is_correct_kind(kind: Kind) -> bool {
        kind == Kind::Constant
    }

    fn from_item_and_dimensions(item: Item, dimensions: DimensionsAndMagnitude) -> Self {
        let constant_entry = item.type_.unwrap_constant();
        Constant {
            dimensions: dimensions.dimensions,
            name: constant_entry.name,
            magnitude: dimensions.magnitude,
        }
    }
}