vrl/diagnostic/
note.rs

1use std::{convert::Into, fmt};
2
3use super::Urls;
4
5#[derive(Debug, Eq, PartialEq, Clone)]
6pub enum Note {
7    Hint(String),
8    Example(String),
9    CoerceValue,
10    SeeFunctionDocs(&'static str),
11    SeeErrorDocs,
12    SeeCodeDocs(usize),
13    SeeLangDocs,
14    SeeFunctionCharacteristicsDocs,
15    SeeRepl,
16
17    #[doc(hidden)]
18    SeeDocs(String, String),
19    #[doc(hidden)]
20    Basic(String),
21    #[doc(hidden)]
22    UserErrorMessage(String),
23}
24
25impl Note {
26    pub fn solution(title: impl Into<String>, content: Vec<impl Into<String>>) -> Vec<Self> {
27        let mut notes = vec![Self::Basic(format!("try: {}", title.into()))];
28
29        notes.push(Self::Basic(" ".to_owned()));
30        for line in content {
31            notes.push(Self::Basic(format!("    {}", line.into())));
32        }
33        notes.push(Self::Basic(" ".to_owned()));
34        notes
35    }
36}
37
38impl fmt::Display for Note {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        use Note::{
41            Basic, CoerceValue, Example, Hint, SeeCodeDocs, SeeDocs, SeeErrorDocs,
42            SeeFunctionCharacteristicsDocs, SeeFunctionDocs, SeeLangDocs, SeeRepl,
43            UserErrorMessage,
44        };
45
46        match self {
47            Hint(hint) => {
48                write!(f, "hint: {hint}")
49            }
50            Example(example) => {
51                write!(f, "example: {example}")
52            }
53            CoerceValue => {
54                Hint("coerce the value to the required type using a coercion function".to_owned())
55                    .fmt(f)
56            }
57            SeeFunctionDocs(ident) => {
58                let url = Urls::func_docs(ident);
59                SeeDocs("function".to_owned(), url).fmt(f)
60            }
61            SeeErrorDocs => {
62                let url = Urls::error_handling_url();
63                SeeDocs("error handling".to_owned(), url).fmt(f)
64            }
65            SeeLangDocs => {
66                let url = Urls::vrl_root_url();
67
68                write!(f, "see language documentation at {url}")
69            }
70            SeeFunctionCharacteristicsDocs => {
71                let url = Urls::func_characteristics();
72                write!(f, "see functions characteristics documentation at {url}")
73            }
74            SeeRepl => {
75                let url = Urls::example_docs();
76
77                write!(f, "try your code in the VRL REPL, learn more at {url}")
78            }
79            SeeCodeDocs(code) => {
80                let url = Urls::error_code_url(*code);
81                write!(f, "learn more about error code {code} at {url}")
82            }
83            SeeDocs(kind, url) => {
84                write!(f, "see documentation about {kind} at {url}")
85            }
86            Basic(string) => write!(f, "{string}"),
87            UserErrorMessage(message) => write!(f, "{message}"),
88        }
89    }
90}