Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Multi-type Attributes

ABAC attributes support six built-in types plus custom extensions. This enables type-safe matching beyond string equality – IP CIDR ranges, numeric comparisons, and domain-specific predicates.

Built-in types

#![allow(unused)]
fn main() {
use abac_rs::AttributeType;
use std::net::IpAddr;
use ipnetwork::IpNetwork;

// String (most common)
let user = AttributeType::String("alice".into());

// Integer
let age = AttributeType::Integer(25);

// Float
let risk_score = AttributeType::Float(0.85);

// IP address
let ip = AttributeType::IpAddr("10.1.2.3".parse().unwrap());

// IP CIDR range
let network = AttributeType::IpCidr("10.0.0.0/8".parse().unwrap());
}

Custom types

Implement AttributeTypeTrait for domain-specific types:

#![allow(unused)]
fn main() {
use abac_rs::AttributeTypeTrait;
use std::any::Any;
use std::sync::Arc;

struct TimeOfDay {
    hour: u8,
    minute: u8,
}

impl AttributeTypeTrait for TimeOfDay {
    fn as_any(&self) -> &dyn Any { self }
    fn eq(&self, other: &dyn AttributeTypeTrait) -> bool {
        other.as_any().downcast_ref::<TimeOfDay>()
            .map_or(false, |t| self.hour == t.hour && self.minute == t.minute)
    }
    fn hash(&self) -> u64 {
        (self.hour as u64) << 8 | (self.minute as u64)
    }
    fn clone_trait(&self) -> Arc<dyn AttributeTypeTrait> {
        Arc::new(TimeOfDay { hour: self.hour, minute: self.minute })
    }
}

// Use in rules
let time = AttributeType::Custom(Arc::new(TimeOfDay { hour: 14, minute: 30 }));
}

Type matching

Attributes match only when types are compatible:

  • String matches String
  • Integer matches Integer
  • IpAddr matches IpAddr (or IpCidr with custom matcher)
  • Custom types match via AttributeTypeTrait::eq()

Cross-type matching requires custom matchers.

See also