121 lines
No EOL
3 KiB
Rust
121 lines
No EOL
3 KiB
Rust
use std::fmt::{Debug, Display, Formatter};
|
|
use crate::utils::{decode_value, encode_value};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Parameter {
|
|
pub name: String,
|
|
pub value: String,
|
|
}
|
|
|
|
pub type ParameterList = Vec<Parameter>;
|
|
|
|
pub fn parameter_find(params: &Vec<Parameter>, name: &str) -> Option<Parameter> {
|
|
for param in params {
|
|
if param.name == name {
|
|
return Some(param.clone());
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn parameter_list_find(param_lists: &Vec<ParameterList>, name: &str, value: &str, strict: bool) -> Option<ParameterList> {
|
|
for params in param_lists {
|
|
if params.iter().any(|param| param.is(name, value, strict)) {
|
|
return Some(params.clone());
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn parameter_list_find_all(param_lists: &Vec<ParameterList>, name: &str, value: &str, strict: bool) -> Vec<ParameterList> {
|
|
let mut found = Vec::new();
|
|
for params in param_lists {
|
|
if params.iter().any(|param| param.is(name, value, strict)) {
|
|
found.push(params.clone())
|
|
}
|
|
}
|
|
found
|
|
}
|
|
|
|
pub fn parameter_parse(params_str: &str) -> ParameterList {
|
|
let parts: Vec<&str> = params_str.split(' ').collect();
|
|
|
|
let mut response_params = Vec::new();
|
|
parts.iter().for_each(|part| {
|
|
response_params.push(Parameter::from(part.split_once('=').unwrap()));
|
|
});
|
|
|
|
response_params
|
|
}
|
|
|
|
impl From<(&str, &str)> for Parameter {
|
|
fn from(param: (&str, &str)) -> Parameter {
|
|
Parameter::new(param.0, param.1)
|
|
}
|
|
}
|
|
|
|
impl Parameter {
|
|
pub fn new(name: &str, value: &str) -> Parameter {
|
|
Parameter {
|
|
name: String::from(name),
|
|
value: decode_value(value)
|
|
}
|
|
}
|
|
|
|
pub fn is(&self, name: &str, value: &str, strict: bool) -> bool {
|
|
if self.name != name {
|
|
return false;
|
|
}
|
|
if self.value != value && strict {
|
|
return false;
|
|
}
|
|
if !self.value.contains(value) && !strict {
|
|
return false;
|
|
}
|
|
true
|
|
}
|
|
|
|
pub fn to_i32(&self, default: i32) -> i32 {
|
|
self.value.parse::<i32>().unwrap_or(default)
|
|
}
|
|
|
|
pub fn list_to_string(parameter_list: ParameterList) -> String {
|
|
parameter_list
|
|
.into_iter()
|
|
.map(String::from)
|
|
.collect::<Vec<String>>()
|
|
.join(" ")
|
|
}
|
|
|
|
pub fn list_to_string_sep(parameter_list: ParameterList, sep: &str) -> String {
|
|
parameter_list
|
|
.into_iter()
|
|
.map(String::from)
|
|
.collect::<Vec<String>>()
|
|
.join(sep)
|
|
}
|
|
}
|
|
|
|
impl Display for Parameter {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}={}", self.name, self.value)
|
|
}
|
|
}
|
|
|
|
impl Debug for Parameter {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}={}", self.name, self.value)
|
|
}
|
|
}
|
|
|
|
impl Default for Parameter {
|
|
fn default() -> Self {
|
|
Parameter::new("", "")
|
|
}
|
|
}
|
|
|
|
impl From<Parameter> for String {
|
|
fn from(value: Parameter) -> Self {
|
|
format!("{}={}", value.name, encode_value(&value.value))
|
|
}
|
|
} |