Import current project code

This commit is contained in:
Your Name
2026-11-17 09:19:28 +02:00
parent 666ef42688
commit a09c4ba2cc
8 changed files with 251 additions and 27 deletions

View File

@@ -60,6 +60,12 @@ pub enum CliCommand {
/// Output format
#[arg(long, short = 'f')]
format: OutputFormat,
/// Filter command results (key=value)
#[arg(long, short = 'F', value_parser = parse_filter)]
filter: Vec<Filter>,
/// Add a command attribute (key=value or flag; repeatable)
#[arg(long, short = 'A', value_parser = parse_attribute)]
attributes: Vec<(String, Option<String>)>,
},
/// Run command continuosly
#[command(
@@ -142,3 +148,80 @@ pub enum OutputFormat {
Table,
Json,
}
#[derive(Debug, Clone, Copy)]
pub enum FilterOp {
Eq,
Gt,
Lt,
Neq,
Ge,
Le,
}
#[derive(Debug, Clone)]
pub struct Filter {
pub key: String,
pub op: FilterOp,
pub value: String,
}
#[derive(Debug, Clone)]
pub struct MtCommand {
pub cmd: String,
pub attr: Vec<(String, Option<String>)>,
pub filter: Vec<Filter>
}
impl MtCommand {
pub fn new(cmd: &str, attr: Vec<(String, Option<String>)>, filter: Vec<Filter>) -> Self {
Self {
cmd: cmd.to_string(),
attr, filter
}
}
pub fn new_basic(cmd: &str) -> Self {
Self {
cmd: cmd.to_string(),
attr: Default::default(),
filter: Default::default()
}
}
}
fn parse_filter(s: &str) -> Result<Filter, String> {
let (key, op, value) = [">=", "<=", "!=", "=", ">", "<"]
.into_iter()
.find_map(|op| {
s.split_once(op)
.map(|(key, value)| (key, op, value))
})
.ok_or_else(|| format!("invalid filter: {s}"))?;
let op = match op {
"=" => FilterOp::Eq,
"!=" => FilterOp::Neq,
">" => FilterOp::Gt,
"<" => FilterOp::Lt,
">=" => FilterOp::Ge,
"<=" => FilterOp::Le,
_ => unreachable!(),
};
Ok(Filter {
key: key.trim().to_string(),
op,
value: value.trim().to_string(),
})
}
fn parse_attribute(s: &str) -> Result<(String, Option<String>), String> {
let s = s.trim();
if let Some((key, value)) = s.split_once('=') {
Ok((key.trim().to_string(), Some(value.trim().to_string())))
} else if !s.is_empty() {
Ok((s.to_string(), None))
} else {
Err("empty attribute".to_string())
}
}