Skip to main content

hax_frontend_exporter_options/
lib.rs

1use hax_adt_into::derive_group;
2use schemars::JsonSchema;
3
4#[derive_group(Serializers)]
5#[derive(Debug, Clone, JsonSchema, Hash, Eq, PartialEq)]
6pub enum Glob {
7    One,  // *
8    Many, // **
9}
10
11impl ToString for Glob {
12    fn to_string(&self) -> String {
13        match self {
14            Self::One => "*",
15            Self::Many => "**",
16        }
17        .to_string()
18    }
19}
20
21#[derive_group(Serializers)]
22#[derive(Debug, Clone, JsonSchema, Hash, Eq, PartialEq)]
23pub enum NamespaceChunk {
24    Glob(Glob),
25    Exact(String),
26}
27
28impl ToString for NamespaceChunk {
29    fn to_string(&self) -> String {
30        match self {
31            Self::Glob(glob) => glob.to_string(),
32            Self::Exact(string) => string.to_string(),
33        }
34    }
35}
36
37impl std::convert::From<&str> for NamespaceChunk {
38    fn from(s: &str) -> Self {
39        match s {
40            "*" => NamespaceChunk::Glob(Glob::One),
41            "**" => NamespaceChunk::Glob(Glob::Many),
42            _ => NamespaceChunk::Exact(String::from(s)),
43        }
44    }
45}
46
47#[derive_group(Serializers)]
48#[derive(Debug, Clone, JsonSchema, Hash, Eq, PartialEq)]
49pub struct Namespace {
50    pub chunks: Vec<NamespaceChunk>,
51}
52
53impl ToString for Namespace {
54    fn to_string(&self) -> String {
55        self.chunks
56            .iter()
57            .map(NamespaceChunk::to_string)
58            .collect::<Vec<_>>()
59            .join("::")
60            .to_string()
61    }
62}
63
64impl std::convert::From<String> for Namespace {
65    fn from(s: String) -> Self {
66        Namespace {
67            chunks: s
68                .split("::")
69                .filter(|s| !s.is_empty())
70                .map(NamespaceChunk::from)
71                .collect(),
72        }
73    }
74}
75
76impl Namespace {
77    pub fn matches(&self, path: &Vec<String>) -> bool {
78        let path: Vec<Option<&str>> = path.iter().map(|s| Some(s.as_str())).collect();
79        self.matches_segments(&path)
80    }
81
82    /// Whether the namespace matches `path`, whose `None` segments (e.g. `impl`
83    /// blocks) only a glob matches.
84    pub fn matches_segments(&self, path: &[Option<&str>]) -> bool {
85        fn aux(pattern: &[NamespaceChunk], path: &[Option<&str>]) -> bool {
86            match (pattern, path) {
87                ([], []) => true,
88                ([NamespaceChunk::Exact(x), pattern @ ..], [Some(y), path @ ..]) => {
89                    x == y && aux(pattern, path)
90                }
91                ([NamespaceChunk::Glob(Glob::One), pattern @ ..], [_, path @ ..]) => {
92                    aux(pattern, path)
93                }
94                ([NamespaceChunk::Glob(Glob::Many), pattern @ ..], []) => aux(pattern, path),
95                ([NamespaceChunk::Glob(Glob::Many), pattern_tl @ ..], [_path_hd, path_tl @ ..]) => {
96                    aux(pattern_tl, path) || aux(pattern, path_tl)
97                }
98                _ => false,
99            }
100        }
101        aux(self.chunks.as_slice(), path)
102    }
103}
104
105#[derive(Debug, Clone)]
106pub struct Options {
107    /// Whether we should evaluate and inline the value of anonymous constants (inline `const {}`
108    /// blocks or advanced constant expressions as in `[T; N+1]`), or refer to them as
109    /// `GlobalName`s.
110    pub inline_anon_consts: bool,
111    /// Options related to bounds.
112    pub bounds_options: BoundsOptions,
113    /// Resolve definition identifiers to their concrete impl counterpart when possible in `ItemRef::translate`.
114    pub item_ref_use_concrete_impl: bool,
115}
116
117#[derive(Debug, Clone, Copy)]
118pub struct BoundsOptions {
119    /// Add `T: Destruct` bounds to every type generic, so that we can build `ImplExpr`s to know
120    /// what code is run on drop.
121    pub resolve_destruct: bool,
122    /// Prune `T: Sized` and `T: MetaSized` predicates.
123    pub prune_sized: bool,
124}