nvrim/diagnostics/
formatter.rs1use serde::Deserialize;
7
8#[allow(clippy::needless_pass_by_value)]
10pub fn format(diagnostic: Diagnostic) -> Option<String> {
11 let Some(msg) = get_msg(&diagnostic).map(|s| s.trim_end_matches('.').to_string()) else {
12 ytil_noxi::notify::error(format!("error missing diagnostic message | diagnostic={diagnostic:#?}"));
13 return None;
14 };
15
16 let Some(src) = get_src(&diagnostic).map(str::to_string) else {
17 ytil_noxi::notify::error(format!("error missing diagnostic source | diagnostic={diagnostic:#?}"));
18 return None;
19 };
20
21 let src_and_code = get_code(&diagnostic).map_or_else(|| src.clone(), |c| format!("{src}: {c}"));
22
23 Some(format!("▶ {msg} [{src_and_code}]"))
24}
25
26fn get_msg(diag: &Diagnostic) -> Option<&str> {
28 diag.user_data
29 .as_ref()
30 .and_then(|user_data| {
31 user_data
32 .lsp
33 .as_ref()
34 .and_then(|lsp| {
35 lsp.data
36 .as_ref()
37 .and_then(|lsp_data| lsp_data.rendered.as_deref())
38 .or(lsp.message.as_deref())
39 })
40 .or(diag.message.as_deref())
41 })
42 .or(diag.message.as_deref())
43}
44
45fn get_src(diag: &Diagnostic) -> Option<&str> {
47 diag.user_data
48 .as_ref()
49 .and_then(|user_data| user_data.lsp.as_ref().and_then(|lsp| lsp.source.as_deref()))
50 .or(diag.source.as_deref())
51}
52
53fn get_code(diag: &Diagnostic) -> Option<&str> {
55 diag.user_data
56 .as_ref()
57 .and_then(|user_data| user_data.lsp.as_ref().and_then(|lsp| lsp.code.as_deref()))
58 .or(diag.code.as_deref())
59}
60
61#[derive(Debug, Deserialize)]
63pub struct Diagnostic {
64 code: Option<String>,
66 message: Option<String>,
68 source: Option<String>,
70 user_data: Option<UserData>,
72}
73
74ytil_noxi::impl_nvim_deserializable!(Diagnostic);
75
76#[derive(Debug, Deserialize)]
78pub struct UserData {
79 lsp: Option<Lsp>,
81}
82
83#[derive(Debug, Deserialize)]
85pub struct Lsp {
86 code: Option<String>,
88 data: Option<LspData>,
90 message: Option<String>,
92 source: Option<String>,
94}
95
96#[derive(Debug, Deserialize)]
98pub struct LspData {
99 rendered: Option<String>,
100}