Refactor response_classes and fix clippy issues

This commit is contained in:
Tobias Reisinger 2023-11-17 17:37:25 +01:00
parent 244a7073fe
commit 463bc99b9c
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 139 additions and 133 deletions

View file

@ -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)]

View file

@ -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<Option<ResponseChannel>, String>) -> ResponseChannel {
channel_res.unwrap_or_else(|err| {

View file

@ -18,7 +18,7 @@ pub fn parameter_find(params: &Vec<Parameter>, name: &str) -> Option<Parameter>
}
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().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<ParameterList>, name: &str, value:
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().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 {

View file

@ -1,3 +1,6 @@
pub mod channel;
pub mod client;
use std::fmt::{Debug, Display, Formatter};
use crate::parameter::*;

83
src/response/channel.rs Normal file
View file

@ -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<ParameterList> 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<ResponseChannel>, order: i32, pid: i32) -> Option<ResponseChannel> {
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<ResponseChannel>) -> Vec<ResponseChannel> {
let mut list_sorted: Vec<ResponseChannel> = Vec::new();
let mut pids: Vec<i32> = 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
}
}

39
src/response/client.rs Normal file
View file

@ -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<ParameterList> 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),
}
}
}

View file

@ -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<ParameterList> 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<ResponseChannel>, order: i32, pid: i32) -> Option<ResponseChannel> {
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<ResponseChannel>) -> Vec<ResponseChannel> {
let mut list_sorted: Vec<ResponseChannel> = Vec::new();
let mut pids: Vec<i32> = 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<ParameterList> 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),
}
}
}

View file

@ -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 {