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
use std::collections::BTreeSet;

/// The set of relevant namespaces and types
pub struct TypeLimits {
    reader: &'static winmd::TypeReader,
    pub inner: BTreeSet<NamespaceTypes>,
}

impl TypeLimits {
    pub fn new(reader: &'static winmd::TypeReader) -> Self {
        Self {
            reader,
            inner: BTreeSet::new(),
        }
    }

    /// Insert a namespace into the set of relevant namespaces
    ///
    /// expects the namespace in the form: `parent::namespace::*`s
    pub fn insert(&mut self, mut limit: NamespaceTypes) -> Result<(), &'static str> {
        if let Some(namespace) = self
            .reader
            .find_lowercase_namespace(&limit.namespace.to_lowercase())
        {
            limit.namespace = namespace;
            self.inner.insert(limit);
            Ok(())
        } else {
            Err(limit.namespace)
        }
    }

    pub fn limits(&self) -> impl Iterator<Item = &NamespaceTypes> {
        self.inner.iter()
    }
}

/// A namespace's relevant types
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct NamespaceTypes {
    pub namespace: &'static str,
    pub limit: TypeLimit,
}

/// A limit on the types in a namespace.
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum TypeLimit {
    /// All the types in a namespace
    All,
    /// Some types in the namespace
    Some(Vec<String>),
}