use super::super::GenerationStateAccess;
use crate::{
capability::StateStore, transform::TransformError, transform::identity::IdentityTarget,
};
use std::time::SystemTime;
#[derive(Clone)]
pub(super) struct StateBinding {
target: IdentityTarget,
conversation_key: String,
expires_at: SystemTime,
}
impl StateBinding {
pub fn new<S: StateStore>(state: &GenerationStateAccess<'_, S>) -> Self {
Self {
target: state.target.clone(),
conversation_key: state.conversation_key.clone(),
expires_at: state.expires_at,
}
}
pub fn check<S: StateStore>(
&self,
state: &GenerationStateAccess<'_, S>,
) -> Result<(), TransformError> {
if state.target != self.target
|| state.conversation_key != self.conversation_key
|| state.expires_at != self.expires_at
{
return Err(super::conflict(
"invocation is bound to another state authority",
));
}
Ok(())
}
pub fn serves<S: StateStore>(&self, state: &GenerationStateAccess<'_, S>) -> bool {
state.conversation_key == self.conversation_key && state.now < self.expires_at
}
}