Skip to content

Commit

Permalink
accept version prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Feb 11, 2024
1 parent 514541a commit f854702
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/versions/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use super::{Version, VersionState};
use anyhow::Result;
use nom::{
bytes::complete::*, character::complete::char, combinator::map_res, error::ErrorKind, IResult,
bytes::complete::*,
character::complete::char,
combinator::map_res,
error::{Error, ErrorKind},
IResult,
};

fn number(s: &str) -> IResult<&str, usize> {
Expand Down Expand Up @@ -34,45 +38,55 @@ fn suffix(s: &str) -> IResult<&str, VersionState> {
}))
}

fn version_prefix(s: &str) -> IResult<&str, ()> {
char::<&str, Error<_>>('v')(s)
.or_else(|_| char::<&str, Error<_>>('V')(s))
.map(|(s, _)| (s, ()))
.or(Ok((s, ())))
}

/// Takes a Golang release version formatted string and
/// parses it into a [`Version`].
pub fn parse_version(s: &str) -> Result<Version> {
let mut version = Version::default();

let (s, major) = number(s).map_err(|_| anyhow::anyhow!("failed parsing major version"))?;
let (s, _) =
version_prefix(s).map_err(|e| anyhow::anyhow!("failed parsing version prefix: {e}"))?;

let (s, major) = number(s).map_err(|e| anyhow::anyhow!("failed parsing major version: {e}"))?;
version.major = major;

if s.is_empty() {
return Ok(version);
}

let Ok((s, _)) = delim(s) else {
let (_, suffix) = suffix(s).map_err(|_| anyhow::anyhow!("failed parsing suffix"))?;
let (_, suffix) = suffix(s).map_err(|e| anyhow::anyhow!("failed parsing suffix: {e}"))?;
version.pre = Some(suffix);
return Ok(version);
};

let (s, minor) = number(s).map_err(|_| anyhow::anyhow!("failed parsing minor version"))?;
let (s, minor) = number(s).map_err(|e| anyhow::anyhow!("failed parsing minor version: {e}"))?;
version.minor = Some(minor);

if s.is_empty() {
return Ok(version);
}

let Ok((s, _)) = delim(s) else {
let (_, suffix) = suffix(s).map_err(|_| anyhow::anyhow!("failed parsing suffix"))?;
let (_, suffix) = suffix(s).map_err(|e| anyhow::anyhow!("failed parsing suffix: {e}"))?;
version.pre = Some(suffix);
return Ok(version);
};

let (s, patch) = number(s).map_err(|_| anyhow::anyhow!("failed parsing patch version"))?;
let (s, patch) = number(s).map_err(|e| anyhow::anyhow!("failed parsing patch version: {e}"))?;
version.patch = Some(patch);

if s.is_empty() {
return Ok(version);
}

let (_, suffix) = suffix(s).map_err(|_| anyhow::anyhow!("failed parsing suffix"))?;
let (_, suffix) = suffix(s).map_err(|e| anyhow::anyhow!("failed parsing suffix: {e}"))?;
version.pre = Some(suffix);

Ok(version)
Expand Down
20 changes: 20 additions & 0 deletions src/versions/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ mod test {
..Default::default()
}
);

assert_eq!(
Version::from_str("v1.2.345").unwrap(),
Version {
major: 1,
minor: Some(2),
patch: Some(345),
..Default::default()
}
);

assert_eq!(
Version::from_str("V1.2.345").unwrap(),
Version {
major: 1,
minor: Some(2),
patch: Some(345),
..Default::default()
}
);
}

#[test]
Expand Down

0 comments on commit f854702

Please sign in to comment.