use super::{Choice, Model, Pick};
use crate::reply::providers::Place;
pub(crate) const SERVER: &str = "server";
pub(crate) fn word(machine: Option<&str>) -> &str {
machine.unwrap_or(SERVER)
}
impl Model {
fn held(&self) -> Vec<(String, Option<Place>)> {
let mut held: Vec<(String, Option<Place>)> = self
.providers
.iter()
.flatten()
.map(|row| (row.name.clone(), row.on.clone()))
.collect();
if let Some(worker) = self.worker()
&& !held.iter().any(|(name, _)| *name == worker.provider)
{
held.insert(0, (worker.provider.clone(), worker.on.clone()));
}
held
}
pub(super) fn machine_of(&self, name: &str) -> Option<String> {
let on = self.held().into_iter().find(|(held, _)| held == name)?.1;
on.map(|at| at.client)
}
pub(super) fn machine_rows(&self) -> Vec<Choice> {
let at = self.worker().map(|w| Place::machine(w.on.as_ref()));
let mut machines: Vec<Option<String>> = vec![None];
for (_, on) in self.held() {
let machine = Place::machine(on.as_ref());
if !machines.contains(&machine) {
machines.push(machine);
}
}
machines
.into_iter()
.map(|machine| Choice {
text: word(machine.as_deref()).to_owned(),
current: at.as_ref() == Some(&machine),
pick: Pick::Machine(machine),
})
.collect()
}
pub(super) fn provider_rows(&self, machine: Option<&str>) -> Vec<Choice> {
let worker = self.worker().map(|w| w.provider.as_str());
self.held()
.into_iter()
.filter(|(_, on)| Place::machine(on.as_ref()).as_deref() == machine)
.map(|(name, on)| Choice {
current: worker == Some(name.as_str()),
text: Place::shown(on.as_ref(), &name),
pick: Pick::Provider(name),
})
.collect()
}
}
#[cfg(test)]
mod tests;