Add event listener with JSON output (WIP) Add notifier on movement events Refactor Parameter and ParameterList (still shit)
This commit is contained in:
parent
2c0a8ab616
commit
d8cdc2bb11
14 changed files with 338 additions and 186 deletions
src
119
src/parameter.rs
119
src/parameter.rs
|
@ -1,36 +1,38 @@
|
|||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::collections::HashMap;
|
||||
use crate::utils::{decode_value, encode_value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Parameter {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
pub type ParameterList = HashMap<String, String>;
|
||||
pub type Parameter = (String, String);
|
||||
|
||||
pub fn parameter_find(params: &ParameterList, name: &str) -> Option<String> {
|
||||
params.get(name).map(|value| value.clone())
|
||||
}
|
||||
|
||||
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());
|
||||
pub fn parameter_list_has(params: &ParameterList, key: &str, value: &str, strict: bool) -> bool {
|
||||
if let Some(check_value) = params.get(key) {
|
||||
if check_value == value && strict {
|
||||
return true;
|
||||
}
|
||||
if check_value.contains(value) && !strict {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
None
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn parameter_list_find(param_lists: &Vec<ParameterList>, name: &str, value: &str, strict: bool) -> Option<ParameterList> {
|
||||
pub fn parameter_list_find(param_lists: &Vec<ParameterList>, key: &str, value: &str, strict: bool) -> Option<ParameterList> {
|
||||
for params in param_lists {
|
||||
if params.iter().any(|param| param.is(name, value, strict)) {
|
||||
if parameter_list_has(params, key, 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> {
|
||||
pub fn parameter_list_find_all(param_lists: &Vec<ParameterList>, key: &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)) {
|
||||
if parameter_list_has(params, key, value, strict) {
|
||||
found.push(params.clone())
|
||||
}
|
||||
}
|
||||
|
@ -40,82 +42,31 @@ pub fn parameter_list_find_all(param_lists: &Vec<ParameterList>, name: &str, val
|
|||
pub fn parameter_parse(params_str: &str) -> ParameterList {
|
||||
let parts: Vec<&str> = params_str.split(' ').collect();
|
||||
|
||||
let mut response_params = Vec::new();
|
||||
let mut response_params = ParameterList::new();
|
||||
parts.iter().for_each(|part| {
|
||||
response_params.push(Parameter::from(part.split_once('=').unwrap()));
|
||||
let (key, value) = part.split_once('=').unwrap_or((part, "1"));
|
||||
response_params.insert(key.to_string(), decode_value(value));
|
||||
});
|
||||
|
||||
response_params
|
||||
}
|
||||
|
||||
impl From<(&str, &str)> for Parameter {
|
||||
fn from(param: (&str, &str)) -> Parameter {
|
||||
Parameter::new(param.0, param.1)
|
||||
}
|
||||
pub fn parameter_to_string(param: Parameter) -> String {
|
||||
format!("{}={}", param.0, encode_value(¶m.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)
|
||||
}
|
||||
pub fn parameter_list_to_string(parameter_list: ParameterList, sep: &str) -> String {
|
||||
parameter_list
|
||||
.into_iter()
|
||||
.map(|param| parameter_to_string(param))
|
||||
.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))
|
||||
}
|
||||
pub fn parameters_to_string(parameters: Vec<Parameter>, sep: &str) -> String {
|
||||
parameters
|
||||
.into_iter()
|
||||
.map(|param| parameter_to_string(param))
|
||||
.collect::<Vec<String>>()
|
||||
.join(sep)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue