mirror of
https://github.com/aljazceru/cdk.git
synced 2026-01-23 23:05:52 +01:00
40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
//! State transition rules
|
|
|
|
use cashu::State;
|
|
|
|
/// State transition Error
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum Error {
|
|
/// Pending Token
|
|
#[error("Token already pending for another update")]
|
|
Pending,
|
|
/// Already spent
|
|
#[error("Token already spent")]
|
|
AlreadySpent,
|
|
/// Invalid transition
|
|
#[error("Invalid transition: From {0} to {1}")]
|
|
InvalidTransition(State, State),
|
|
}
|
|
|
|
#[inline]
|
|
/// Check if the state transition is allowed
|
|
pub fn check_state_transition(current_state: State, new_state: State) -> Result<(), Error> {
|
|
let is_valid_transition = match current_state {
|
|
State::Unspent => matches!(new_state, State::Pending | State::Spent),
|
|
State::Pending => matches!(new_state, State::Unspent | State::Spent),
|
|
// Any other state shouldn't be updated by the mint, and the wallet does not use this
|
|
// function
|
|
_ => false,
|
|
};
|
|
|
|
if !is_valid_transition {
|
|
Err(match current_state {
|
|
State::Pending => Error::Pending,
|
|
State::Spent => Error::AlreadySpent,
|
|
_ => Error::InvalidTransition(current_state, new_state),
|
|
})
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|