]>
Commit | Line | Data |
---|---|---|
416331ca | 1 | use serde::{Serialize, Deserialize, Deserializer}; |
dc9dc135 | 2 | |
416331ca | 3 | use annotate_snippets::snippet::{ |
dc9dc135 XL |
4 | Annotation, AnnotationType, Slice, Snippet, SourceAnnotation, |
5 | }; | |
6 | ||
7 | #[derive(Deserialize)] | |
8 | #[serde(remote = "Snippet")] | |
9 | pub struct SnippetDef { | |
10 | #[serde(deserialize_with = "deserialize_annotation")] | |
11 | #[serde(default)] | |
12 | pub title: Option<Annotation>, | |
13 | #[serde(deserialize_with = "deserialize_annotations")] | |
14 | #[serde(default)] | |
15 | pub footer: Vec<Annotation>, | |
16 | #[serde(deserialize_with = "deserialize_slices")] | |
17 | pub slices: Vec<Slice>, | |
18 | } | |
19 | ||
20 | fn deserialize_slices<'de, D>(deserializer: D) -> Result<Vec<Slice>, D::Error> | |
21 | where | |
22 | D: Deserializer<'de>, | |
23 | { | |
24 | #[derive(Deserialize)] | |
25 | struct Wrapper(#[serde(with = "SliceDef")] Slice); | |
26 | ||
27 | let v = Vec::deserialize(deserializer)?; | |
28 | Ok(v.into_iter().map(|Wrapper(a)| a).collect()) | |
29 | } | |
30 | ||
31 | fn deserialize_annotation<'de, D>(deserializer: D) -> Result<Option<Annotation>, D::Error> | |
32 | where | |
33 | D: Deserializer<'de>, | |
34 | { | |
35 | #[derive(Deserialize)] | |
36 | struct Wrapper(#[serde(with = "AnnotationDef")] Annotation); | |
37 | ||
38 | Option::<Wrapper>::deserialize(deserializer) | |
39 | .map(|opt_wrapped: Option<Wrapper>| opt_wrapped.map(|wrapped: Wrapper| wrapped.0)) | |
40 | } | |
41 | ||
42 | fn deserialize_annotations<'de, D>(deserializer: D) -> Result<Vec<Annotation>, D::Error> | |
43 | where | |
44 | D: Deserializer<'de>, | |
45 | { | |
46 | #[derive(Deserialize)] | |
47 | struct Wrapper(#[serde(with = "AnnotationDef")] Annotation); | |
48 | ||
49 | let v = Vec::deserialize(deserializer)?; | |
50 | Ok(v.into_iter().map(|Wrapper(a)| a).collect()) | |
51 | } | |
52 | ||
53 | #[derive(Deserialize)] | |
54 | #[serde(remote = "Slice")] | |
55 | pub struct SliceDef { | |
56 | pub source: String, | |
57 | pub line_start: usize, | |
58 | pub origin: Option<String>, | |
59 | #[serde(deserialize_with = "deserialize_source_annotations")] | |
60 | pub annotations: Vec<SourceAnnotation>, | |
61 | #[serde(default)] | |
62 | pub fold: bool, | |
63 | } | |
64 | ||
65 | fn deserialize_source_annotations<'de, D>( | |
66 | deserializer: D, | |
67 | ) -> Result<Vec<SourceAnnotation>, D::Error> | |
68 | where | |
69 | D: Deserializer<'de>, | |
70 | { | |
71 | #[derive(Deserialize)] | |
72 | struct Wrapper(#[serde(with = "SourceAnnotationDef")] SourceAnnotation); | |
73 | ||
74 | let v = Vec::deserialize(deserializer)?; | |
75 | Ok(v.into_iter().map(|Wrapper(a)| a).collect()) | |
76 | } | |
77 | ||
78 | #[derive(Serialize, Deserialize)] | |
79 | #[serde(remote = "SourceAnnotation")] | |
80 | pub struct SourceAnnotationDef { | |
81 | pub range: (usize, usize), | |
82 | pub label: String, | |
83 | #[serde(with = "AnnotationTypeDef")] | |
84 | pub annotation_type: AnnotationType, | |
85 | } | |
86 | ||
87 | #[derive(Serialize, Deserialize)] | |
88 | #[serde(remote = "Annotation")] | |
89 | pub struct AnnotationDef { | |
90 | pub id: Option<String>, | |
91 | pub label: Option<String>, | |
92 | #[serde(with = "AnnotationTypeDef")] | |
93 | pub annotation_type: AnnotationType, | |
94 | } | |
95 | ||
96 | #[allow(dead_code)] | |
97 | #[derive(Serialize, Deserialize)] | |
98 | #[serde(remote = "AnnotationType")] | |
99 | enum AnnotationTypeDef { | |
100 | Error, | |
101 | Warning, | |
102 | Info, | |
103 | Note, | |
104 | Help, | |
105 | } |