nimbus_fml/schema/
types.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2* License, v. 2.0. If a copy of the MPL was not distributed with this
3* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use std::collections::{BTreeMap, HashSet};
6
7use crate::intermediate_representation::{FeatureDef, ObjectDef, TypeFinder, TypeRef};
8
9pub(crate) struct TypeQuery<'a> {
10    object_defs: &'a BTreeMap<String, ObjectDef>,
11}
12
13impl<'a> TypeQuery<'a> {
14    pub(crate) fn new(objs: &'a BTreeMap<String, ObjectDef>) -> Self {
15        Self { object_defs: objs }
16    }
17
18    pub(crate) fn all_types(&self, feature_def: &FeatureDef) -> HashSet<TypeRef> {
19        let mut types = Default::default();
20        self.gather_types(&feature_def.all_types(), &mut types);
21        types
22    }
23
24    fn gather_types(&self, unseen: &HashSet<TypeRef>, seen: &mut HashSet<TypeRef>) {
25        for t in unseen {
26            if !seen.contains(t) {
27                seen.insert(t.clone());
28                if let TypeRef::Object(nm) = t {
29                    let def = self.object_defs.get(nm).unwrap();
30                    self.gather_types(&def.all_types(), seen);
31                }
32            }
33        }
34    }
35}