|
| 1 | +// (C) Copyright IBM Corp. 2025. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | + |
| 17 | +use super::errors::CheckOperatorErrorDetail; |
| 18 | +use crate::network::serialization::{Rule, Segment}; |
| 19 | +use crate::segment_evaluation::errors::SegmentEvaluationError; |
| 20 | +use crate::segment_evaluation::rule_operator::RuleOperator; |
| 21 | +use crate::Value; |
| 22 | + |
| 23 | +pub(crate) trait MatchesAttributes { |
| 24 | + type Error; |
| 25 | + |
| 26 | + fn matches_attributes( |
| 27 | + &self, |
| 28 | + attributes: &HashMap<String, Value>, |
| 29 | + ) -> std::result::Result<bool, Self::Error>; |
| 30 | +} |
| 31 | + |
| 32 | +impl MatchesAttributes for Segment { |
| 33 | + type Error = SegmentEvaluationError; |
| 34 | + |
| 35 | + /// A [`Segment`] matches attributes iif: |
| 36 | + /// * ALL the rules match the attributes |
| 37 | + fn matches_attributes( |
| 38 | + &self, |
| 39 | + attributes: &HashMap<String, Value>, |
| 40 | + ) -> std::result::Result<bool, Self::Error> { |
| 41 | + self.rules |
| 42 | + .iter() |
| 43 | + .map(|rule| { |
| 44 | + rule.matches_attributes(attributes) |
| 45 | + .map_err(|(e, rule_value)| (e, self, rule, rule_value).into()) |
| 46 | + }) |
| 47 | + .collect::<std::result::Result<Vec<bool>, _>>() |
| 48 | + .map(|v| v.iter().all(|&x| x)) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl MatchesAttributes for Rule { |
| 53 | + type Error = (CheckOperatorErrorDetail, String); |
| 54 | + |
| 55 | + /// A [`Rule`] matches attributes iif: |
| 56 | + /// * the attributes contain the requested attribute, AND |
| 57 | + /// * the attribute satisfies ANY of the rule values. |
| 58 | + /// |
| 59 | + /// TODO: What if rules.values is empty? Now it returns false |
| 60 | + fn matches_attributes( |
| 61 | + &self, |
| 62 | + attributes: &HashMap<String, Value>, |
| 63 | + ) -> std::result::Result<bool, Self::Error> { |
| 64 | + attributes |
| 65 | + .get(&self.attribute_name) |
| 66 | + .map_or(Ok(false), |attr_value| { |
| 67 | + self.values |
| 68 | + .iter() |
| 69 | + .map(|value| { |
| 70 | + attr_value |
| 71 | + .operate(&self.operator, value) |
| 72 | + .map_err(|e| (e, value.to_owned())) |
| 73 | + }) |
| 74 | + .collect::<std::result::Result<Vec<bool>, _>>() |
| 75 | + .map(|v| v.iter().any(|&x| x)) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments