Skip to content

Commit e76a493

Browse files
committed
Remove diagnostics from parser
1 parent 4a02852 commit e76a493

File tree

3 files changed

+114
-127
lines changed

3 files changed

+114
-127
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub use rule::Rule;
2222

2323
/// Parse a changelog from a string.
2424
///
25-
/// Parsing a changelog will nearly always succeed. This function returns a [`Result`] to support
26-
/// future fatal errors (e.g. if the document is not a changelog at all).
25+
/// Parsing a changelog will always succeed. This function returns a [`Result`] to support future
26+
/// fatal errors (e.g. if the document is not a changelog at all).
2727
pub fn parse_str(s: &str) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
2828
parse(s, None)
2929
}
@@ -38,9 +38,9 @@ pub fn parse_file(path: &Path) -> Result<(Changelog, Vec<Diagnostic>), Error> {
3838
}
3939

4040
fn parse(s: &str, path: Option<&Path>) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
41-
let (changelog, mut diagnostics) = parser::parse(s);
41+
let changelog = parser::parse(s);
4242
let profile = profile::Profile::default();
43-
diagnostics.append(&mut linter::lint(&changelog, &profile));
43+
let mut diagnostics = linter::lint(&changelog, &profile);
4444
diagnostics.sort_by_key(|d| d.span);
4545
for diagnostic in &mut diagnostics {
4646
diagnostic.path = path.map(|p| p.to_path_buf());

src/parser.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Parse a changelog as its [intermediate representation](crate::ir::Changelog).
22
use crate::ast::*;
3-
use crate::diagnostic::Diagnostic;
43
use crate::ir::*;
54
use crate::rule::Rule;
65
use crate::span::{Span, SpanIterator};
@@ -10,11 +9,9 @@ use pulldown_cmark as md;
109

1110
/// Parse a changelog.
1211
///
13-
/// Parsing never fails, although the parser may return diagnostics indicating that it cannot parse
14-
/// some element of the document correctly.
15-
pub fn parse(s: &str) -> (Changelog, Vec<Diagnostic>) {
12+
/// Parsing never fails.
13+
pub fn parse(s: &str) -> Changelog {
1614
let mut changelog = Changelog::default();
17-
let mut diagnostics: Vec<Diagnostic> = Vec::new();
1815
let mut blocks = Parser::new(s).peekable();
1916
while let Some(block) = blocks.next() {
2017
match block {
@@ -31,8 +28,7 @@ pub fn parse(s: &str) -> (Changelog, Vec<Diagnostic>) {
3128
changelog.sections.push(section);
3229
}
3330
Block::Heading(heading @ Heading { level: 2, .. }) => {
34-
let (section, mut section_diagnostics) = parse_section(s, &heading, &mut blocks);
35-
diagnostics.append(&mut section_diagnostics);
31+
let section = parse_section(s, &heading, &mut blocks);
3632
if let Some(sec) = section {
3733
changelog.sections.push(sec);
3834
}
@@ -47,20 +43,14 @@ pub fn parse(s: &str) -> (Changelog, Vec<Diagnostic>) {
4743
};
4844
let parser = md::Parser::new_with_broken_link_callback(s, md::Options::empty(), Some(callback));
4945
for _event in parser {}
50-
(changelog, diagnostics)
46+
changelog
5147
}
5248

53-
fn parse_section(
54-
s: &str,
55-
heading: &Heading,
56-
blocks: &mut Peekable<Parser>,
57-
) -> (Option<Section>, Vec<Diagnostic>) {
58-
let mut diagnostics: Vec<Diagnostic> = Vec::new();
49+
fn parse_section(s: &str, heading: &Heading, blocks: &mut Peekable<Parser>) -> Option<Section> {
5950
let section = match heading.inlines.as_slice() {
6051
// Unreleased
6152
[Inline::Link(l)] if &s[l.content.span.range()] == "Unreleased" => {
62-
let (changes, mut change_diagnostics) = parse_changes(s, blocks);
63-
diagnostics.append(&mut change_diagnostics);
53+
let changes = parse_changes(s, blocks);
6454
Some(Section::Unreleased(Unreleased {
6555
heading_span: heading.span,
6656
url: Some(l.target.clone()),
@@ -86,21 +76,19 @@ fn parse_section(
8676
let yanked = &s[span.range()];
8777
release.yanked = Some(Spanned::new(span, yanked.to_string()));
8878
}
89-
let (changes, mut change_diagnostics) = parse_changes(s, blocks);
79+
let changes = parse_changes(s, blocks);
9080
release.changes = changes;
91-
diagnostics.append(&mut change_diagnostics);
9281
Some(Section::Release(release))
9382
}
9483
_ => Some(Section::Invalid(InvalidSection {
9584
heading_span: heading.span,
9685
})),
9786
};
98-
(section, diagnostics)
87+
section
9988
}
10089

101-
fn parse_changes(s: &str, blocks: &mut Peekable<Parser>) -> (Vec<Changes>, Vec<Diagnostic>) {
90+
fn parse_changes(s: &str, blocks: &mut Peekable<Parser>) -> Vec<Changes> {
10291
let mut sections: Vec<(Span, Spanned<String>, Vec<Spanned<String>>)> = Vec::new();
103-
let diagnostics: Vec<Diagnostic> = Vec::new();
10492
let mut current_kind: Option<String> = None;
10593
let mut current_changes: Vec<Spanned<String>> = Vec::new();
10694
let mut current_heading_span: Span = Span::default();
@@ -151,7 +139,7 @@ fn parse_changes(s: &str, blocks: &mut Peekable<Parser>) -> (Vec<Changes>, Vec<D
151139
})
152140
.collect();
153141

154-
(changes, diagnostics)
142+
changes
155143
}
156144

157145
fn get_heading_span(heading: &Heading) -> Option<Span> {
@@ -206,7 +194,7 @@ mod tests {
206194
[1.0.0]: https://example.org/release/1.0.0
207195
[0.1.0]: https://example.org/release/0.1.0
208196
";
209-
let (changelog, diagnostics) = parse(source);
210-
assert_yaml_snapshot!((changelog, diagnostics));
197+
let changelog = parse(source);
198+
assert_yaml_snapshot!(changelog);
211199
}
212200
}
Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,109 @@
11
---
22
source: src/parser.rs
3-
expression: "(changelog, diagnostics)"
3+
expression: changelog
44
---
5-
- sections:
6-
- Title:
7-
span:
8-
start: 3
9-
end: 10
10-
value: Title 1
11-
- Title:
12-
span:
13-
start: 13
14-
end: 20
15-
value: Title 2
16-
- Unreleased:
17-
heading_span:
18-
start: 22
19-
end: 39
20-
url: "https://example.org/unreleased"
21-
changes:
22-
- heading_span:
5+
sections:
6+
- Title:
7+
span:
8+
start: 3
9+
end: 10
10+
value: Title 1
11+
- Title:
12+
span:
13+
start: 13
14+
end: 20
15+
value: Title 2
16+
- Unreleased:
17+
heading_span:
18+
start: 22
19+
end: 39
20+
url: "https://example.org/unreleased"
21+
changes:
22+
- heading_span:
23+
start: 41
24+
end: 52
25+
kind:
26+
span:
2327
start: 41
2428
end: 52
25-
kind:
26-
span:
27-
start: 41
28-
end: 52
29-
value: Removed
30-
changes:
31-
- span:
32-
start: 56
33-
end: 66
34-
value: Remove foo
35-
- Release:
36-
heading_span:
37-
start: 68
29+
value: Removed
30+
changes:
31+
- span:
32+
start: 56
33+
end: 66
34+
value: Remove foo
35+
- Release:
36+
heading_span:
37+
start: 68
38+
end: 91
39+
version:
40+
span:
41+
start: 72
42+
end: 77
43+
value: 1.0.0
44+
url: "https://example.org/release/1.0.0"
45+
date:
46+
span:
47+
start: 81
3848
end: 91
39-
version:
40-
span:
41-
start: 72
42-
end: 77
43-
value: 1.0.0
44-
url: "https://example.org/release/1.0.0"
45-
date:
46-
span:
47-
start: 81
48-
end: 91
49-
value: 2025-01-01
50-
yanked: ~
51-
changes:
52-
- heading_span:
49+
value: 2025-01-01
50+
yanked: ~
51+
changes:
52+
- heading_span:
53+
start: 93
54+
end: 102
55+
kind:
56+
span:
5357
start: 93
5458
end: 102
55-
kind:
56-
span:
57-
start: 93
58-
end: 102
59-
value: Added
60-
changes:
61-
- span:
62-
start: 106
63-
end: 113
64-
value: Add foo
65-
- span:
66-
start: 116
67-
end: 123
68-
value: Add bar
69-
- Release:
70-
heading_span:
71-
start: 125
59+
value: Added
60+
changes:
61+
- span:
62+
start: 106
63+
end: 113
64+
value: Add foo
65+
- span:
66+
start: 116
67+
end: 123
68+
value: Add bar
69+
- Release:
70+
heading_span:
71+
start: 125
72+
end: 148
73+
version:
74+
span:
75+
start: 129
76+
end: 134
77+
value: 0.1.0
78+
url: "https://example.org/release/0.1.0"
79+
date:
80+
span:
81+
start: 138
7282
end: 148
73-
version:
74-
span:
75-
start: 129
76-
end: 134
77-
value: 0.1.0
78-
url: "https://example.org/release/0.1.0"
79-
date:
80-
span:
81-
start: 138
82-
end: 148
83-
value: 2024-01-01
84-
yanked: ~
85-
changes:
86-
- heading_span:
83+
value: 2024-01-01
84+
yanked: ~
85+
changes:
86+
- heading_span:
87+
start: 150
88+
end: 159
89+
kind:
90+
span:
8791
start: 150
8892
end: 159
89-
kind:
90-
span:
91-
start: 150
92-
end: 159
93-
value: Added
94-
changes:
95-
- span:
96-
start: 163
97-
end: 170
98-
value: Add baz
99-
- span:
100-
start: 173
101-
end: 181
102-
value: Add quux
103-
- Invalid:
104-
heading_span:
105-
start: 183
106-
end: 207
107-
broken_links:
108-
- start: 186
109-
end: 194
110-
- []
93+
value: Added
94+
changes:
95+
- span:
96+
start: 163
97+
end: 170
98+
value: Add baz
99+
- span:
100+
start: 173
101+
end: 181
102+
value: Add quux
103+
- Invalid:
104+
heading_span:
105+
start: 183
106+
end: 207
107+
broken_links:
108+
- start: 186
109+
end: 194

0 commit comments

Comments
 (0)