use super::Area;
use super::human::{self, Place, Placed};
use super::json::{self, Status};
use crate::protection::Protection;
use crate::render::Deferred;
use crate::report::Row;
pub(super) struct Report {
pub problems: Vec<(Area, String)>,
pub out: Deferred,
pub rows: Vec<json::Row>,
pub human: Vec<Placed>,
pub agents: Vec<Protection>,
}
impl Report {
pub fn new(out: Deferred) -> Self {
Self {
problems: Vec::new(),
out,
rows: Vec::new(),
human: Vec::new(),
agents: Vec::new(),
}
}
pub fn line(&mut self, area: Area, ok: bool, text: String) {
let status = if ok { Status::Ok } else { Status::Problem };
self.push(status, Some(area), &text, None, None);
self.problem(area, ok, text);
}
pub fn line_as(&mut self, area: Area, ok: bool, text: String, place: Place, row: Row) {
let status = if ok { Status::Ok } else { Status::Problem };
self.push(status, Some(area), &text, Some(place), Some(row));
self.problem(area, ok, text);
}
fn problem(&mut self, area: Area, ok: bool, text: String) {
if !ok {
self.problems.push((area, text));
}
}
pub fn note(&mut self, text: &str) {
self.push(Status::Note, None, text, None, None);
}
pub fn note_at(&mut self, text: &str, place: Place) {
self.push(Status::Note, None, text, Some(place), None);
}
pub fn warn(&mut self, text: &str) {
self.push(Status::Warning, None, text, None, None);
}
pub fn warn_at(&mut self, text: &str, place: Place) {
self.push(Status::Warning, None, text, Some(place), None);
}
pub fn warn_as(&mut self, text: &str, row: Row) {
self.push(
Status::Warning,
None,
text,
Some(Place::Attention),
Some(row),
);
}
pub fn human(&mut self, place: Place, row: Row) {
self.human.push(Placed { place, row });
}
fn push(
&mut self,
status: Status,
area: Option<Area>,
text: &str,
place: Option<Place>,
row: Option<Row>,
) {
let json = json::Row::new(status, area, text);
let place = place.unwrap_or_else(|| human::default_place(status, area, &json.name));
let row = row.unwrap_or_else(|| human::default_row(status, &json, place));
self.human.push(Placed { place, row });
if place != Place::Details {
let details = human::default_row(status, &json, Place::Details);
self.human.push(Placed {
place: Place::Details,
row: details,
});
}
self.rows.push(json);
}
}