use super::{Decided, Verdict, misplaced};
use crate::render::Form;
use crate::verbs::doors::{CEILING, Door, PRICE};
pub const ON: &str = "--on";
pub(super) fn price(provider: &str, model: &str, tail: &[&str], form: Form) -> Decided {
let (own, on) = aimed(tail);
match crate::verbs::prices::rates(tail.get(..own).unwrap_or(tail)) {
Ok(rates) => Decided::Aimed {
on,
envelope: crate::verbs::price(provider.to_owned(), model.to_owned(), rates),
form,
},
Err(why) => refused(&PRICE, why, tail),
}
}
pub(super) fn ceiling(tail: &[&str], form: Form) -> Decided {
let (own, on) = aimed(tail);
let [word] = tail.get(..own).unwrap_or(tail) else {
let why = format!("a ceiling is one bound, a number or `off`, and got {own} word(s)");
return refused(&CEILING, why, tail);
};
match crate::verbs::prices::bound(word) {
Ok(usd) => Decided::Aimed {
on,
envelope: crate::verbs::ceiling(usd),
form,
},
Err(why) => refused(&CEILING, why, tail),
}
}
fn aimed(tail: &[&str]) -> (usize, Option<String>) {
match tail {
[words @ .., word, name] if *word == ON => (words.len(), Some((*name).to_owned())),
_ => (tail.len(), None),
}
}
fn refused(door: &Door, why: String, words: &[&str]) -> Decided {
Decided::Say(Verdict::refused(misplaced(
format!("`lernie {}`: {why} — usage: {}", door.word, door.usage()),
words,
)))
}