Skip to main content

macho_core/model/validate/
diagnostics.rs

1use crate::model::addr::ThinFileOffset;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4/// The Severity type.
5#[non_exhaustive]
6pub enum Severity {
7    /// The Error variant.
8    Error,
9    /// The Warning variant.
10    Warning,
11    /// The Info variant.
12    Info,
13}
14
15#[derive(Debug, Clone)]
16/// The DiagnosticCode type.
17pub struct DiagnosticCode(pub &'static str);
18
19#[derive(Debug, Clone)]
20/// The Span type.
21pub struct Span {
22    /// The offset field.
23    pub offset: ThinFileOffset,
24    /// The size field.
25    pub size: u64,
26    /// The label field.
27    pub label: Option<String>,
28}
29
30#[derive(Debug, Clone)]
31/// The Diagnostic type.
32pub struct Diagnostic {
33    /// The severity field.
34    pub severity: Severity,
35    /// The code field.
36    pub code: DiagnosticCode,
37    /// The message field.
38    pub message: String,
39    /// The spans field.
40    pub spans: Vec<Span>,
41}
42
43impl Diagnostic {
44    /// Performs error.
45    pub fn error(code: &'static str, message: impl Into<String>) -> Self {
46        Self {
47            severity: Severity::Error,
48            code: DiagnosticCode(code),
49            message: message.into(),
50            spans: Vec::new(),
51        }
52    }
53
54    /// Performs warning.
55    pub fn warning(code: &'static str, message: impl Into<String>) -> Self {
56        Self {
57            severity: Severity::Warning,
58            code: DiagnosticCode(code),
59            message: message.into(),
60            spans: Vec::new(),
61        }
62    }
63
64    /// Performs info.
65    pub fn info(code: &'static str, message: impl Into<String>) -> Self {
66        Self {
67            severity: Severity::Info,
68            code: DiagnosticCode(code),
69            message: message.into(),
70            spans: Vec::new(),
71        }
72    }
73
74    /// Performs with_span.
75    pub fn with_span(mut self, span: Span) -> Self {
76        self.spans.push(span);
77        self
78    }
79}