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
use crate::{Position, Range};
use derive_builder::Builder;
use std::fmt::{self, Debug, Display};
use std::ops;
use std::path::PathBuf;

// TypedBuilder offers type safe builds at compile time.
// unfortunatly it's a consuming builder, I don't believe it's
// possible to make a non consuming typed-builder.
#[derive(Builder, Clone, Debug, Default)]
pub struct AnalysisLog {
    #[builder(setter(into, strip_option), default)]
    pub engine_id: Option<String>,
    #[builder(setter(into, strip_option), default)]
    pub file: Option<PathBuf>,
    #[builder(setter(into, strip_option), default)]
    pub level: Option<u16>,
    #[builder(setter(into))]
    pub message: String,
    #[builder(setter(into, strip_option), default)]
    pub position: Option<Position>,
    // FIXME: We seem to only need this for the end position, and I don't see
    // any usages for the byte indices either. We can probably trim this.
    #[builder(setter(into, strip_option), default)]
    pub range: Option<Range>,
    #[builder(setter(into, strip_option), default)]
    pub syntax_tree: Option<String>,
    #[builder(setter(into, strip_option), default)]
    pub source: Option<String>,
}

pub struct AnalysisLogs(Vec<AnalysisLog>);

impl AnalysisLogs {
    pub fn logs(self) -> Vec<AnalysisLog> {
        self.0
    }
}

impl fmt::Display for AnalysisLogs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.iter().try_fold((), |_, log| writeln!(f, "{}", log))
    }
}

impl From<Vec<AnalysisLog>> for AnalysisLogs {
    fn from(logs: Vec<AnalysisLog>) -> Self {
        Self(logs)
    }
}

impl ops::Deref for AnalysisLogs {
    type Target = Vec<AnalysisLog>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ops::DerefMut for AnalysisLogs {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Debug for AnalysisLogs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.iter().try_fold((), |_, log| writeln!(f, "{}", log))
    }
}

impl Display for AnalysisLog {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "engine_id: {:?}\nfile_name: {:?}\nlevel: {:?}\nmessage: {:?}\nposition: {:?}",
            self.engine_id, self.file, self.level, self.message, self.position
        )
    }
}