use anyhow::Result;
use lazy_static::lazy_static;
use pomsky_macro::pomsky;
use regex::Captures;
pub fn validate_440_version(version: &str) -> Result<Captures> {
lazy_static! {
static ref VERSION_VALIDATOR: regex::Regex =
regex::Regex::new(VALIDATION_REGEX).unwrap();
}
let version_match: Captures = match VERSION_VALIDATOR.captures(version) {
Some(v) => v,
None => anyhow::bail!("Failed to decode version {}", version),
};
Ok(version_match)
}
static VALIDATION_REGEX: &str = pomsky!(
"v"?
(:epoch(['0'-'9']+)'!')?
:release(['0'-'9']+("."['0'-'9']+)*)
:pre(
["-" "_" "."]?
:pre_l(
("preview"|"alpha"|"beta"|"pre"|"rc"|"a"|"b"|"c")
)
["-" "_" "."]?
:pre_n(['0'-'9']+)?
)?
:post(
"-"
:post_n1(['0'-'9']+)
|
["-" "_" "."]?
:post_l("post" | "rev" | "r")
["-" "_" "."]?
:post_n2(['0'-'9']+)?
)?
:dev(
["-" "_" "."]?
:dev_l("dev")
["-" "_" "."]?
:dev_n(['0'-'9']+)?
)?
(
"+"
:local(
['a'-'z' '0'-'9']+
((["-" "_" "."] ['a'-'z' '0'-'9']+)+)?
)
)?
);