Skip to content

Commit

Permalink
Merge branch 'winnow'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 25, 2023
2 parents 146c149 + 3cd460b commit 37ade02
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 60 deletions.
6 changes: 3 additions & 3 deletions cargo-smart-release/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo-smart-release/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ toml_edit = "0.19.1"
semver = "1.0.4"
crates-index = { version = "1.0.0", git = "https://github.com/Byron/rust-crates-index", branch = "replace-git2-with-gitoxide" }
cargo_toml = "0.15.1"
nom = { version = "7", default-features = false, features = ["std"]}
winnow = "0.5.1"
git-conventional = "0.12.0"
time = "0.3.19"
pulldown-cmark = "0.9.0"
Expand Down
107 changes: 51 additions & 56 deletions cargo-smart-release/src/changelog/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use std::{
};

use gix::bstr::ByteSlice;
use nom::{
branch::alt,
bytes::complete::{tag, tag_no_case, take_till, take_while, take_while_m_n},
combinator::{all_consuming, map, map_res, opt},
error::{FromExternalError, ParseError},
sequence::{delimited, preceded, separated_pair, terminated, tuple},
Finish, IResult,
};
use pulldown_cmark::{CowStr, Event, HeadingLevel, OffsetIter, Tag};
use winnow::{
combinator::alt,
combinator::opt,
combinator::{delimited, preceded, separated_pair, terminated},
error::{FromExternalError, ParserError},
prelude::*,
token::{tag_no_case, take_till0, take_while},
};

use crate::{
changelog,
Expand Down Expand Up @@ -467,59 +467,54 @@ impl<'a> TryFrom<&'a str> for Headline {
type Error = ();

fn try_from(value: &'a str) -> Result<Self, Self::Error> {
all_consuming(headline::<()>)(value).finish().map(|(_, h)| h)
headline::<()>.parse(value).map_err(|err| err.into_inner())
}
}

fn headline<'a, E: ParseError<&'a str> + FromExternalError<&'a str, ()>>(i: &'a str) -> IResult<&'a str, Headline, E> {
let hashes = take_while(|c: char| c == '#');
let greedy_whitespace = |i| take_while(char::is_whitespace)(i);
let take_n_digits = |n: usize| {
map_res(take_while_m_n(n, n, |c: char| c.is_ascii_digit()), |num| {
u32::from_str(num).map_err(|_| ())
})
};
map(
terminated(
tuple((
separated_pair(
hashes,
greedy_whitespace,
alt((
tuple((
opt(tag("v")),
map_res(take_till(char::is_whitespace), |v| {
semver::Version::parse(v).map_err(|_| ()).map(Some)
}),
)),
map(tag_no_case("unreleased"), |_| (None, None)),
)),
),
opt(preceded(
greedy_whitespace,
delimited(
tag("("),
map_res(
tuple((take_n_digits(4), tag("-"), take_n_digits(2), tag("-"), take_n_digits(2))),
|(year, _, month, _, day)| {
time::Month::try_from(month as u8).map_err(|_| ()).and_then(|month| {
time::Date::from_calendar_date(year as i32, month, day as u8)
.map_err(|_| ())
.map(|d| d.midnight().assume_utc())
})
},
),
tag(")"),
fn headline<'a, E: ParserError<&'a str> + FromExternalError<&'a str, ()>>(i: &mut &'a str) -> PResult<Headline, E> {
let hashes = take_while(0.., |c: char| c == '#');
let greedy_whitespace = |i: &mut &'a str| take_while(0.., char::is_whitespace).parse_next(i);
let take_n_digits =
|n: usize| take_while(n, |c: char| c.is_ascii_digit()).try_map(|num| u32::from_str(num).map_err(|_| ()));

terminated(
(
separated_pair(
hashes,
greedy_whitespace,
alt((
(
opt("v"),
take_till0(char::is_whitespace)
.try_map(|v| semver::Version::parse(v).map_err(|_| ()).map(Some)),
),
tag_no_case("unreleased").map(|_| (None, None)),
)),
),
opt(preceded(
greedy_whitespace,
delimited(
"(",
(take_n_digits(4), "-", take_n_digits(2), "-", take_n_digits(2)).try_map(
|(year, _, month, _, day)| {
time::Month::try_from(month as u8).map_err(|_| ()).and_then(|month| {
time::Date::from_calendar_date(year as i32, month, day as u8)
.map_err(|_| ())
.map(|d| d.midnight().assume_utc())
})
},
),
")",
),
)),
greedy_whitespace,
),
|((hashes, (prefix, version)), date)| Headline {
level: hashes.len(),
version_prefix: prefix.map_or_else(String::new, ToOwned::to_owned),
version,
date,
},
)(i)
greedy_whitespace,
)
.map(|((hashes, (prefix, version)), date)| Headline {
level: hashes.len(),
version_prefix: prefix.map_or_else(String::new, ToOwned::to_owned),
version,
date,
})
.parse_next(i)
}

0 comments on commit 37ade02

Please sign in to comment.