Skip to content

Commit 887331e

Browse files
committed
Separate parsing and linting in public API
1 parent e76a493 commit 887331e

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/cli/commands.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::path::PathBuf;
44

55
use clap::ArgMatches;
66

7-
use crate::parse_file;
7+
use crate::parse_and_lint_file;
8+
use crate::profile::Profile;
89
use crate::rule::Rule;
910
use crate::span::Index;
1011

@@ -23,7 +24,8 @@ pub fn check(matches: &ArgMatches) -> Result<()> {
2324
"full" => OutputFormat::Full,
2425
_ => unreachable!(),
2526
};
26-
let (_, diagnostics) = parse_file(&path).unwrap();
27+
let profile = Profile::default();
28+
let (_, diagnostics) = parse_and_lint_file(&path, &profile).unwrap();
2729
if diagnostics.is_empty() {
2830
Ok(())
2931
} else {

src/lib.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,52 @@ pub mod unist;
1818
pub use changelog::Changelog;
1919
pub use diagnostic::Diagnostic;
2020
pub use error::{Error, ParseError};
21+
pub use profile::Profile;
2122
pub use rule::Rule;
2223

2324
/// Parse a changelog from a string.
2425
///
2526
/// Parsing a changelog will always succeed. This function returns a [`Result`] to support future
2627
/// fatal errors (e.g. if the document is not a changelog at all).
27-
pub fn parse_str(s: &str) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
28-
parse(s, None)
28+
pub fn parse_str(s: &str) -> Result<Changelog, ParseError> {
29+
Ok(parser::parse(&s).into())
30+
}
31+
32+
/// Parse and lint a changelog from a string.
33+
pub fn parse_and_lint_str(
34+
s: &str,
35+
profile: &Profile,
36+
) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
37+
parse_and_lint(s, None, profile)
2938
}
3039

3140
/// Parse a changelog from a file.
3241
///
3342
/// As with [`parse_str()`], parsing the changelog document will nearly always succeed.
3443
/// `parse_file()` may additionally return a [`std::io::Error`] ([`Error::Io`]).
35-
pub fn parse_file(path: &Path) -> Result<(Changelog, Vec<Diagnostic>), Error> {
44+
pub fn parse_file(path: &Path) -> Result<Changelog, Error> {
45+
let s = std::fs::read_to_string(path)?;
46+
Ok(parser::parse(&s).into())
47+
}
48+
49+
/// Parse and lint a changelog from a file.
50+
///
51+
/// As with [`parse_str()`], parsing the changelog document will nearly always succeed.
52+
/// `parse_file()` may additionally return a [`std::io::Error`] ([`Error::Io`]).
53+
pub fn parse_and_lint_file(
54+
path: &Path,
55+
profile: &Profile,
56+
) -> Result<(Changelog, Vec<Diagnostic>), Error> {
3657
let s = std::fs::read_to_string(path)?;
37-
Ok(parse(&s, Some(path))?)
58+
Ok(parse_and_lint(&s, Some(path), profile)?)
3859
}
3960

40-
fn parse(s: &str, path: Option<&Path>) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
61+
fn parse_and_lint(
62+
s: &str,
63+
path: Option<&Path>,
64+
profile: &Profile,
65+
) -> Result<(Changelog, Vec<Diagnostic>), ParseError> {
4166
let changelog = parser::parse(s);
42-
let profile = profile::Profile::default();
4367
let mut diagnostics = linter::lint(&changelog, &profile);
4468
diagnostics.sort_by_key(|d| d.span);
4569
for diagnostic in &mut diagnostics {

0 commit comments

Comments
 (0)