vrl/compiler/
deprecation_warning.rs

1use std::fmt::{Display, Formatter};
2
3use crate::diagnostic::{DiagnosticMessage, Label, Note, Severity};
4
5use super::Span;
6
7#[derive(Debug)]
8pub struct DeprecationWarning {
9    item: String,
10    version: String,
11    notes: Vec<Note>,
12    span: Option<Span>,
13}
14
15impl DeprecationWarning {
16    #[must_use]
17    pub fn new(item: &str, version: &str) -> Self {
18        DeprecationWarning {
19            item: item.to_string(),
20            version: version.to_string(),
21            notes: vec![],
22            span: None,
23        }
24    }
25
26    #[must_use]
27    pub fn with_note(mut self, note: Note) -> Self {
28        self.notes.push(note);
29        self
30    }
31
32    #[must_use]
33    pub fn with_notes(mut self, mut notes: Vec<Note>) -> Self {
34        self.notes.append(&mut notes);
35        self
36    }
37
38    #[must_use]
39    pub fn with_span(mut self, span: Span) -> Self {
40        self.span = Some(span);
41        self
42    }
43}
44
45impl std::error::Error for DeprecationWarning {}
46
47impl Display for DeprecationWarning {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", self.message())
50    }
51}
52
53impl DiagnosticMessage for DeprecationWarning {
54    fn code(&self) -> usize {
55        801
56    }
57
58    fn message(&self) -> String {
59        format!("{} is deprecated since v{}", self.item, self.version)
60    }
61
62    fn labels(&self) -> Vec<Label> {
63        if let Some(span) = self.span {
64            vec![Label::primary("this is deprecated", span)]
65        } else {
66            vec![]
67        }
68    }
69
70    fn notes(&self) -> Vec<Note> {
71        self.notes.clone()
72    }
73
74    fn severity(&self) -> Severity {
75        Severity::Warning
76    }
77}