use std::path::Path;
use crate::cli::Verdict;
use crate::qr::Symbol;
use crate::reply::{Read, Reply};
const KEPT: &str = "not written down anywhere — scan it now, or enroll again";
pub fn enroll(data_root: &Path, workspace: &str, name: &str, grade: &str) -> Verdict {
let gesture = crate::verbs::enroll(workspace.to_owned(), name.to_owned(), grade.to_owned());
let stream = match crate::seat::sent(data_root, &gesture) {
Ok(stream) => stream,
Err(why) => return Verdict::failed(why),
};
let Some(frame) = stream.last() else {
return Verdict::failed("the engine answered nothing at all".to_owned());
};
match crate::reply::read(frame) {
Read::Answer(Reply::Enrolled(material)) => drawn(&material),
Read::Refusal(said) => Verdict::answered(said, false),
Read::Unreadable(why) => Verdict::failed(why),
Read::Answer(_) => Verdict::failed(format!(
"`enroll` was answered with something else entirely; the engine at \
{workspace:?} did not mint anything"
)),
}
}
fn drawn(material: &crate::reply::enrolled::Enrolled) -> Verdict {
let envelope = material.envelope();
match Symbol::encode(envelope.as_bytes()) {
Ok(symbol) => Verdict::ok(format!(
"{}\n\n{}\n{KEPT}",
material.caption(),
symbol.block()
)),
Err(too_long) => Verdict::failed(format!(
"the engine's material will not fit one symbol: {too_long}"
)),
}
}
#[cfg(test)]
mod tests;