pub struct MultiValuedAttribute<T> { /* private fields */ }Expand description
A generic container for multi-valued SCIM attributes.
This type provides type-safe handling of multi-valued attributes with support for designating one value as primary. It enforces the SCIM constraint that at most one value can be marked as primary.
§Type Parameters
T- The type of values contained in the multi-valued attribute
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_emails = MultiValuedAttribute::new(emails)?;
assert_eq!(multi_emails.len(), 2);
assert!(multi_emails.primary().is_none());
Ok(())
}Implementations§
Source§impl<T> MultiValuedAttribute<T>
impl<T> MultiValuedAttribute<T>
Sourcepub fn new(values: Vec<T>) -> ValidationResult<Self>
pub fn new(values: Vec<T>) -> ValidationResult<Self>
Creates a new multi-valued attribute from a collection of values.
§Arguments
values- Vector of values to store
§Returns
Ok(MultiValuedAttribute<T>)- Successfully created multi-valued attributeErr(ValidationError)- If the input is invalid (e.g., empty vector)
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_emails = MultiValuedAttribute::new(emails)?;
Ok(())
}Sourcepub fn single(value: T) -> Self
pub fn single(value: T) -> Self
Creates a new multi-valued attribute with a single value.
§Arguments
value- Single value to store
§Returns
A multi-valued attribute containing the single value
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let email = EmailAddress::new_simple("user@example.com".to_string())?;
let multi_email = MultiValuedAttribute::single(email);
assert_eq!(multi_email.len(), 1);
Ok(())
}Sourcepub fn single_primary(value: T) -> Self
pub fn single_primary(value: T) -> Self
Creates a new multi-valued attribute with a single primary value.
§Arguments
value- Single value to store as primary
§Returns
A multi-valued attribute containing the single primary value
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let email = EmailAddress::new_simple("primary@example.com".to_string())?;
let multi_email = MultiValuedAttribute::single_primary(email);
assert!(multi_email.primary().is_some());
Ok(())
}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of values in the collection.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
assert_eq!(multi_attr.len(), 2);
Ok(())
}Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the collection is empty.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
let multi_attr = MultiValuedAttribute::single(
EmailAddress::new_simple("test@example.com".to_string()).unwrap()
);
assert!(!multi_attr.is_empty());Sourcepub fn primary(&self) -> Option<&T>
pub fn primary(&self) -> Option<&T>
Returns a reference to the primary value, if one is set.
§Returns
Some(&T)- Reference to the primary valueNone- No primary value is set
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?.with_primary(0)?;
if let Some(primary) = multi_attr.primary() {
println!("Primary value found");
}
Ok(())
}Sourcepub fn primary_index(&self) -> Option<usize>
pub fn primary_index(&self) -> Option<usize>
Returns the index of the primary value, if one is set.
§Returns
Some(usize)- Index of the primary valueNone- No primary value is set
Sourcepub fn values(&self) -> &[T]
pub fn values(&self) -> &[T]
Returns a reference to all values in the collection.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
for value in multi_attr.values() {
println!("Value: {:?}", value);
}
Ok(())
}Sourcepub fn with_primary(self, index: usize) -> ValidationResult<Self>
pub fn with_primary(self, index: usize) -> ValidationResult<Self>
Creates a new multi-valued attribute with the specified value set as primary.
§Arguments
index- Index of the value to set as primary
§Returns
Ok(MultiValuedAttribute<T>)- New instance with primary value setErr(ValidationError)- If the index is out of bounds
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let with_primary = multi_attr.with_primary(0)?;
assert!(with_primary.primary().is_some());
Ok(())
}Sourcepub fn without_primary(self) -> Self
pub fn without_primary(self) -> Self
Creates a new multi-valued attribute with no primary value set.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?.with_primary(0)?;
let without_primary = multi_attr.without_primary();
assert!(without_primary.primary().is_none());
Ok(())
}Sourcepub fn with_value(self, value: T) -> Self
pub fn with_value(self, value: T) -> Self
Creates a new multi-valued attribute with an additional value.
§Arguments
value- Value to add to the collection
§Returns
A new multi-valued attribute with the added value
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let new_email = EmailAddress::new_simple("personal@example.com".to_string())?;
let with_value = multi_attr.with_value(new_email);
assert_eq!(with_value.len(), 2);
Ok(())
}Sourcepub fn with_primary_value(self, value: T) -> Self
pub fn with_primary_value(self, value: T) -> Self
Creates a new multi-valued attribute with an additional primary value.
This method adds the value and sets it as the primary value.
§Arguments
value- Value to add as primary
§Returns
A new multi-valued attribute with the added primary value
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let new_email = EmailAddress::new_simple("primary@example.com".to_string())?;
let with_primary = multi_attr.with_primary_value(new_email.clone());
assert_eq!(with_primary.primary(), Some(&new_email));
Ok(())
}Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator over the values in the collection.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
for value in multi_attr.iter() {
println!("Value: {:?}", value);
}
Ok(())
}Sourcepub fn validate_single_primary(&self) -> ValidationResult<()>
pub fn validate_single_primary(&self) -> ValidationResult<()>
Validates that at most one value is marked as primary.
This method is primarily used internally to ensure invariants are maintained during construction and modification.
§Returns
Ok(())- Validation passedErr(ValidationError)- Multiple primary values detected
Sourcepub fn find<P>(&self, predicate: P) -> Option<&T>
pub fn find<P>(&self, predicate: P) -> Option<&T>
Finds the first value that matches the given predicate.
§Arguments
predicate- Function to test each value
§Returns
Some(&T)- Reference to the first matching valueNone- No value matches the predicate
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let found = multi_attr.find(|value| value.value().contains("work"));
assert!(found.is_some());
Ok(())
}Sourcepub fn filter<P>(&self, predicate: P) -> Vec<&T>
pub fn filter<P>(&self, predicate: P) -> Vec<&T>
Returns all values that satisfy the given predicate. Filters values that match the given predicate.
§Arguments
predicate- Function to test each value
§Returns
An iterator over references to matching values
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let work_values = multi_attr.filter(|v| v.value().contains("work"));
assert_eq!(work_values.len(), 1);
Ok(())
}Sourcepub fn into_values(self) -> Vec<T>
pub fn into_values(self) -> Vec<T>
Converts the multi-valued attribute into a vector of values.
This consumes the multi-valued attribute and returns the underlying vector.
§Examples
use scim_server::resource::value_objects::{MultiValuedAttribute, EmailAddress};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let emails = vec![
EmailAddress::new_simple("work@example.com".to_string())?,
EmailAddress::new_simple("personal@example.com".to_string())?,
];
let multi_attr = MultiValuedAttribute::new(emails)?;
let values_vec = multi_attr.into_values();
assert_eq!(values_vec.len(), 2);
Ok(())
}Trait Implementations§
Source§impl<T: Clone> Clone for MultiValuedAttribute<T>
impl<T: Clone> Clone for MultiValuedAttribute<T>
Source§fn clone(&self) -> MultiValuedAttribute<T>
fn clone(&self) -> MultiValuedAttribute<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more