Added filtering and attribute flags, improved bes_wifi by adding ssids
and tags to the equasion
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use clap::ArgGroup;
|
||||
use serde::de::value;
|
||||
|
||||
#[derive(Debug, Clone, clap::Parser, Default)]
|
||||
#[command(
|
||||
@@ -172,6 +173,8 @@ pub struct MtCommand {
|
||||
pub filter: Vec<Filter>
|
||||
}
|
||||
|
||||
pub struct MtCommandBuilder(MtCommand);
|
||||
|
||||
impl MtCommand {
|
||||
pub fn new(cmd: &str, attr: Vec<(String, Option<String>)>, filter: Vec<Filter>) -> Self {
|
||||
Self {
|
||||
@@ -179,6 +182,13 @@ impl MtCommand {
|
||||
attr, filter
|
||||
}
|
||||
}
|
||||
pub fn builder(cmd: &str) -> MtCommandBuilder {
|
||||
MtCommandBuilder(Self {
|
||||
cmd: cmd.to_string(),
|
||||
attr: Default::default(),
|
||||
filter: Default::default()
|
||||
})
|
||||
}
|
||||
pub fn new_basic(cmd: &str) -> Self {
|
||||
Self {
|
||||
cmd: cmd.to_string(),
|
||||
@@ -188,6 +198,72 @@ impl MtCommand {
|
||||
}
|
||||
}
|
||||
|
||||
impl MtCommandBuilder {
|
||||
pub fn attribute_raw(mut self, key: String, value: Option<String>) -> Self {
|
||||
self.0.attr.push((key, value));
|
||||
self
|
||||
}
|
||||
pub fn filter_raw(mut self, key: String, op: FilterOp, value: String) -> Self {
|
||||
self.0.filter.push(Filter { key, op, value });
|
||||
self
|
||||
}
|
||||
pub fn attribute(self, s: &str) -> anyhow::Result<Self> {
|
||||
let (key, value) = parse_attribute(&s).map_err(|e| anyhow::anyhow!(e))?;
|
||||
Ok(self.attribute_raw(key, value))
|
||||
}
|
||||
pub fn filter(self, s: &str) -> anyhow::Result<Self> {
|
||||
let Filter{key, op, value} = parse_filter(&s).map_err(|e| anyhow::anyhow!(e))?;
|
||||
Ok(self.filter_raw(key, op, value))
|
||||
}
|
||||
pub fn build(self) -> MtCommand {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for MtCommand {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.cmd)?;
|
||||
|
||||
let mut words = Vec::new();
|
||||
|
||||
for filter in &self.filter {
|
||||
let (operator, negate) = match filter.op {
|
||||
FilterOp::Eq => ('=', false),
|
||||
FilterOp::Neq => ('=', true),
|
||||
FilterOp::Gt => ('>', false),
|
||||
FilterOp::Lt => ('<', false),
|
||||
FilterOp::Ge => ('<', true),
|
||||
FilterOp::Le => ('>', true),
|
||||
};
|
||||
|
||||
let prefix = match operator {
|
||||
'>' => ">",
|
||||
'<' => "<",
|
||||
_ => "",
|
||||
};
|
||||
|
||||
words.push(format!("?{prefix}{}={}", filter.key, filter.value));
|
||||
|
||||
if negate {
|
||||
words.push("?#!".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for (key, value) in &self.attr {
|
||||
words.push(match value {
|
||||
Some(value) => format!("&={key}={value}"),
|
||||
None => format!("&={key}"),
|
||||
});
|
||||
}
|
||||
|
||||
if !words.is_empty() {
|
||||
write!(f, "{}", words.join(""))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_filter(s: &str) -> Result<Filter, String> {
|
||||
let (key, op, value) = [">=", "<=", "!=", "=", ">", "<"]
|
||||
.into_iter()
|
||||
|
||||
Reference in New Issue
Block a user