erg_compiler/module/
errors.rs

1use erg_common::pathutil::NormalizedPathBuf;
2use erg_common::set::Set;
3use erg_common::shared::Shared;
4
5use crate::error::{CompileError, CompileErrors};
6
7#[derive(Debug, Clone, Default)]
8pub struct SharedCompileErrors(Shared<Set<CompileError>>);
9
10impl SharedCompileErrors {
11    pub fn new() -> Self {
12        Self(Shared::new(Set::new()))
13    }
14
15    pub fn is_empty(&self) -> bool {
16        self.0.borrow().is_empty()
17    }
18
19    pub fn len(&self) -> usize {
20        self.0.borrow().len()
21    }
22
23    pub fn push(&self, error: CompileError) {
24        self.0.borrow_mut().insert(error);
25    }
26
27    pub fn extend(&self, errors: CompileErrors) {
28        self.0.borrow_mut().extend(errors);
29    }
30
31    pub fn take(&self) -> CompileErrors {
32        self.0.borrow_mut().take_all().to_vec().into()
33    }
34
35    pub fn clear(&self) {
36        self.0.borrow_mut().clear();
37    }
38
39    pub fn remove(&self, path: &NormalizedPathBuf) {
40        self.0
41            .borrow_mut()
42            .retain(|e| &NormalizedPathBuf::from(e.input.path()) != path);
43    }
44
45    pub fn get(&self, path: &NormalizedPathBuf) -> CompileErrors {
46        self.0
47            .borrow()
48            .iter()
49            .filter(|e| &NormalizedPathBuf::from(e.input.path()) == path)
50            .cloned()
51            .collect()
52    }
53
54    pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> {
55        let _ref = self.0.borrow();
56        let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
57        ref_.iter()
58    }
59}
60
61pub type SharedCompileWarnings = SharedCompileErrors;