From f21ae0da2d88eac45636508bf9244189ba563c3a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 29 Jun 2021 09:31:38 +0200 Subject: Add trait to match conditions of dependencies Signed-off-by: Matthias Beyer --- src/package/dependency/condition.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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; +} + +impl ConditionCheckable for crate::package::BuildDependency { + fn check_condition(&self, data: &ConditionData<'_>) -> Result { + 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 { + 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::*; -- cgit v1.2.3