growthbook-rust 0.2.0

Official Growthbook Rust SDK
Documentation
use crate::condition::eval_context::ConditionEvalContext;
use crate::extensions::FindGrowthBookAttribute;
use crate::model_public::{GrowthBookAttribute, GrowthBookAttributeValue};

pub struct TypeComparison;

impl TypeComparison {
    pub fn matches(
        parent_attribute: Option<&GrowthBookAttribute>,
        feature_attribute: &GrowthBookAttribute,
        ctx: &ConditionEvalContext,
    ) -> bool {
        if let GrowthBookAttributeValue::String(feature_type) = &feature_attribute.value {
            if let Some(user_value) = ctx.find_value(&parent_attribute.unwrap_or(feature_attribute).key) {
                match user_value {
                    GrowthBookAttributeValue::String(_) => feature_type == "string",
                    GrowthBookAttributeValue::Int(_) => feature_type == "number",
                    GrowthBookAttributeValue::Float(_) => feature_type == "number",
                    GrowthBookAttributeValue::Bool(_) => feature_type == "boolean",
                    GrowthBookAttributeValue::Array(_) => feature_type == "array",
                    // Any object, including the empty object `{}`, is "object".
                    GrowthBookAttributeValue::Object(_) => feature_type == "object",
                    GrowthBookAttributeValue::Empty => feature_type == "null",
                }
            } else {
                feature_type == "null"
            }
        } else {
            false
        }
    }
}