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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fmt;

use erg_common::config::ErgConfig;
use erg_common::traits::{Runnable, Stream};

use crate::context::ModuleContext;
use crate::error::CompileErrors;
use crate::hir::HIR;
use crate::module::SharedCompilerResource;

#[derive(Debug)]
pub struct CompleteArtifact<Inner = HIR> {
    pub object: Inner,
    pub warns: CompileErrors,
}

impl<Inner> CompleteArtifact<Inner> {
    pub const fn new(object: Inner, warns: CompileErrors) -> Self {
        Self { object, warns }
    }
}

#[derive(Debug)]
pub struct IncompleteArtifact<Inner = HIR> {
    pub object: Option<Inner>,
    pub errors: CompileErrors,
    pub warns: CompileErrors,
}

impl fmt::Display for IncompleteArtifact {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.warns.is_empty() {
            writeln!(f, "{}", self.warns)?;
        }
        write!(f, "{}", self.errors)
    }
}

impl std::error::Error for IncompleteArtifact {}

impl From<CompleteArtifact> for IncompleteArtifact {
    fn from(artifact: CompleteArtifact) -> Self {
        Self {
            object: Some(artifact.object),
            errors: CompileErrors::empty(),
            warns: artifact.warns,
        }
    }
}

impl<Inner> IncompleteArtifact<Inner> {
    pub const fn new(object: Option<Inner>, errors: CompileErrors, warns: CompileErrors) -> Self {
        Self {
            object,
            errors,
            warns,
        }
    }
}

#[derive(Debug)]
pub struct ErrorArtifact {
    pub errors: CompileErrors,
    pub warns: CompileErrors,
}

impl fmt::Display for ErrorArtifact {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.warns.is_empty() {
            writeln!(f, "{}", self.warns)?;
        }
        write!(f, "{}", self.errors)
    }
}

impl std::error::Error for ErrorArtifact {}

impl From<IncompleteArtifact> for ErrorArtifact {
    fn from(artifact: IncompleteArtifact) -> Self {
        Self {
            errors: artifact.errors,
            warns: artifact.warns,
        }
    }
}

impl ErrorArtifact {
    pub const fn new(errors: CompileErrors, warns: CompileErrors) -> Self {
        Self { errors, warns }
    }
}

pub trait Buildable<T = HIR> {
    fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self
    where
        Self: Sized;
    fn build(
        &mut self,
        src: String,
        mode: &str,
    ) -> Result<CompleteArtifact<T>, IncompleteArtifact<T>>;
    fn pop_context(&mut self) -> Option<ModuleContext>;
    fn get_context(&self) -> Option<&ModuleContext>;
}

pub trait BuildRunnable<T = HIR>: Buildable<T> + Runnable {}