use super::*;
impl TokenState {
pub fn can_transition_to(self, target: TokenState) -> bool {
use TokenState::*;
matches!(
(self, target),
(Issued, Active)
| (Active, Active)
| (Active, Expired)
| (Active, Revoked)
| (Active, Refreshed)
| (Refreshed, Revoked)
)
}
pub fn transition_to(self, target: TokenState) -> GarrisonResult<TokenState> {
if self.can_transition_to(target) {
Ok(target)
} else {
Err(GarrisonError::InvalidStateTransition {
from: format!("{:?}", self),
to: format!("{:?}", target),
})
}
}
}
impl std::fmt::Display for TokenState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use TokenState::*;
match self {
Issued => write!(f, "ISSUED"),
Active => write!(f, "ACTIVE"),
Expired => write!(f, "EXPIRED"),
Revoked => write!(f, "REVOKED"),
Refreshed => write!(f, "REFRESHED"),
}
}
}
impl UserStatus {
pub fn can_transition_to(self, target: UserStatus) -> bool {
use UserStatus::*;
matches!(
(self, target),
(Pending, Active)
| (Pending, Suspended)
| (Active, Suspended)
| (Active, Inactive)
| (Active, Deleted)
| (Suspended, Active)
| (Suspended, Deleted)
| (Inactive, Active)
| (Inactive, Deleted)
)
}
pub fn transition_to(self, target: UserStatus) -> GarrisonResult<UserStatus> {
if self.can_transition_to(target) {
Ok(target)
} else {
Err(GarrisonError::InvalidStateTransition {
from: format!("{:?}", self),
to: format!("{:?}", target),
})
}
}
}
impl std::fmt::Display for UserStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use UserStatus::*;
match self {
Pending => write!(f, "PENDING"),
Active => write!(f, "ACTIVE"),
Suspended => write!(f, "SUSPENDED"),
Inactive => write!(f, "INACTIVE"),
Deleted => write!(f, "DELETED"),
}
}
}