microcad_lang_base/diag/
mod.rs1mod diag_error;
14mod diag_handler;
15mod diag_list;
16mod diagnostic;
17mod level;
18
19pub use diag_error::*;
20pub use diag_handler::*;
21pub use diag_list::*;
22pub use diagnostic::*;
23pub use level::*;
24use miette::Report;
25
26use crate::src_ref::*;
27
28pub trait PushDiag {
30 fn push_diag(&mut self, diag: Diagnostic) -> DiagResult<()>;
32
33 fn trace(&mut self, src: &impl SrcReferrer, message: String) {
35 self.push_diag(Diagnostic::Trace(Refer::new(
36 Report::msg(message),
37 src.src_ref(),
38 )))
39 .expect("could not push diagnostic trace message");
40 }
41 fn info(&mut self, src: &impl SrcReferrer, message: String) {
43 self.push_diag(Diagnostic::Info(Refer::new(
44 Report::msg(message),
45 src.src_ref(),
46 )))
47 .expect("could not push diagnostic info message");
48 }
49 fn warning(&mut self, src: &impl SrcReferrer, err: impl Into<Report>) -> DiagResult<()> {
51 let err = Diagnostic::Warning(Refer::new(err.into(), src.src_ref()));
52 if cfg!(feature = "ansi-color") {
53 log::warn!("{}", color_print::cformat!("<y,s>{err}</>"));
54 } else {
55 log::warn!("{err}");
56 }
57 self.push_diag(err)
58 }
59 fn error(&mut self, src: &impl SrcReferrer, err: impl Into<Report>) -> DiagResult<()> {
61 let err = Diagnostic::Error(Refer::new(err.into(), src.src_ref()));
62 if cfg!(feature = "ansi-color") {
63 log::error!("{}", color_print::cformat!("<r,s>{err}</>"));
64 } else {
65 log::error!("{err}");
66 }
67 self.push_diag(err)
68 }
69}
70
71pub trait Diag {
73 fn fmt_diagnosis(&self, f: &mut dyn std::fmt::Write) -> std::fmt::Result;
75
76 fn write_diagnosis(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
78 write!(w, "{}", self.diagnosis())
79 }
80
81 fn diagnosis(&self) -> String {
83 let mut str = String::new();
84 self.fmt_diagnosis(&mut str).expect("displayable diagnosis");
85 str
86 }
87
88 fn has_warnings(&self) -> bool {
90 self.warning_count() > 0
91 }
92
93 fn warning_count(&self) -> u32;
95
96 fn has_errors(&self) -> bool {
98 self.error_count() > 0
99 }
100
101 fn error_count(&self) -> u32;
103
104 fn error_lines(&self) -> std::collections::HashSet<usize>;
106
107 fn warning_lines(&self) -> std::collections::HashSet<usize>;
109}