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 167
|
use serde::{Deserialize, Deserializer, Serialize};
use annotate_snippets::{
display_list::FormatOptions,
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
#[derive(Deserialize)]
pub struct SnippetDef<'a> {
#[serde(deserialize_with = "deserialize_annotation")]
#[serde(default)]
#[serde(borrow)]
pub title: Option<Annotation<'a>>,
#[serde(deserialize_with = "deserialize_annotations")]
#[serde(default)]
#[serde(borrow)]
pub footer: Vec<Annotation<'a>>,
#[serde(deserialize_with = "deserialize_opt")]
#[serde(default)]
pub opt: FormatOptions,
#[serde(deserialize_with = "deserialize_slices")]
#[serde(borrow)]
pub slices: Vec<Slice<'a>>,
}
impl<'a> Into<Snippet<'a>> for SnippetDef<'a> {
fn into(self) -> Snippet<'a> {
let SnippetDef {
title,
footer,
opt,
slices,
} = self;
Snippet {
title,
footer,
slices,
opt,
}
}
}
fn deserialize_opt<'de, D>(deserializer: D) -> Result<FormatOptions, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(with = "FormatOptionsDef")] FormatOptions);
Wrapper::deserialize(deserializer).map(|w| w.0)
}
#[derive(Deserialize)]
#[serde(remote = "FormatOptions")]
pub struct FormatOptionsDef {
pub color: bool,
pub anonymized_line_numbers: bool,
}
fn deserialize_slices<'de, D>(deserializer: D) -> Result<Vec<Slice<'de>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper<'a>(
#[serde(with = "SliceDef")]
#[serde(borrow)]
Slice<'a>,
);
let v = Vec::deserialize(deserializer)?;
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
}
fn deserialize_annotation<'de, D>(deserializer: D) -> Result<Option<Annotation<'de>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper<'a>(
#[serde(with = "AnnotationDef")]
#[serde(borrow)]
Annotation<'a>,
);
Option::<Wrapper>::deserialize(deserializer)
.map(|opt_wrapped: Option<Wrapper>| opt_wrapped.map(|wrapped: Wrapper| wrapped.0))
}
fn deserialize_annotations<'de, D>(deserializer: D) -> Result<Vec<Annotation<'de>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper<'a>(
#[serde(with = "AnnotationDef")]
#[serde(borrow)]
Annotation<'a>,
);
let v = Vec::deserialize(deserializer)?;
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
}
#[derive(Deserialize)]
#[serde(remote = "Slice")]
pub struct SliceDef<'a> {
#[serde(borrow)]
pub source: &'a str,
pub line_start: usize,
#[serde(borrow)]
pub origin: Option<&'a str>,
#[serde(deserialize_with = "deserialize_source_annotations")]
#[serde(borrow)]
pub annotations: Vec<SourceAnnotation<'a>>,
#[serde(default)]
pub fold: bool,
}
fn deserialize_source_annotations<'de, D>(
deserializer: D,
) -> Result<Vec<SourceAnnotation<'de>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper<'a>(
#[serde(with = "SourceAnnotationDef")]
#[serde(borrow)]
SourceAnnotation<'a>,
);
let v = Vec::deserialize(deserializer)?;
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "SourceAnnotation")]
pub struct SourceAnnotationDef<'a> {
pub range: (usize, usize),
#[serde(borrow)]
pub label: &'a str,
#[serde(with = "AnnotationTypeDef")]
pub annotation_type: AnnotationType,
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "Annotation")]
pub struct AnnotationDef<'a> {
#[serde(borrow)]
pub id: Option<&'a str>,
#[serde(borrow)]
pub label: Option<&'a str>,
#[serde(with = "AnnotationTypeDef")]
pub annotation_type: AnnotationType,
}
#[allow(dead_code)]
#[derive(Serialize, Deserialize)]
#[serde(remote = "AnnotationType")]
enum AnnotationTypeDef {
Error,
Warning,
Info,
Note,
Help,
}
|