use crate::qr::Symbol;
use crate::reply::enrolled::Enrolled;
use super::Aim;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Grade {
#[default]
Operator,
Foot,
}
impl Grade {
pub fn word(self) -> String {
match self {
Self::Operator => "operator",
Self::Foot => "foot",
}
.to_owned()
}
pub fn both() -> Vec<Self> {
vec![Self::Operator, Self::Foot]
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Shown {
pub caption: String,
pub symbol: Symbol,
}
impl Shown {
pub fn of(material: &Enrolled) -> Result<Self, String> {
Symbol::encode(material.envelope().as_bytes())
.map(|symbol| Self {
caption: material.caption(),
symbol,
})
.map_err(|too_long| {
format!("the engine's material will not fit one symbol: {too_long}")
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Enrolling {
pub aim: Aim,
pub name: String,
pub grade: Grade,
pub posted: bool,
pub shown: Option<Shown>,
}
impl Enrolling {
pub fn at(aim: Aim) -> Self {
Self {
aim,
name: String::new(),
grade: Grade::default(),
posted: false,
shown: None,
}
}
pub fn ready(&self) -> bool {
!self.name.trim().is_empty() && !self.posted && self.shown.is_none()
}
pub fn minting(&self) -> bool {
self.posted && self.shown.is_none()
}
pub fn gesture(&self) -> serde_json::Value {
crate::verbs::enroll(
self.aim.address.clone(),
self.name.trim().to_owned(),
self.grade.word(),
)
}
}