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
53
54
55
56
57
58
59
60
61
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set::Set;
use erg_common::shared::Shared;

use crate::error::{CompileError, CompileErrors};

#[derive(Debug, Clone, Default)]
pub struct SharedCompileErrors(Shared<Set<CompileError>>);

impl SharedCompileErrors {
    pub fn new() -> Self {
        Self(Shared::new(Set::new()))
    }

    pub fn is_empty(&self) -> bool {
        self.0.borrow().is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.borrow().len()
    }

    pub fn push(&self, error: CompileError) {
        self.0.borrow_mut().insert(error);
    }

    pub fn extend(&self, errors: CompileErrors) {
        self.0.borrow_mut().extend(errors);
    }

    pub fn take(&self) -> CompileErrors {
        self.0.borrow_mut().take_all().to_vec().into()
    }

    pub fn clear(&self) {
        self.0.borrow_mut().clear();
    }

    pub fn remove(&self, path: &NormalizedPathBuf) {
        self.0
            .borrow_mut()
            .retain(|e| &NormalizedPathBuf::from(e.input.path()) != path);
    }

    pub fn get(&self, path: &NormalizedPathBuf) -> CompileErrors {
        self.0
            .borrow()
            .iter()
            .filter(|e| &NormalizedPathBuf::from(e.input.path()) == path)
            .cloned()
            .collect()
    }

    pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> {
        let _ref = self.0.borrow();
        let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
        ref_.iter()
    }
}

pub type SharedCompileWarnings = SharedCompileErrors;