#[derive(Debug, Clone, PartialEq)]
pub enum AccountStatus {
NoKeypair,
Unfunded { id: String, shortfall_algos: f64 },
Funded { id: String },
Active {
id: String,
handle: String,
balance_algos: f64,
operating_min_algos: f64,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialGap {
NoAccount,
FundedNeedsHandle,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StartupDecision {
Proceed { handle: String },
Register { handle: String },
Fund { id: String, needed_algos: f64 },
NeedCredentials { gap: CredentialGap },
HandleMismatch { existing: String, supplied: String },
}
pub fn decide_startup(
status: &AccountStatus,
cli_handle: Option<&str>,
have_passphrase: bool,
) -> StartupDecision {
match status {
AccountStatus::NoKeypair => match (have_passphrase, cli_handle) {
(true, Some(handle)) => StartupDecision::Register {
handle: handle.to_string(),
},
_ => StartupDecision::NeedCredentials {
gap: CredentialGap::NoAccount,
},
},
AccountStatus::Unfunded {
id,
shortfall_algos,
} => StartupDecision::Fund {
id: id.clone(),
needed_algos: *shortfall_algos,
},
AccountStatus::Funded { id: _ } => match cli_handle {
Some(handle) => StartupDecision::Register {
handle: handle.to_string(),
},
None => StartupDecision::NeedCredentials {
gap: CredentialGap::FundedNeedsHandle,
},
},
AccountStatus::Active {
id,
handle,
balance_algos,
operating_min_algos,
} => {
if let Some(supplied) = cli_handle
&& supplied != handle
{
return StartupDecision::HandleMismatch {
existing: handle.clone(),
supplied: supplied.to_string(),
};
}
if balance_algos < operating_min_algos {
return StartupDecision::Fund {
id: id.clone(),
needed_algos: operating_min_algos - balance_algos,
};
}
StartupDecision::Proceed {
handle: handle.clone(),
}
}
}
}