diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-17 02:53:53 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-17 02:53:53 +0900 |
| commit | a1c7e6908a26e10f6e1f23a51721664b5e2d838d (patch) | |
| tree | c575c76f1b43359ed74913da4c6a2636643f1ba0 /crates/shirabe-semver/src | |
| parent | 7f606f36fef0c0467c3c0db3d0da33af486dae8a (diff) | |
| download | php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.tar.gz php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.tar.zst php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.zip | |
chore(style): cargo fmt
Diffstat (limited to 'crates/shirabe-semver/src')
| -rw-r--r-- | crates/shirabe-semver/src/compiling_matcher.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/constraint.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/constraint/constraint.rs | 22 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/constraint/multi_constraint.rs | 13 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/intervals.rs | 74 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/lib.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/version_parser.rs | 205 |
7 files changed, 204 insertions, 135 deletions
diff --git a/crates/shirabe-semver/src/compiling_matcher.rs b/crates/shirabe-semver/src/compiling_matcher.rs index 7fb17ec..f6a3689 100644 --- a/crates/shirabe-semver/src/compiling_matcher.rs +++ b/crates/shirabe-semver/src/compiling_matcher.rs @@ -27,8 +27,8 @@ static TRANS_OP_INT: &[(i64, &str)] = &[ pub struct CompilingMatcher; impl CompilingMatcher { - fn compiled_checker_cache( - ) -> &'static Mutex<IndexMap<String, Box<dyn Fn(String, bool) -> bool + Send + Sync>>> { + fn compiled_checker_cache() + -> &'static Mutex<IndexMap<String, Box<dyn Fn(String, bool) -> bool + Send + Sync>>> { COMPILED_CHECKER_CACHE.get_or_init(|| Mutex::new(IndexMap::new())) } @@ -42,8 +42,7 @@ impl CompilingMatcher { } pub fn r#match(constraint: &dyn ConstraintInterface, operator: i64, version: String) -> bool { - let result_cache_key = - format!("{}{};{}", operator, constraint.__to_string(), version); + let result_cache_key = format!("{}{};{}", operator, constraint.__to_string(), version); { let cache = Self::result_cache().lock().unwrap(); @@ -59,7 +58,10 @@ impl CompilingMatcher { .expect("unknown operator"); let result = constraint.matches(&Constraint::new(trans_op.to_string(), version)); - Self::result_cache().lock().unwrap().insert(result_cache_key, result); + Self::result_cache() + .lock() + .unwrap() + .insert(result_cache_key, result); result } } diff --git a/crates/shirabe-semver/src/constraint.rs b/crates/shirabe-semver/src/constraint.rs new file mode 100644 index 0000000..8410b61 --- /dev/null +++ b/crates/shirabe-semver/src/constraint.rs @@ -0,0 +1,6 @@ +pub mod bound; +pub mod constraint; +pub mod constraint_interface; +pub mod match_all_constraint; +pub mod match_none_constraint; +pub mod multi_constraint; diff --git a/crates/shirabe-semver/src/constraint/constraint.rs b/crates/shirabe-semver/src/constraint/constraint.rs index e35b3e4..b0c937c 100644 --- a/crates/shirabe-semver/src/constraint/constraint.rs +++ b/crates/shirabe-semver/src/constraint/constraint.rs @@ -131,26 +131,17 @@ impl Constraint { if self.version.starts_with("dev-") { if Self::OP_EQ == self.operator { if Self::OP_EQ == other_operator { - return format!( - "$b && $v === {}", - php::var_export_str(&self.version, true) - ); + return format!("$b && $v === {}", php::var_export_str(&self.version, true)); } if Self::OP_NE == other_operator { - return format!( - "!$b || $v !== {}", - php::var_export_str(&self.version, true) - ); + return format!("!$b || $v !== {}", php::var_export_str(&self.version, true)); } return "false".to_string(); } if Self::OP_NE == self.operator { if Self::OP_EQ == other_operator { - return format!( - "!$b || $v !== {}", - php::var_export_str(&self.version, true) - ); + return format!("!$b || $v !== {}", php::var_export_str(&self.version, true)); } if Self::OP_NE == other_operator { return "true".to_string(); @@ -276,7 +267,12 @@ impl Constraint { }; if self - .version_compare(version1, version2, Self::trans_op_int(operator), compare_branches) + .version_compare( + version1, + version2, + Self::trans_op_int(operator), + compare_branches, + ) .expect("valid operator") { return !(Self::trans_op_int(provider.operator) == provider_no_equal_op diff --git a/crates/shirabe-semver/src/constraint/multi_constraint.rs b/crates/shirabe-semver/src/constraint/multi_constraint.rs index ff7fbfb..86afa8d 100644 --- a/crates/shirabe-semver/src/constraint/multi_constraint.rs +++ b/crates/shirabe-semver/src/constraint/multi_constraint.rs @@ -108,7 +108,9 @@ impl MultiConstraint { conjunctive: bool, ) -> anyhow::Result<Box<dyn ConstraintInterface>> { if constraints.is_empty() { - return Ok(Box::new(MatchAllConstraint { pretty_string: None })); + return Ok(Box::new(MatchAllConstraint { + pretty_string: None, + })); } if constraints.len() == 1 { @@ -170,7 +172,8 @@ impl MultiConstraint { true, ) .unwrap(), - ) as Box<dyn ConstraintInterface>) + ) + as Box<dyn ConstraintInterface>) } else { None } @@ -280,11 +283,7 @@ impl ConstraintInterface for MultiConstraint { return s.clone(); } - let parts: Vec<String> = self - .constraints - .iter() - .map(|c| c.__to_string()) - .collect(); + let parts: Vec<String> = self.constraints.iter().map(|c| c.__to_string()).collect(); let sep = if self.conjunctive { " " } else { " || " }; let result = format!("[{}]", parts.join(sep)); diff --git a/crates/shirabe-semver/src/intervals.rs b/crates/shirabe-semver/src/intervals.rs index 9da98de..be7466e 100644 --- a/crates/shirabe-semver/src/intervals.rs +++ b/crates/shirabe-semver/src/intervals.rs @@ -45,21 +45,29 @@ impl Intervals { candidate: &dyn ConstraintInterface, constraint: &dyn ConstraintInterface, ) -> anyhow::Result<bool> { - if constraint.as_any().downcast_ref::<MatchAllConstraint>().is_some() { + if constraint + .as_any() + .downcast_ref::<MatchAllConstraint>() + .is_some() + { return Ok(true); } - if candidate.as_any().downcast_ref::<MatchNoneConstraint>().is_some() - || constraint.as_any().downcast_ref::<MatchNoneConstraint>().is_some() + if candidate + .as_any() + .downcast_ref::<MatchNoneConstraint>() + .is_some() + || constraint + .as_any() + .downcast_ref::<MatchNoneConstraint>() + .is_some() { return Ok(false); } // Phase B: ConstraintInterface needs clone_box() to create owned copies from references. - let multi = MultiConstraint::new( - vec![candidate.clone_box(), constraint.clone_box()], - true, - )?; + let multi = + MultiConstraint::new(vec![candidate.clone_box(), constraint.clone_box()], true)?; let intersection_intervals = Self::get(&multi)?; let candidate_intervals = Self::get(candidate)?; @@ -128,7 +136,11 @@ impl Intervals { pub fn compact_constraint( constraint: &dyn ConstraintInterface, ) -> anyhow::Result<Box<dyn ConstraintInterface>> { - if constraint.as_any().downcast_ref::<MultiConstraint>().is_none() { + if constraint + .as_any() + .downcast_ref::<MultiConstraint>() + .is_none() + { return Ok(constraint.clone_box()); } @@ -137,8 +149,7 @@ impl Intervals { let mut has_numeric_match_all = false; if intervals.numeric.len() == 1 - && intervals.numeric[0].get_start().__to_string() - == Interval::from_zero().__to_string() + && intervals.numeric[0].get_start().__to_string() == Interval::from_zero().__to_string() && intervals.numeric[0].get_end().__to_string() == Interval::until_positive_infinity().__to_string() { @@ -190,10 +201,8 @@ impl Intervals { // count is 1 if entire constraint is just one != expression if un_equal_constraints.len() > 1 { - constraints.push(Box::new(MultiConstraint::new( - un_equal_constraints, - true, - )?)); + constraints + .push(Box::new(MultiConstraint::new(un_equal_constraints, true)?)); } else { constraints.push(un_equal_constraints.into_iter().next().unwrap()); } @@ -240,7 +249,9 @@ impl Intervals { if intervals.branches.names.is_empty() { if intervals.branches.exclude && has_numeric_match_all { - return Ok(Box::new(MatchAllConstraint { pretty_string: None })); + return Ok(Box::new(MatchAllConstraint { + pretty_string: None, + })); // otherwise constraint should contain a != operator and already cover this } } else { @@ -262,12 +273,11 @@ impl Intervals { // > 2.0 != dev-foo must return a conjunctive constraint if intervals.branches.exclude { if constraints.len() > 1 { - let merged: Vec<Box<dyn ConstraintInterface>> = std::iter::once( - Box::new(MultiConstraint::new(constraints, false)?) - as Box<dyn ConstraintInterface>, - ) - .chain(dev_constraints) - .collect(); + let merged: Vec<Box<dyn ConstraintInterface>> = + std::iter::once(Box::new(MultiConstraint::new(constraints, false)?) + as Box<dyn ConstraintInterface>) + .chain(dev_constraints) + .collect(); return Ok(Box::new(MultiConstraint::new(merged, true)?)); } @@ -298,7 +308,9 @@ impl Intervals { return Ok(constraints.into_iter().next().unwrap()); } - Ok(Box::new(MatchNoneConstraint { pretty_string: None })) + Ok(Box::new(MatchNoneConstraint { + pretty_string: None, + })) } pub fn get(constraint: &dyn ConstraintInterface) -> anyhow::Result<IntervalCollection> { @@ -325,7 +337,11 @@ impl Intervals { constraint: &dyn ConstraintInterface, stop_on_first_valid_interval: bool, ) -> anyhow::Result<IntervalCollection> { - if constraint.as_any().downcast_ref::<MatchAllConstraint>().is_some() { + if constraint + .as_any() + .downcast_ref::<MatchAllConstraint>() + .is_some() + { return Ok(IntervalCollection { numeric: vec![Interval::new( Interval::from_zero().clone(), @@ -335,7 +351,11 @@ impl Intervals { }); } - if constraint.as_any().downcast_ref::<MatchNoneConstraint>().is_some() { + if constraint + .as_any() + .downcast_ref::<MatchNoneConstraint>() + .is_some() + { return Ok(IntervalCollection { numeric: vec![], branches: Interval::no_dev(), @@ -560,7 +580,8 @@ impl Intervals { }); } - if op.starts_with('>') { // > & >= + if op.starts_with('>') { + // > & >= return Ok(IntervalCollection { numeric: vec![Interval::new( constraint.clone(), @@ -569,7 +590,8 @@ impl Intervals { branches: Interval::no_dev(), }); } - if op.starts_with('<') { // < & <= + if op.starts_with('<') { + // < & <= return Ok(IntervalCollection { numeric: vec![Interval::new( Interval::from_zero().clone(), diff --git a/crates/shirabe-semver/src/lib.rs b/crates/shirabe-semver/src/lib.rs new file mode 100644 index 0000000..04c3e96 --- /dev/null +++ b/crates/shirabe-semver/src/lib.rs @@ -0,0 +1,7 @@ +pub mod comparator; +pub mod compiling_matcher; +pub mod constraint; +pub mod interval; +pub mod intervals; +pub mod semver; +pub mod version_parser; diff --git a/crates/shirabe-semver/src/version_parser.rs b/crates/shirabe-semver/src/version_parser.rs index eefc65e..95149af 100644 --- a/crates/shirabe-semver/src/version_parser.rs +++ b/crates/shirabe-semver/src/version_parser.rs @@ -37,7 +37,11 @@ impl VersionParser { php::preg_match(&pattern, &lower, &mut match_); // match_[3] = the ([.-]?dev)? capture - if match_.get(3).and_then(|o| o.as_deref()).is_some_and(|s| !s.is_empty()) { + if match_ + .get(3) + .and_then(|o| o.as_deref()) + .is_some_and(|s| !s.is_empty()) + { return "dev".to_string(); } @@ -116,8 +120,10 @@ impl VersionParser { let mut matches: Vec<Option<String>> = Vec::new(); // match classical versioning - let classical_pattern = - format!("{{^v?(\\d{{1,5}}+)(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?{}$}}i", MODIFIER_REGEX); + let classical_pattern = format!( + "{{^v?(\\d{{1,5}}+)(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?{}$}}i", + MODIFIER_REGEX + ); if php::preg_match(&classical_pattern, &version, &mut matches) > 0 { let m2 = matches[2].as_deref().unwrap_or(""); let m3 = matches[3].as_deref().unwrap_or(""); @@ -137,12 +143,8 @@ impl VersionParser { MODIFIER_REGEX ); if php::preg_match(&datetime_pattern, &version, &mut matches) > 0 { - version = php::preg_replace( - "{\\D}", - ".", - matches[1].as_deref().unwrap_or(""), - ) - .unwrap_or_default(); + version = php::preg_replace("{\\D}", ".", matches[1].as_deref().unwrap_or("")) + .unwrap_or_default(); index = Some(2); } } @@ -154,7 +156,10 @@ impl VersionParser { if mi == "stable" { return Ok(version); } - let mi1 = matches.get(idx + 1).and_then(|o| o.as_deref()).unwrap_or(""); + let mi1 = matches + .get(idx + 1) + .and_then(|o| o.as_deref()) + .unwrap_or(""); version = format!( "{}-{}{}", version, @@ -203,7 +208,10 @@ impl VersionParser { &mut Vec::new(), ) > 0 { - format!(" in \"{}\", the alias must be an exact version", full_version) + format!( + " in \"{}\", the alias must be an exact version", + full_version + ) } else if php::preg_match( &format!( "{{^{}(?:@(?:{}))? +as +}}", @@ -223,7 +231,11 @@ impl VersionParser { String::new() }; - anyhow::bail!("Invalid version string \"{}\"{}", orig_version, extra_message) + anyhow::bail!( + "Invalid version string \"{}\"{}", + orig_version, + extra_message + ) } pub fn parse_numeric_alias_prefix(&self, branch: &str) -> Option<String> { @@ -288,11 +300,8 @@ impl VersionParser { ) -> anyhow::Result<Box<dyn ConstraintInterface>> { let pretty_constraint = constraints.to_string(); - let or_constraints = php::preg_split( - "{\\s*\\|\\|?\\s*}", - &php::trim(constraints, None), - ) - .ok_or_else(|| anyhow::anyhow!("Failed to preg_split string: {}", constraints))?; + let or_constraints = php::preg_split("{\\s*\\|\\|?\\s*}", &php::trim(constraints, None)) + .ok_or_else(|| anyhow::anyhow!("Failed to preg_split string: {}", constraints))?; let mut or_groups: Vec<Box<dyn ConstraintInterface>> = Vec::new(); @@ -301,22 +310,20 @@ impl VersionParser { "{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}", or_constraint, ) - .ok_or_else(|| { - anyhow::anyhow!("Failed to preg_split string: {}", or_constraint) - })?; + .ok_or_else(|| anyhow::anyhow!("Failed to preg_split string: {}", or_constraint))?; - let constraint_objects: Vec<Box<dyn ConstraintInterface>> = - if and_constraints.len() > 1 { - let mut objs: Vec<Box<dyn ConstraintInterface>> = Vec::new(); - for and_constraint in &and_constraints { - for parsed in self.parse_constraint(and_constraint)? { - objs.push(parsed); - } + let constraint_objects: Vec<Box<dyn ConstraintInterface>> = if and_constraints.len() > 1 + { + let mut objs: Vec<Box<dyn ConstraintInterface>> = Vec::new(); + for and_constraint in &and_constraints { + for parsed in self.parse_constraint(and_constraint)? { + objs.push(parsed); } - objs - } else { - self.parse_constraint(&and_constraints[0])? - }; + } + objs + } else { + self.parse_constraint(&and_constraints[0])? + }; let constraint: Box<dyn ConstraintInterface> = if constraint_objects.len() == 1 { constraint_objects.into_iter().next().unwrap() @@ -342,7 +349,12 @@ impl VersionParser { // strip off aliasing let mut match_: Vec<Option<String>> = Vec::new(); - if php::preg_match("{^([^,\\s]++) ++as ++([^,\\s]++)$}", &constraint, &mut match_) > 0 { + if php::preg_match( + "{^([^,\\s]++) ++as ++([^,\\s]++)$}", + &constraint, + &mut match_, + ) > 0 + { constraint = match_[1].clone().unwrap_or_default(); } @@ -376,8 +388,16 @@ impl VersionParser { let mut match_: Vec<Option<String>> = Vec::new(); if php::preg_match("{^(v)?[xX*](\\.[xX*])*$}i", &constraint, &mut match_) > 0 { - let m1_nonempty = !match_.get(1).and_then(|o| o.as_deref()).unwrap_or("").is_empty(); - let m2_nonempty = !match_.get(2).and_then(|o| o.as_deref()).unwrap_or("").is_empty(); + let m1_nonempty = !match_ + .get(1) + .and_then(|o| o.as_deref()) + .unwrap_or("") + .is_empty(); + let m2_nonempty = !match_ + .get(2) + .and_then(|o| o.as_deref()) + .unwrap_or("") + .is_empty(); if m1_nonempty || m2_nonempty { return Ok(vec![Box::new(Constraint::new( ">=".to_string(), @@ -385,7 +405,9 @@ impl VersionParser { )?)]); } - return Ok(vec![Box::new(MatchAllConstraint { pretty_string: None })]); + return Ok(vec![Box::new(MatchAllConstraint { + pretty_string: None, + })]); } let version_regex = format!( @@ -428,20 +450,17 @@ impl VersionParser { } // Calculate the stability suffix - let stability_suffix = - if matches[5].as_deref().unwrap_or("").is_empty() - && matches[7].as_deref().unwrap_or("").is_empty() - && matches[8].as_deref().unwrap_or("").is_empty() - { - "-dev" - } else { - "" - }; + let stability_suffix = if matches[5].as_deref().unwrap_or("").is_empty() + && matches[7].as_deref().unwrap_or("").is_empty() + && matches[8].as_deref().unwrap_or("").is_empty() + { + "-dev" + } else { + "" + }; - let low_version = self.normalize( - &format!("{}{}", &constraint[1..], stability_suffix), - None, - )?; + let low_version = + self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?; let lower_bound = Constraint::new(">=".to_string(), low_version)?; // For upper bound, we increment the position of one more significance, @@ -478,20 +497,17 @@ impl VersionParser { }; // Calculate the stability suffix - let stability_suffix = - if matches[5].as_deref().unwrap_or("").is_empty() - && matches[7].as_deref().unwrap_or("").is_empty() - && matches[8].as_deref().unwrap_or("").is_empty() - { - "-dev" - } else { - "" - }; + let stability_suffix = if matches[5].as_deref().unwrap_or("").is_empty() + && matches[7].as_deref().unwrap_or("").is_empty() + && matches[8].as_deref().unwrap_or("").is_empty() + { + "-dev" + } else { + "" + }; - let low_version = self.normalize( - &format!("{}{}", &constraint[1..], stability_suffix), - None, - )?; + let low_version = + self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?; let lower_bound = Constraint::new(">=".to_string(), low_version)?; // For upper bound, we increment the position of one more significance, @@ -538,7 +554,10 @@ impl VersionParser { ); if low_version == "0.0.0.0-dev" { - return Ok(vec![Box::new(Constraint::new("<".to_string(), high_version)?)]); + return Ok(vec![Box::new(Constraint::new( + "<".to_string(), + high_version, + )?)]); } return Ok(vec![ @@ -563,15 +582,14 @@ impl VersionParser { // matches[1]='from' string, matches[2..9]=from captures, matches[10]='to' string, // matches[11..18]=to captures, matches[19]='($)' // matches[6]=from stability, matches[8]=from dev, matches[9]=from wildcard-dev - let low_stability_suffix = - if matches[6].as_deref().unwrap_or("").is_empty() - && matches[8].as_deref().unwrap_or("").is_empty() - && matches[9].as_deref().unwrap_or("").is_empty() - { - "-dev" - } else { - "" - }; + let low_stability_suffix = if matches[6].as_deref().unwrap_or("").is_empty() + && matches[8].as_deref().unwrap_or("").is_empty() + && matches[9].as_deref().unwrap_or("").is_empty() + { + "-dev" + } else { + "" + }; let from_str = matches[1].clone().unwrap_or_default(); // matches['from'] let low_version = self.normalize(&from_str, None)?; @@ -581,9 +599,8 @@ impl VersionParser { )?; // PHP's empty() on "0" returns true, but here we only check for truly empty/missing - let empty = |x: &Option<String>| -> bool { - x.as_deref().map_or(true, |s| s.is_empty()) - }; + let empty = + |x: &Option<String>| -> bool { x.as_deref().map_or(true, |s| s.is_empty()) }; // matches[12]=to minor, matches[13]=to patch, matches[15]=to stability, // matches[17]=to dev, matches[18]=to wildcard-dev @@ -636,11 +653,8 @@ impl VersionParser { // dev-foobar except if the constraint uses a known operator, in which // case it must be a parse error if version_str.ends_with("-dev") - && php::preg_match( - "{^[0-9a-zA-Z-./]+$}", - &version_str, - &mut Vec::new(), - ) > 0 + && php::preg_match("{^[0-9a-zA-Z-./]+$}", &version_str, &mut Vec::new()) + > 0 { self.normalize( &format!("dev-{}", &version_str[..version_str.len() - 4]), @@ -697,10 +711,30 @@ impl VersionParser { ) -> Option<String> { let mut parts: [i64; 5] = [ 0, - matches.get(1).and_then(|o| o.as_deref()).unwrap_or("0").parse().unwrap_or(0), - matches.get(2).and_then(|o| o.as_deref()).unwrap_or("0").parse().unwrap_or(0), - matches.get(3).and_then(|o| o.as_deref()).unwrap_or("0").parse().unwrap_or(0), - matches.get(4).and_then(|o| o.as_deref()).unwrap_or("0").parse().unwrap_or(0), + matches + .get(1) + .and_then(|o| o.as_deref()) + .unwrap_or("0") + .parse() + .unwrap_or(0), + matches + .get(2) + .and_then(|o| o.as_deref()) + .unwrap_or("0") + .parse() + .unwrap_or(0), + matches + .get(3) + .and_then(|o| o.as_deref()) + .unwrap_or("0") + .parse() + .unwrap_or(0), + matches + .get(4) + .and_then(|o| o.as_deref()) + .unwrap_or("0") + .parse() + .unwrap_or(0), ]; let pad_val: i64 = pad.parse().unwrap_or(0); let mut position = position; @@ -722,7 +756,10 @@ impl VersionParser { } } - Some(format!("{}.{}.{}.{}", parts[1], parts[2], parts[3], parts[4])) + Some(format!( + "{}.{}.{}.{}", + parts[1], parts[2], parts[3], parts[4] + )) } fn expand_stability(&self, stability: &str) -> String { |
