diff options
Diffstat (limited to 'crates/shirabe-semver/src/constraint')
3 files changed, 26 insertions, 31 deletions
diff --git a/crates/shirabe-semver/src/constraint/constraint.rs b/crates/shirabe-semver/src/constraint/constraint.rs index f8ff04e..1435139 100644 --- a/crates/shirabe-semver/src/constraint/constraint.rs +++ b/crates/shirabe-semver/src/constraint/constraint.rs @@ -60,22 +60,25 @@ impl Constraint { } } - pub fn new(operator: String, version: String) -> anyhow::Result<Self> { - let op_int = Self::trans_op_str(&operator).ok_or_else(|| { - anyhow::anyhow!( + pub fn new(operator: impl Into<String>, version: impl Into<String>) -> Self { + let operator: String = operator.into(); + let op_int = Self::trans_op_str(&operator).unwrap_or_else(|| { + // PHP raises InvalidArgumentException; in the Rust port keep that as a panic + // because invalid operators are programmer errors caught during porting. + panic!( "Invalid operator \"{}\" given, expected one of: {}", operator, Self::get_supported_operators().join(", ") ) - })?; + }); - Ok(Self { + Self { operator: op_int, - version, + version: version.into(), pretty_string: None, lower_bound: Mutex::new(None), upper_bound: Mutex::new(None), - }) + } } pub fn get_version(&self) -> &str { diff --git a/crates/shirabe-semver/src/constraint/constraint_interface.rs b/crates/shirabe-semver/src/constraint/constraint_interface.rs index f8879b3..09536e6 100644 --- a/crates/shirabe-semver/src/constraint/constraint_interface.rs +++ b/crates/shirabe-semver/src/constraint/constraint_interface.rs @@ -2,7 +2,7 @@ use crate::constraint::bound::Bound; -pub trait ConstraintInterface { +pub trait ConstraintInterface: std::fmt::Debug { fn matches(&self, provider: &dyn ConstraintInterface) -> bool; fn compile(&self, other_operator: i64) -> String; diff --git a/crates/shirabe-semver/src/constraint/multi_constraint.rs b/crates/shirabe-semver/src/constraint/multi_constraint.rs index 04e262b..5596cc4 100644 --- a/crates/shirabe-semver/src/constraint/multi_constraint.rs +++ b/crates/shirabe-semver/src/constraint/multi_constraint.rs @@ -2,7 +2,6 @@ use std::cell::RefCell; -use anyhow::bail; use crate::constraint::bound::Bound; use crate::constraint::constraint_interface::ConstraintInterface; @@ -26,26 +25,22 @@ impl std::fmt::Debug for MultiConstraint { } impl MultiConstraint { - pub fn new( - constraints: Vec<Box<dyn ConstraintInterface>>, - conjunctive: bool, - ) -> anyhow::Result<Self> { - if constraints.len() < 2 { - bail!( - "Must provide at least two constraints for a MultiConstraint. Use \ + pub fn new(constraints: Vec<Box<dyn ConstraintInterface>>, conjunctive: bool) -> Self { + assert!( + constraints.len() >= 2, + "Must provide at least two constraints for a MultiConstraint. Use \ the regular Constraint class for one constraint only or MatchAllConstraint for none. You may use \ MultiConstraint::create() which optimizes and handles those cases automatically." - ); - } + ); - Ok(Self { + Self { constraints, pretty_string: None, string: RefCell::new(None), conjunctive, lower_bound: RefCell::new(None), upper_bound: RefCell::new(None), - }) + } } pub fn get_constraints(&self) -> &[Box<dyn ConstraintInterface>] { @@ -123,7 +118,7 @@ impl MultiConstraint { return Ok(constraints.into_iter().next().unwrap()); } - Ok(Box::new(MultiConstraint::new(constraints, conjunctive)?)) + Ok(Box::new(MultiConstraint::new(constraints, conjunctive))) } // Returns the (possibly optimized) constraints and the effective conjunctive flag. @@ -163,16 +158,13 @@ impl MultiConstraint { && right1.starts_with('<') && left1.get(2..) == right0.get(3..) { - Some(Box::new( - MultiConstraint::new( - vec![ - l_mc.constraints[0].clone_box(), - r_mc.constraints[1].clone_box(), - ], - true, - ) - .unwrap(), - ) + Some(Box::new(MultiConstraint::new( + vec![ + l_mc.constraints[0].clone_box(), + r_mc.constraints[1].clone_box(), + ], + true, + )) as Box<dyn ConstraintInterface>) } else { None |
