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
use crate::error::{Error, ErrorKind};
#[derive(Copy, Clone, Debug)]
pub struct Parts<'a> {
pub front_matter: &'a str,
pub markdown: &'a str,
pub title: &'a str,
pub description: &'a str,
}
impl<'a> Parts<'a> {
pub fn parse(advisory_data: &'a str) -> Result<Self, Error> {
if !advisory_data.starts_with("```toml") {
let context = if advisory_data.len() > 20 {
&advisory_data[..20]
} else {
advisory_data
};
fail!(
ErrorKind::Parse,
"unexpected start of advisory: \"{}\"",
context
)
}
let toml_end = advisory_data.find("\n```").ok_or_else(|| {
format_err!(
ErrorKind::Parse,
"couldn't find end of TOML front matter in advisory"
)
})?;
let front_matter = advisory_data[7..toml_end].trim_start().trim_end();
let markdown = advisory_data[(toml_end + 4)..].trim_start();
if !markdown.starts_with("# ") {
fail!(
ErrorKind::Parse,
"Expected # header after TOML front matter"
);
}
let next_newline = markdown.find('\n').ok_or_else(|| {
format_err!(
ErrorKind::Parse,
"no Markdown body (i.e. description) found"
)
})?;
let title = markdown[2..next_newline].trim_end();
let description = markdown[(next_newline + 1)..].trim_start().trim_end();
Ok(Self {
front_matter,
markdown,
title,
description,
})
}
}