Skip to main content

crisp_errors/
result.rs

1use crisp_ast::Span;
2use std::collections::BTreeSet;
3
4#[derive(Debug, Clone, Default, PartialEq, Eq)]
5pub struct ErrorSet {
6    names: BTreeSet<String>,
7}
8
9impl ErrorSet {
10    pub fn new() -> Self {
11        Self::default()
12    }
13
14    pub fn insert(&mut self, name: impl Into<String>) {
15        self.names.insert(name.into());
16    }
17
18    pub fn extend(&mut self, other: &ErrorSet) {
19        self.names.extend(other.names.iter().cloned());
20    }
21
22    pub fn remove(&mut self, name: &str) {
23        self.names.remove(name);
24    }
25
26    pub fn is_empty(&self) -> bool {
27        self.names.is_empty()
28    }
29
30    pub fn iter(&self) -> impl Iterator<Item = &String> {
31        self.names.iter()
32    }
33
34    pub fn contains(&self, name: &str) -> bool {
35        self.names.contains(name)
36    }
37
38    pub fn union(a: &ErrorSet, b: &ErrorSet) -> ErrorSet {
39        let mut out = a.clone();
40        out.extend(b);
41        out
42    }
43
44    pub fn subtract(base: &ErrorSet, handled: &ErrorSet) -> ErrorSet {
45        let mut out = base.clone();
46        for name in handled.iter() {
47            out.names.remove(name);
48        }
49        out
50    }
51}
52
53impl FromIterator<String> for ErrorSet {
54    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
55        let mut s = ErrorSet::new();
56        for x in iter {
57            s.insert(x);
58        }
59        s
60    }
61}
62
63#[derive(Debug, Clone)]
64pub struct ErrorSig {
65    pub module: String,
66    pub name: String,
67    pub fallible: bool,
68    pub errors: ErrorSet,
69    pub declared: Option<ErrorSet>,
70    pub asserts_never: bool,
71    pub span: Span,
72}
73
74#[derive(Debug, Clone)]
75pub struct CrispErrorVariant {
76    pub name: String,
77    pub payload_type: String,
78}
79
80#[derive(Debug, Clone, Default)]
81pub struct CrispErrorEnum {
82    pub variants: Vec<CrispErrorVariant>,
83}
84
85#[derive(Debug, Clone, Default)]
86pub struct ErrorResult {
87    pub signatures: std::collections::BTreeMap<String, ErrorSig>,
88    pub crisp_error: CrispErrorEnum,
89}
90
91impl ErrorResult {
92    pub fn get(&self, module: &str, name: &str) -> Option<&ErrorSig> {
93        self.signatures.get(&format!("{module}::{name}"))
94    }
95}