semver_php/constraint/
match_all.rs

1use super::{Bound, Constraint};
2use std::fmt;
3
4/// A constraint that matches all versions (wildcard *).
5#[derive(Debug, Clone, Default)]
6pub struct MatchAllConstraint {
7	pretty_string: Option<String>,
8}
9
10impl MatchAllConstraint {
11	/// Create a new match-all constraint.
12	#[must_use]
13	pub fn new() -> Self {
14		Self::default()
15	}
16}
17
18impl Constraint for MatchAllConstraint {
19	fn matches(&self, _other: &dyn Constraint) -> bool {
20		// Match-all always returns true
21		true
22	}
23
24	fn lower_bound(&self) -> Bound {
25		Bound::zero()
26	}
27
28	fn upper_bound(&self) -> Bound {
29		Bound::positive_infinity()
30	}
31
32	fn is_match_all(&self) -> bool {
33		true
34	}
35
36	fn set_pretty_string(&mut self, pretty: String) {
37		self.pretty_string = Some(pretty);
38	}
39
40	fn pretty_string(&self) -> String {
41		self.pretty_string
42			.clone()
43			.unwrap_or_else(|| self.to_string())
44	}
45}
46
47impl fmt::Display for MatchAllConstraint {
48	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49		write!(f, "*")
50	}
51}
52
53#[cfg(test)]
54mod tests {
55	use super::*;
56	use crate::constraint::{Operator, SingleConstraint};
57
58	#[test]
59	fn test_match_all_matches_everything() {
60		let match_all = MatchAllConstraint::new();
61
62		let eq = SingleConstraint::new(Operator::Eq, "1.0.0");
63		assert!(match_all.matches(&eq));
64
65		let lt = SingleConstraint::new(Operator::Lt, "2.0.0");
66		assert!(match_all.matches(&lt));
67	}
68
69	#[test]
70	fn test_match_all_bounds() {
71		let match_all = MatchAllConstraint::new();
72		assert!(match_all.lower_bound().is_zero());
73		assert!(match_all.upper_bound().is_positive_infinity());
74	}
75
76	#[test]
77	fn test_is_match_all() {
78		let match_all = MatchAllConstraint::new();
79		assert!(match_all.is_match_all());
80		assert!(!match_all.is_match_none());
81	}
82}