use std::collections::HashSet;
use crate::Error;
#[derive(Debug)]
pub struct SummaryOptions {
human: bool,
condensed: bool,
show_domain_names: bool,
}
impl SummaryOptions {
pub fn new(human: bool, condensed: bool, show_domain_names: bool) -> SummaryOptions {
SummaryOptions {
human,
condensed,
show_domain_names,
}
}
pub fn human(&self) -> bool {
self.human
}
pub fn condensed(&self) -> bool {
self.condensed
}
pub fn show_domain_names(&self) -> bool {
self.show_domain_names
}
}
impl Default for SummaryOptions {
fn default() -> Self {
SummaryOptions {
human: true,
condensed: false,
show_domain_names: false,
}
}
}
static VALID_SUMMARY_OPTIONS: &[&str] = &["human", "condensed", "show-domain-names"];
impl<'a> TryFrom<Vec<&'a str>> for SummaryOptions {
type Error = Error;
fn try_from(values: Vec<&'a str>) -> std::result::Result<Self, Self::Error> {
let options: HashSet<&str> = values.into_iter().collect();
for opt in &options {
if !VALID_SUMMARY_OPTIONS.contains(opt) {
return Err(Error::ParserError {
what: opt.to_string(),
to: "SummaryOptions",
why: format!(
"unknown option '{}', valid options are: {}",
opt,
VALID_SUMMARY_OPTIONS.join(", ")
),
});
}
}
Ok(SummaryOptions {
human: options.contains("human"),
condensed: options.contains("condensed"),
show_domain_names: options.contains("show-domain-names"),
})
}
}
pub trait Rendering {
fn render(&self, opts: &SummaryOptions) -> String;
#[allow(unused_variables)]
fn render_with_suffix(&self, suffix: &str, opts: &SummaryOptions) -> String {
self.render(opts)
}
}