File: deserialize.rs

package info (click to toggle)
rust-annotate-snippets 0.11.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: makefile: 2
file content (166 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use serde::{Deserialize, Deserializer, Serialize};
use std::ops::Range;

use annotate_snippets::renderer::DEFAULT_TERM_WIDTH;
use annotate_snippets::{Annotation, Level, Message, Renderer, Snippet};

#[derive(Deserialize)]
pub(crate) struct Fixture<'a> {
    #[serde(default)]
    pub(crate) renderer: RendererDef,
    #[serde(borrow)]
    pub(crate) message: MessageDef<'a>,
}

#[derive(Deserialize)]
pub struct MessageDef<'a> {
    #[serde(with = "LevelDef")]
    pub level: Level,
    #[serde(borrow)]
    pub title: &'a str,
    #[serde(default)]
    #[serde(borrow)]
    pub id: Option<&'a str>,
    #[serde(default)]
    #[serde(borrow)]
    pub footer: Vec<MessageDef<'a>>,
    #[serde(deserialize_with = "deserialize_snippets")]
    #[serde(borrow)]
    pub snippets: Vec<Snippet<'a>>,
}

impl<'a> From<MessageDef<'a>> for Message<'a> {
    fn from(val: MessageDef<'a>) -> Self {
        let MessageDef {
            level,
            title,
            id,
            footer,
            snippets,
        } = val;
        let mut message = level.title(title);
        if let Some(id) = id {
            message = message.id(id);
        }
        message = message.snippets(snippets);
        message = message.footers(footer.into_iter().map(Into::into));
        message
    }
}

fn deserialize_snippets<'de, D>(deserializer: D) -> Result<Vec<Snippet<'de>>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    struct Wrapper<'a>(
        #[serde(with = "SnippetDef")]
        #[serde(borrow)]
        SnippetDef<'a>,
    );

    let v = Vec::deserialize(deserializer)?;
    Ok(v.into_iter().map(|Wrapper(a)| a.into()).collect())
}

#[derive(Deserialize)]
pub struct SnippetDef<'a> {
    #[serde(borrow)]
    pub source: &'a str,
    pub line_start: usize,
    #[serde(borrow)]
    pub origin: Option<&'a str>,
    #[serde(deserialize_with = "deserialize_annotations")]
    #[serde(borrow)]
    pub annotations: Vec<Annotation<'a>>,
    #[serde(default)]
    pub fold: bool,
}

impl<'a> From<SnippetDef<'a>> for Snippet<'a> {
    fn from(val: SnippetDef<'a>) -> Self {
        let SnippetDef {
            source,
            line_start,
            origin,
            annotations,
            fold,
        } = val;
        let mut snippet = Snippet::source(source).line_start(line_start).fold(fold);
        if let Some(origin) = origin {
            snippet = snippet.origin(origin);
        }
        snippet = snippet.annotations(annotations);
        snippet
    }
}

fn deserialize_annotations<'de, D>(deserializer: D) -> Result<Vec<Annotation<'de>>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    struct Wrapper<'a>(#[serde(borrow)] AnnotationDef<'a>);

    let v = Vec::deserialize(deserializer)?;
    Ok(v.into_iter().map(|Wrapper(a)| a.into()).collect())
}

#[derive(Serialize, Deserialize)]
pub struct AnnotationDef<'a> {
    pub range: Range<usize>,
    #[serde(borrow)]
    pub label: &'a str,
    #[serde(with = "LevelDef")]
    pub level: Level,
}

impl<'a> From<AnnotationDef<'a>> for Annotation<'a> {
    fn from(val: AnnotationDef<'a>) -> Self {
        let AnnotationDef {
            range,
            label,
            level,
        } = val;
        level.span(range).label(label)
    }
}

#[derive(Serialize, Deserialize)]
pub(crate) struct LabelDef<'a> {
    #[serde(with = "LevelDef")]
    pub(crate) level: Level,
    #[serde(borrow)]
    pub(crate) label: &'a str,
}

#[allow(dead_code)]
#[derive(Serialize, Deserialize)]
#[serde(remote = "Level")]
enum LevelDef {
    Error,
    Warning,
    Info,
    Note,
    Help,
}

#[derive(Default, Deserialize)]
pub struct RendererDef {
    #[serde(default)]
    anonymized_line_numbers: bool,
    #[serde(default)]
    term_width: Option<usize>,
}

impl From<RendererDef> for Renderer {
    fn from(val: RendererDef) -> Self {
        let RendererDef {
            anonymized_line_numbers,
            term_width,
        } = val;
        Renderer::plain()
            .anonymized_line_numbers(anonymized_line_numbers)
            .term_width(term_width.unwrap_or(DEFAULT_TERM_WIDTH))
    }
}