Skip to content

Instantly share code, notes, and snippets.

@huntc
Last active July 22, 2022 02:25
Show Gist options
  • Save huntc/fae728d5cda7e6d9edb67d0c2e35706c to your computer and use it in GitHub Desktop.
Save huntc/fae728d5cda7e6d9edb67d0c2e35706c to your computer and use it in GitHub Desktop.
/// The entity
pub struct Configuration {}
/// The FSM's states
pub enum State {
RootKeyGenerated,
Uninitialised,
VpnKeyGenerated { configuration: Configuration },
}
/// The FSM's commands
pub enum Command {
GenerateRootKey,
GenerateVpnKey,
Reset,
GetUsername,
SetCredentials { username: String, password: String },
}
/// The FSM's events
pub enum Event {
CredentialsSet,
Reset,
RootKeyGenerated,
UsernameRetrieved { username: String },
VpnKeyGenerated,
}
/// The effect handlers
pub struct EffectHandlers<'d, R>
where
R: RngCore,
{
rng: &'d mut R,
root_key_path: &'d Path,
}
/// The FSM
pub struct Configurator<'d, R> {
phantom: PhantomData<&'d R>,
}
#[impl_fsm]
impl<'d, R> Fsm<State, Command, Event, EffectHandlers<'d, R>> for Configurator<'d, R>
where
R: RngCore + 'd,
{
state!(State::Uninitialised / exit);
transition!(State::Uninitialised => Command::GenerateRootKey => Event::RootKeyGenerated => State::RootKeyGenerated);
transition!(State::RootKeyGenerated => Command::GenerateVpnKey => Event::VpnKeyGenerated => State::VpnKeyGenerated);
transition!(State::VpnKeyGenerated => Command::SetCredentials { username, password } => Event::CredentialsSet { username } => State::CredentialsSet { entity });
transition!(_ => Command::GetUsername => Event::UsernameRetrieved);
transition!(_ => Command::Reset { factory } if factory => Event::Reset { factory } if factory => State::Uninitialised);
transition!(_ => Command::Reset { .. } => Event::Reset { .. } => State::VpnKeyGenerated);
impl<'d, R> Configurator<'d, R>
where
R: RngCore,
{
... // Command, event, entry and exit handlers in here and supplied by the programmer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment