summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-06-29 09:31:38 +0200
committerMatthias Beyer <matthias.beyer@atos.net>2021-09-16 12:32:21 +0200
commitf21ae0da2d88eac45636508bf9244189ba563c3a (patch)
treec079f78e3940570b5b460f3d91f992a1c476edde
parent2224aa1061d01f4af1bf239a3e3739aa3d9493f1 (diff)
Add trait to match conditions of dependencies
Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r--src/package/dependency/condition.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/package/dependency/condition.rs b/src/package/dependency/condition.rs
index c9b934a..bcbde05 100644
--- a/src/package/dependency/condition.rs
+++ b/src/package/dependency/condition.rs
@@ -247,6 +247,42 @@ pub struct ConditionData<'a> {
pub(crate) env: &'a [(EnvironmentVariableName, String)],
}
+/// Trait for all things that have a condition that can be checked against ConditionData.
+///
+/// To be implemented by dependency types.
+///
+/// # Return value
+///
+/// Ok(true) if the dependency is relevant, considering the ConditionData
+/// Ok(false) if the dependency should be ignored, considering the ConditionData
+/// Err(_) if the condition checking failed (see `Condition::matches`)
+///
+pub trait ConditionCheckable {
+ fn check_condition(&self, data: &ConditionData<'_>) -> Result<bool>;
+}
+
+impl ConditionCheckable for crate::package::BuildDependency {
+ fn check_condition(&self, data: &ConditionData<'_>) -> Result<bool> {
+ match self {
+ // If the dependency is a simple one, e.g. "foo =1.2.3", there is no condition, so the
+ // dependency has always to be used
+ crate::package::BuildDependency::Simple(_) => Ok(true),
+ crate::package::BuildDependency::Conditional { condition, .. } => condition.matches(data),
+ }
+ }
+}
+
+impl ConditionCheckable for crate::package::Dependency {
+ fn check_condition(&self, data: &ConditionData<'_>) -> Result<bool> {
+ match self {
+ // If the dependency is a simple one, e.g. "foo =1.2.3", there is no condition, so the
+ // dependency has always to be used
+ crate::package::Dependency::Simple(_) => Ok(true),
+ crate::package::Dependency::Conditional { condition, .. } => condition.matches(data),
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;