fabaccess-bffh/bffhd/resources/modules/fabaccess.rs

144 lines
3.6 KiB
Rust
Raw Permalink Normal View History

2022-06-18 16:52:22 +02:00
use crate::config::deser_option;
2022-03-13 17:29:21 +01:00
use crate::utils::oid::ObjectIdentifier;
use once_cell::sync::Lazy;
2022-03-15 20:00:43 +01:00
use rkyv::{Archive, Archived, Deserialize, Infallible};
2022-05-05 15:50:44 +02:00
use std::fmt;
use std::fmt::Write;
2022-03-13 17:29:21 +01:00
use std::str::FromStr;
2022-04-21 23:02:58 +02:00
//use crate::oidvalue;
2022-05-05 15:50:44 +02:00
use crate::resources::state::State;
2022-03-15 20:00:43 +01:00
2022-03-15 17:52:47 +01:00
use crate::users::UserRef;
2022-03-13 17:29:21 +01:00
/// Status of a Machine
#[derive(
Clone,
PartialEq,
Eq,
Debug,
Archive,
rkyv::Serialize,
rkyv::Deserialize,
serde::Serialize,
serde::Deserialize,
)]
2022-03-13 22:50:37 +01:00
#[archive_attr(derive(Debug, PartialEq))]
2022-03-13 17:29:21 +01:00
pub enum Status {
/// Not currently used by anybody
Free,
/// Used by somebody
2022-03-15 17:52:47 +01:00
InUse(UserRef),
2022-03-13 17:29:21 +01:00
/// Was used by somebody and now needs to be checked for cleanliness
2022-03-15 17:52:47 +01:00
ToCheck(UserRef),
2022-03-13 17:29:21 +01:00
/// Not used by anybody but also can not be used. E.g. down for maintenance
2022-03-15 17:52:47 +01:00
Blocked(UserRef),
2022-03-13 17:29:21 +01:00
/// Disabled for some other reason
Disabled,
/// Reserved
2022-03-15 17:52:47 +01:00
Reserved(UserRef),
2022-03-13 17:29:21 +01:00
}
#[derive(
Clone,
PartialEq,
Eq,
Debug,
Archive,
rkyv::Serialize,
rkyv::Deserialize,
serde::Serialize,
serde::Deserialize,
)]
2022-03-13 20:11:37 +01:00
#[archive_attr(derive(Debug, PartialEq))]
2022-03-13 17:29:21 +01:00
/// The status of the machine
pub struct MachineState {
pub state: Status,
2022-06-18 16:52:22 +02:00
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deser_option"
)]
2022-03-15 17:52:47 +01:00
pub previous: Option<UserRef>,
2022-03-13 17:29:21 +01:00
}
2022-03-20 22:46:04 +01:00
impl fmt::Display for ArchivedMachineState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.state {
ArchivedStatus::Free => f.write_str("free"),
ArchivedStatus::InUse(user) => write!(f, "inuse {}", user),
ArchivedStatus::ToCheck(user) => write!(f, "tocheck {}", user),
ArchivedStatus::Blocked(user) => write!(f, "blocked {}", user),
ArchivedStatus::Disabled => f.write_str("disabled"),
ArchivedStatus::Reserved(user) => write!(f, "reserved {}", user),
}
}
}
2022-03-13 17:29:21 +01:00
impl MachineState {
pub fn new() -> Self {
Self {
state: Status::Free,
2022-03-13 20:11:37 +01:00
previous: None,
2022-03-13 17:29:21 +01:00
}
}
2022-03-13 20:11:37 +01:00
pub fn from(dbstate: &Archived<State>) -> Self {
let state: &Archived<MachineState> = &dbstate.inner;
Deserialize::deserialize(state, &mut Infallible).unwrap()
}
pub fn to_state(&self) -> State {
State {
2022-05-05 15:50:44 +02:00
inner: self.clone(),
2022-03-13 20:11:37 +01:00
}
2022-03-13 17:29:21 +01:00
}
2022-03-15 17:52:47 +01:00
pub fn free(previous: Option<UserRef>) -> Self {
2022-03-13 17:29:21 +01:00
Self {
state: Status::Free,
2022-03-13 20:11:37 +01:00
previous,
2022-03-13 17:29:21 +01:00
}
}
2022-03-15 17:52:47 +01:00
pub fn used(user: UserRef, previous: Option<UserRef>) -> Self {
2022-03-13 17:29:21 +01:00
Self {
state: Status::InUse(user),
2022-03-13 20:11:37 +01:00
previous,
2022-03-13 17:29:21 +01:00
}
}
2022-03-15 17:52:47 +01:00
pub fn blocked(user: UserRef, previous: Option<UserRef>) -> Self {
2022-03-13 17:29:21 +01:00
Self {
state: Status::Blocked(user),
2022-03-13 20:11:37 +01:00
previous,
2022-03-13 17:29:21 +01:00
}
}
2022-03-15 17:52:47 +01:00
pub fn disabled(previous: Option<UserRef>) -> Self {
2022-03-13 17:29:21 +01:00
Self {
state: Status::Disabled,
2022-03-13 20:11:37 +01:00
previous,
2022-03-13 17:29:21 +01:00
}
}
2022-03-15 17:52:47 +01:00
pub fn reserved(user: UserRef, previous: Option<UserRef>) -> Self {
2022-03-13 17:29:21 +01:00
Self {
state: Status::Reserved(user),
2022-03-13 20:11:37 +01:00
previous,
2022-03-13 17:29:21 +01:00
}
}
2022-03-15 17:52:47 +01:00
pub fn check(user: UserRef) -> Self {
2022-03-13 17:29:21 +01:00
Self {
2022-03-13 22:50:37 +01:00
state: Status::ToCheck(user.clone()),
2022-03-13 20:11:37 +01:00
previous: Some(user),
2022-03-13 17:29:21 +01:00
}
}
}
2022-03-13 20:11:37 +01:00
pub static OID_TYPE: Lazy<ObjectIdentifier> =
2022-03-13 17:29:21 +01:00
Lazy::new(|| ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.14").unwrap());
2022-03-13 20:11:37 +01:00
pub static OID_VALUE: Lazy<ObjectIdentifier> =
2022-03-13 17:29:21 +01:00
Lazy::new(|| ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.4").unwrap());
2022-04-21 23:02:58 +02:00
//oidvalue!(OID_TYPE, MachineState, ArchivedMachineState);