diff --git a/src/cli.rs b/src/cli.rs index 1a4cf43..1dd3606 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,8 @@ use clap::{Parser, Subcommand, Args}; use telnet::Telnet; use crate::parameter::{Parameter, ParameterList}; -use crate::response_classes::{ResponseChannel, ResponseClient}; +use crate::response::channel::ResponseChannel; +use crate::response::client::ResponseClient; use crate::utils; #[derive(Parser)] diff --git a/src/main.rs b/src/main.rs index 861c69a..e4a40d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,13 @@ mod response; mod utils; mod commands; mod parameter; -mod response_classes; mod cli; use std::process::exit; use telnet::Telnet; use crate::cli::Commands; -use crate::response_classes::{ResponseChannel, ResponseClient}; +use crate::response::channel::ResponseChannel; +use crate::response::client::ResponseClient; fn channel_or_exit(channel_res: Result, String>) -> ResponseChannel { channel_res.unwrap_or_else(|err| { diff --git a/src/parameter.rs b/src/parameter.rs index e574968..8890aa9 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -18,7 +18,7 @@ pub fn parameter_find(params: &Vec, name: &str) -> Option } pub fn parameter_list_find(param_lists: &Vec, name: &str, value: &str, strict: bool) -> Option { for params in param_lists { - if params.iter().position(|param| param.is(name, value, strict)).is_some() { + if params.iter().any(|param| param.is(name, value, strict)) { return Some(params.clone()); } } @@ -27,7 +27,7 @@ pub fn parameter_list_find(param_lists: &Vec, name: &str, value: pub fn parameter_list_find_all(param_lists: &Vec, name: &str, value: &str, strict: bool) -> Vec { let mut found = Vec::new(); for params in param_lists { - if params.iter().position(|param| param.is(name, value, strict)).is_some() { + if params.iter().any(|param| param.is(name, value, strict)) { found.push(params.clone()) } } @@ -65,17 +65,15 @@ impl Parameter { pub fn is(&self, name: &str, value: &str, strict: bool) -> bool { if self.name != name { - false + return false; } - else if self.value != value && strict { - false + if self.value != value && strict { + return false; } - else if !self.value.contains(value) && !strict { - false - } - else { - true + if !self.value.contains(value) && !strict { + return false; } + true } pub fn to_i32(&self, default: i32) -> i32 { diff --git a/src/response.rs b/src/response.rs index 09f362f..6dab5ba 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,3 +1,6 @@ +pub mod channel; +pub mod client; + use std::fmt::{Debug, Display, Formatter}; use crate::parameter::*; diff --git a/src/response/channel.rs b/src/response/channel.rs new file mode 100644 index 0000000..ab5d0fe --- /dev/null +++ b/src/response/channel.rs @@ -0,0 +1,83 @@ +use std::fmt::{Display, Formatter}; +use crate::parameter::{parameter_find, ParameterList}; + +#[derive(Debug)] +pub struct ResponseChannel { + pub cid: i32, + pub pid: i32, + pub channel_order: i32, + pub channel_name: String, + pub channel_topic: String, + pub channel_flag_are_subscribed: bool, +} + +impl Display for ResponseChannel { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} ({})", self.channel_name, self.cid) + } +} + +impl From for ResponseChannel { + fn from(value: ParameterList) -> Self { + ResponseChannel { + cid: parameter_find(&value, "cid") + .unwrap_or_default() + .to_i32(-1), + pid: parameter_find(&value, "pid") + .unwrap_or_default() + .to_i32(-1), + channel_order: parameter_find(&value, "channel_order") + .unwrap_or_default() + .to_i32(-1), + channel_name: parameter_find(&value, "channel_name") + .unwrap_or_default() + .value, + channel_topic: parameter_find(&value, "channel_topic") + .unwrap_or_default() + .value, + channel_flag_are_subscribed: parameter_find(&value, "channel_flag_are_subscribed") + .unwrap_or_default() + .to_i32(0) == 1, + } + } +} + +impl ResponseChannel { + pub fn is_spacer(&self) -> bool { + self.channel_name.starts_with("[spacer") + || self.channel_name.starts_with("[*spacer") + || self.channel_name.starts_with("[lspacer") + || self.channel_name.starts_with("[cspacer") + || self.channel_name.starts_with("[rspacer") + } + + fn list_find_next(list: &mut Vec, order: i32, pid: i32) -> Option { + let index = list.iter().position(|a| a.channel_order == order && a.pid == pid)?; + Some(list.swap_remove(index)) + } + + pub fn sort_list(mut list: Vec) -> Vec { + let mut list_sorted: Vec = Vec::new(); + let mut pids: Vec = Vec::new(); + let mut pid = 0; + let mut order = 0; + + while !list.is_empty() { + match ResponseChannel::list_find_next(&mut list, order, pid) { + None => { + order = pid; + pid = pids.pop().unwrap_or(0); + } + Some(elem) => { + pids.push(pid); + pid = elem.cid; + order = 0; + list_sorted.push(elem); + } + } + } + + list_sorted + } +} + diff --git a/src/response/client.rs b/src/response/client.rs new file mode 100644 index 0000000..4a59d07 --- /dev/null +++ b/src/response/client.rs @@ -0,0 +1,39 @@ +use std::fmt::{Display, Formatter}; +use crate::parameter::{parameter_find, ParameterList}; + +#[derive(Debug)] +pub struct ResponseClient { + pub cid: i32, + pub clid: i32, + pub client_database_id: i32, + pub client_nickname: String, + pub client_type: i32, +} + +impl Display for ResponseClient { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} ({})", self.client_nickname, self.clid) + } +} + +impl From for ResponseClient { + fn from(value: ParameterList) -> Self { + ResponseClient { + cid: parameter_find(&value, "cid") + .unwrap_or_default() + .to_i32(-1), + clid: parameter_find(&value, "clid") + .unwrap_or_default() + .to_i32(-1), + client_database_id: parameter_find(&value, "client_database_id") + .unwrap_or_default() + .to_i32(-1), + client_nickname: parameter_find(&value, "client_nickname") + .unwrap_or_default() + .value, + client_type: parameter_find(&value, "channel_topic") + .unwrap_or_default() + .to_i32(0), + } + } +} \ No newline at end of file diff --git a/src/response_classes.rs b/src/response_classes.rs index ac11043..e69de29 100644 --- a/src/response_classes.rs +++ b/src/response_classes.rs @@ -1,119 +0,0 @@ -use std::fmt::{Display, Formatter}; -use crate::parameter::{parameter_find, ParameterList}; - -#[derive(Debug)] -pub struct ResponseChannel { - pub cid: i32, - pub pid: i32, - pub channel_order: i32, - pub channel_name: String, - pub channel_topic: String, - pub channel_flag_are_subscribed: bool, -} - -impl Display for ResponseChannel { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{} ({})", self.channel_name, self.cid) - } -} - -impl From for ResponseChannel { - fn from(value: ParameterList) -> Self { - ResponseChannel { - cid: parameter_find(&value, "cid") - .unwrap_or_default() - .to_i32(-1), - pid: parameter_find(&value, "pid") - .unwrap_or_default() - .to_i32(-1), - channel_order: parameter_find(&value, "channel_order") - .unwrap_or_default() - .to_i32(-1), - channel_name: parameter_find(&value, "channel_name") - .unwrap_or_default() - .value, - channel_topic: parameter_find(&value, "channel_topic") - .unwrap_or_default() - .value, - channel_flag_are_subscribed: parameter_find(&value, "channel_flag_are_subscribed") - .unwrap_or_default() - .to_i32(0) == 1, - } - } -} - -impl ResponseChannel { - pub fn is_spacer(&self) -> bool { - self.channel_name.starts_with("[spacer") - || self.channel_name.starts_with("[*spacer") - || self.channel_name.starts_with("[lspacer") - || self.channel_name.starts_with("[cspacer") - || self.channel_name.starts_with("[rspacer") - } - - fn list_find_next(list: &mut Vec, order: i32, pid: i32) -> Option { - let index = list.iter().position(|a| a.channel_order == order && a.pid == pid)?; - Some(list.swap_remove(index)) - } - - pub fn sort_list(mut list: Vec) -> Vec { - let mut list_sorted: Vec = Vec::new(); - let mut pids: Vec = Vec::new(); - let mut pid = 0; - let mut order = 0; - - while !list.is_empty() { - match ResponseChannel::list_find_next(&mut list, order, pid) { - None => { - order = pid; - pid = pids.pop().unwrap_or(0); - } - Some(elem) => { - pids.push(pid); - pid = elem.cid; - order = 0; - list_sorted.push(elem); - } - } - } - - list_sorted - } -} - -#[derive(Debug)] -pub struct ResponseClient { - pub cid: i32, - pub clid: i32, - pub client_database_id: i32, - pub client_nickname: String, - pub client_type: i32, -} - -impl Display for ResponseClient { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{} ({})", self.client_nickname, self.clid) - } -} - -impl From for ResponseClient { - fn from(value: ParameterList) -> Self { - ResponseClient { - cid: parameter_find(&value, "cid") - .unwrap_or_default() - .to_i32(-1), - clid: parameter_find(&value, "clid") - .unwrap_or_default() - .to_i32(-1), - client_database_id: parameter_find(&value, "client_database_id") - .unwrap_or_default() - .to_i32(-1), - client_nickname: parameter_find(&value, "client_nickname") - .unwrap_or_default() - .value, - client_type: parameter_find(&value, "channel_topic") - .unwrap_or_default() - .to_i32(0), - } - } -} diff --git a/src/utils.rs b/src/utils.rs index 33a6f44..1252c8f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,7 +6,8 @@ use telnet::Event::TimedOut; use crate::{commands, parameter}; use crate::parameter::{parameter_list_find_all, ParameterList}; use crate::response::Response; -use crate::response_classes::{ResponseChannel, ResponseClient}; +use crate::response::channel::ResponseChannel; +use crate::response::client::ResponseClient; pub fn skip_welcome(connection: &mut Telnet) { loop {