use crate::evaluator::Value;
use crate::messages;
use crate::model::{ClarDefinition, ClarItem};
use crate::path::{ClarPath, IntoClarPath};
use antex::{ColorMode, Text};
#[derive(Debug)]
pub struct ClarMatches {
app: String,
description: Option<String>,
values: Vec<Value>,
definition: ClarDefinition,
cm: ColorMode,
}
impl ClarMatches {
pub(crate) fn new(app: String, description: Option<String>, values: Vec<Value>, definition: ClarDefinition, cm: ColorMode) -> Self {
ClarMatches {
app,
description,
values,
definition,
cm,
}
}
pub fn is_present(&self, path: impl IntoClarPath) -> bool {
let path: ClarPath = path.into_clar_path();
self.values.iter().any(|value| path.eq(value.path()) && value.is_present())
}
pub fn is_short(&self, path: impl IntoClarPath) -> bool {
let path: ClarPath = path.into_clar_path();
self
.values
.iter()
.any(|v| matches!(v, Value::ShortOption(option_path, _) if path.eq(option_path)))
}
pub fn is_long(&self, path: impl IntoClarPath) -> bool {
let path: ClarPath = path.into_clar_path();
self
.values
.iter()
.any(|v| matches!(v, Value::LongOption(option_path, _) if path.eq(option_path)))
}
pub fn get_count(&self, path: impl IntoClarPath) -> usize {
let path: ClarPath = path.into_clar_path();
self.values.iter().filter(|value| path.eq(value.path()) && value.is_present()).count()
}
pub fn get_values(&self, path: impl IntoClarPath) -> Vec<Option<String>> {
let path: ClarPath = path.into_clar_path();
let mut values = self
.values
.iter()
.filter(|v| path.eq(v.path()))
.flat_map(|v| v.value())
.collect::<Vec<Option<String>>>();
println!("DDD: c = {}", values.is_empty());
if values.is_empty()
&& let value @ Some(_) = self.default_value(&path)
{
values.push(value);
}
values
}
pub fn get_first_value(&self, path: impl IntoClarPath) -> Option<String> {
println!("DDD: b");
self.get_values(path).first().cloned().flatten()
}
pub fn get_help(&self) -> Text {
messages::get_help_text(&self.app, &self.description, &self.definition, false, self.cm)
}
pub fn get_help_long(&self) -> Text {
messages::get_help_text(&self.app, &self.description, &self.definition, true, self.cm)
}
pub fn get_help_command(&self, path: impl IntoClarPath) -> Text {
messages::get_help_text_for_command(&self.app, &path.into_clar_path(), &self.definition, false, self.cm)
}
pub fn get_help_long_command(&self, path: impl IntoClarPath) -> Text {
messages::get_help_text_for_command(&self.app, &path.into_clar_path(), &self.definition, true, self.cm)
}
fn default_value(&self, path: &ClarPath) -> Option<String> {
println!("DDD: a");
for item in self.definition.items() {
match item {
ClarItem::Options(options) => {
for option in options {
if option.get_path() == path && option.get_takes_value().is_some() {
return option.get_default_value().clone();
}
}
}
ClarItem::Arguments(arguments) => {
for argument in arguments {
if argument.get_path() == path {
return argument.get_default_value().clone();
}
}
}
_ => {}
}
}
None
}
}