use std::collections::HashSet;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Identity {
principal: String,
scopes: ScopeSet,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ScopeSet(HashSet<String>);
impl std::hash::Hash for ScopeSet {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let mut sorted: Vec<&String> = self.0.iter().collect();
sorted.sort();
for scope in sorted {
scope.hash(state);
}
}
}
impl Identity {
pub fn new(value: impl Into<String>) -> Self {
Self {
principal: value.into(),
scopes: ScopeSet::default(),
}
}
pub fn with_scopes(value: impl Into<String>, scopes: HashSet<String>) -> Self {
Self {
principal: value.into(),
scopes: ScopeSet(scopes),
}
}
pub fn anonymous() -> Self {
Self {
principal: "anonymous".into(),
scopes: ScopeSet::default(),
}
}
pub fn as_str(&self) -> &str {
&self.principal
}
pub fn is_anonymous(&self) -> bool {
self.principal == "anonymous"
}
pub fn scopes(&self) -> &HashSet<String> {
&self.scopes.0
}
pub fn has_scope(&self, scope: &str) -> bool {
self.scopes.0.contains(scope)
}
}