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:
StringmatchesStringIntegermatchesIntegerIpAddrmatchesIpAddr(orIpCidrwith custom matcher)- Custom types match via
AttributeTypeTrait::eq()
Cross-type matching requires custom matchers.