From ad58ab9bdd1f68b12d8fe89895586961c58e2e71 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sun, 26 Nov 2023 22:46:08 +0100 Subject: [PATCH] Refactor naming --- src/cli.rs | 14 +++---- src/commands.rs | 4 +- src/main.rs | 11 +++--- src/{response => models}/channel.rs | 18 ++++----- src/{response => models}/client.rs | 8 ++-- src/models/mod.rs | 5 +++ src/response.rs | 3 -- src/response_classes.rs | 0 src/utils.rs | 5 ++- src/wrappers.rs | 60 ++++++++++++++--------------- 10 files changed, 66 insertions(+), 62 deletions(-) rename src/{response => models}/channel.rs (82%) rename src/{response => models}/client.rs (88%) create mode 100644 src/models/mod.rs delete mode 100644 src/response_classes.rs diff --git a/src/cli.rs b/src/cli.rs index 692841f..f518b41 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,8 +2,8 @@ use clap::{Args, Parser, Subcommand}; use telnet::Telnet; use crate::parameter::{Parameter, ParameterList}; -use crate::response::channel::ResponseChannel; -use crate::response::client::ResponseClient; +use crate::models::Channel; +use crate::models::Client; use crate::utils::SendTextMessageTarget; use crate::wrappers; @@ -87,14 +87,14 @@ impl FetchArgs { self.client.is_some() } - pub fn channel(&self, connection: &mut Telnet) -> Result, String> { + pub fn channel(&self, connection: &mut Telnet) -> Result, String> { if let Some(channel) = &self.channel { wrappers::find_channel(connection, channel, self.strict_channel) } else { Err("No channel specified.".to_string()) } } - pub fn client(&self, connection: &mut Telnet) -> Result, String> { + pub fn client(&self, connection: &mut Telnet) -> Result, String> { if let Some(client) = &self.client { wrappers::find_client(connection, client, self.strict_client) } else { @@ -109,7 +109,7 @@ impl MessageArgs { Ok(SendTextMessageTarget::Server) } else if let Some(client) = &self.client { if let Some(client) = wrappers::find_client(connection, client, self.strict_client)? { - return Ok(SendTextMessageTarget::Client(client.clid)); + return Ok(SendTextMessageTarget::Client(client)); } return Err("Could not find client.".to_string()); } else { @@ -119,10 +119,10 @@ impl MessageArgs { } impl MoveArgs { - pub fn channel(&self, connection: &mut Telnet) -> Result, String> { + pub fn channel(&self, connection: &mut Telnet) -> Result, String> { wrappers::find_channel(connection, &self.channel, self.strict_channel) } - pub fn client(&self, connection: &mut Telnet) -> Result, String> { + pub fn client(&self, connection: &mut Telnet) -> Result, String> { match &self.client { Some(client) => { wrappers::find_client(connection, client, self.strict_client) diff --git a/src/commands.rs b/src/commands.rs index 5b69f45..a448c55 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -17,7 +17,7 @@ fn read_part(connection: &mut Telnet) -> Result { Ok(event) => { match event { Data(bytes) => Ok(String::from_utf8(bytes.to_vec()) - .map_err(|_| "Teamspeak returned a badly formatted response.")?), + .map_err(|_| "Teamspeak returned a badly formatted models.")?), _ => { Err(String::from("Received unknown event from Teamspeak.")) } @@ -63,7 +63,7 @@ fn read_response(connection: &mut Telnet, skip_ok: bool, mut buffer: String) -> Ok(Response::Ok) } } else { - Err(format!("Received error response from Teamspeak: {} ({})", err.msg, err.id)) + Err(format!("Received error models from Teamspeak: {} ({})", err.msg, err.id)) } } } diff --git a/src/main.rs b/src/main.rs index 5bbcdd1..f4d735b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,17 +3,18 @@ use std::process::exit; use telnet::Telnet; use crate::cli::Commands; -use crate::response::channel::ResponseChannel; -use crate::response::client::ResponseClient; +use crate::models::Channel; +use crate::models::Client; -mod response; mod wrappers; mod commands; mod parameter; mod cli; mod utils; +mod models; +mod response; -fn channel_or_exit(channel_res: Result, String>) -> ResponseChannel { +fn channel_or_exit(channel_res: Result, String>) -> Channel { channel_res.unwrap_or_else(|err| { println!("Failed to find channel: {}", err); exit(1); @@ -24,7 +25,7 @@ fn channel_or_exit(channel_res: Result, String>) -> Resp }) } -fn client_or_exit(client_res: Result, String>) -> ResponseClient { +fn client_or_exit(client_res: Result, String>) -> Client { client_res.unwrap_or_else(|err| { println!("Failed to find client: {}", err); exit(1); diff --git a/src/response/channel.rs b/src/models/channel.rs similarity index 82% rename from src/response/channel.rs rename to src/models/channel.rs index 88be17c..bc0d928 100644 --- a/src/response/channel.rs +++ b/src/models/channel.rs @@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter}; use crate::parameter::{parameter_find, ParameterList}; #[derive(Debug)] -pub struct ResponseChannel { +pub struct Channel { pub cid: i32, pub pid: i32, pub channel_order: i32, @@ -12,15 +12,15 @@ pub struct ResponseChannel { pub channel_flag_are_subscribed: bool, } -impl Display for ResponseChannel { +impl Display for Channel { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{} ({})", self.channel_name, self.cid) } } -impl From for ResponseChannel { +impl From for Channel { fn from(value: ParameterList) -> Self { - ResponseChannel { + Channel { cid: parameter_find(&value, "cid") .unwrap_or_default() .to_i32(-1), @@ -43,7 +43,7 @@ impl From for ResponseChannel { } } -impl ResponseChannel { +impl Channel { pub fn is_spacer(&self) -> bool { self.channel_name.starts_with("[spacer") || self.channel_name.starts_with("[*spacer") @@ -52,19 +52,19 @@ impl ResponseChannel { || self.channel_name.starts_with("[rspacer") } - fn list_find_next(list: &mut Vec, order: i32, pid: i32) -> Option { + 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(); + 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) { + match Channel::list_find_next(&mut list, order, pid) { None => { order = pid; pid = pids.pop().unwrap_or(0); diff --git a/src/response/client.rs b/src/models/client.rs similarity index 88% rename from src/response/client.rs rename to src/models/client.rs index 93fd50c..25405e0 100644 --- a/src/response/client.rs +++ b/src/models/client.rs @@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter}; use crate::parameter::{parameter_find, ParameterList}; #[derive(Debug)] -pub struct ResponseClient { +pub struct Client { pub cid: i32, pub clid: i32, pub client_database_id: i32, @@ -11,15 +11,15 @@ pub struct ResponseClient { pub client_type: i32, } -impl Display for ResponseClient { +impl Display for Client { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{} ({})", self.client_nickname, self.clid) } } -impl From for ResponseClient { +impl From for Client { fn from(value: ParameterList) -> Self { - ResponseClient { + Client { cid: parameter_find(&value, "cid") .unwrap_or_default() .to_i32(-1), diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..df71696 --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1,5 @@ +mod channel; +mod client; + +pub use channel::Channel; +pub use client::Client; \ No newline at end of file diff --git a/src/response.rs b/src/response.rs index 47bef89..d4c5a00 100644 --- a/src/response.rs +++ b/src/response.rs @@ -2,9 +2,6 @@ use std::fmt::{Debug, Display, Formatter}; use crate::parameter::*; -pub mod channel; -pub mod client; - pub enum Response { Ok, Data(ParameterList), diff --git a/src/response_classes.rs b/src/response_classes.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils.rs b/src/utils.rs index 8f19cab..766eb23 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,4 @@ +use crate::models::Client; use crate::parameter::Parameter; pub fn decode_value(value: &str) -> String { @@ -13,7 +14,7 @@ pub fn encode_value(value: &str) -> String { } pub enum SendTextMessageTarget { - Client(i32), + Client(Client), Channel, Server, } @@ -27,7 +28,7 @@ impl From for String { }; let target = match value { - SendTextMessageTarget::Client(id) => id.to_string(), + SendTextMessageTarget::Client(client) => client.clid.to_string(), _ => String::from("0"), }; diff --git a/src/wrappers.rs b/src/wrappers.rs index e69cc94..0071a7a 100644 --- a/src/wrappers.rs +++ b/src/wrappers.rs @@ -6,8 +6,8 @@ use telnet::Telnet; use crate::{commands, parameter}; use crate::parameter::{parameter_list_find_all, ParameterList}; -use crate::response::channel::ResponseChannel; -use crate::response::client::ResponseClient; +use crate::models::Channel; +use crate::models::Client; use crate::response::Response; use crate::utils::SendTextMessageTarget; @@ -41,101 +41,101 @@ pub fn login(connection: &mut Telnet) { } } -pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result, String> { +pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result, String> { match commands::channellist(connection) { Ok(response) => { match response { Response::DataList(parameter_lists) => { - let channels: Vec = parameter_lists.iter() - .map(|params| ResponseChannel::from(params.clone())) + let channels: Vec = parameter_lists.iter() + .map(|params| Channel::from(params.clone())) .collect(); - let mut channels = ResponseChannel::sort_list(channels); + let mut channels = Channel::sort_list(channels); if !spacers { channels.retain(|c| !c.is_spacer()); } Ok(channels) } - _ => Err(String::from("Received unexpected response from Teamspeak.")) + _ => Err(String::from("Received unexpected models from Teamspeak.")) } } Err(msg) => Err(msg) } } -pub fn find_channel(connection: &mut Telnet, name: &str, strict: bool) -> Result, String> { +pub fn find_channel(connection: &mut Telnet, name: &str, strict: bool) -> Result, String> { match commands::channellist(connection) { Ok(response) => { match response { Response::DataList(parameter_lists) => { match parameter::parameter_list_find(¶meter_lists, "channel_name", name, strict) { Some(params) => { - Ok(Some(ResponseChannel::from(params))) + Ok(Some(Channel::from(params))) } None => { Ok(None) } } } - _ => Err(String::from("Received unexpected response from Teamspeak.")) + _ => Err(String::from("Received unexpected models from Teamspeak.")) } } Err(msg) => Err(msg) } } -pub fn get_clients(connection: &mut Telnet) -> Result, String> { +pub fn get_clients(connection: &mut Telnet) -> Result, String> { match commands::clientlist(connection) { Ok(response) => { match response { Response::DataList(parameter_lists) => { - let mut clients: Vec = parameter_lists.iter() - .map(|params| ResponseClient::from(params.clone())) + let mut clients: Vec = parameter_lists.iter() + .map(|params| Client::from(params.clone())) .collect(); clients.sort_by(|a, b| a.client_nickname.cmp(&b.client_nickname)); Ok(clients) } - _ => Err(String::from("Received unexpected response from Teamspeak.")) + _ => Err(String::from("Received unexpected models from Teamspeak.")) } } Err(msg) => Err(msg) } } -pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result, String> { +pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result, String> { match commands::clientlist(connection) { Ok(response) => { match response { Response::DataList(parameter_lists) => { match parameter::parameter_list_find(¶meter_lists, "client_nickname", name, strict) { Some(params) => { - Ok(Some(ResponseClient::from(params))) + Ok(Some(Client::from(params))) } None => { Ok(None) } } } - _ => Err(String::from("Received unexpected response from Teamspeak.")) + _ => Err(String::from("Received unexpected models from Teamspeak.")) } } Err(msg) => Err(msg) } } -pub fn get_channel_clients(connection: &mut Telnet, channel: &ResponseChannel) -> Result, String> { +pub fn get_channel_clients(connection: &mut Telnet, channel: &Channel) -> Result, String> { match commands::clientlist(connection) { Ok(response) => { match response { Response::DataList(parameter_lists) => { - let mut clients: Vec = Vec::new(); + let mut clients: Vec = Vec::new(); for client_params in parameter_list_find_all(¶meter_lists, "cid", &channel.cid.to_string(), true) { - clients.push(ResponseClient::from(client_params)); + clients.push(Client::from(client_params)); } Ok(clients) } - _ => Err(String::from("Received unexpected response from Teamspeak.")) + _ => Err(String::from("Received unexpected models from Teamspeak.")) } } Err(msg) => Err(msg) @@ -148,18 +148,18 @@ fn get_self_clid(connection: &mut Telnet) -> Result { match response { Response::Data(params) => { match parameter::parameter_find(¶ms, "clid") { - None => Err(String::from("Could not find clid in response from Teamspeak.")), + None => Err(String::from("Could not find clid in models from Teamspeak.")), Some(param) => Ok(param.value) } } - _ => Err(String::from("Received unexpected response from Teamspeak for whoami.")) + _ => Err(String::from("Received unexpected models from Teamspeak for whoami.")) } } Err(msg) => Err(msg) } } -pub fn find_self(connection: &mut Telnet) -> Result { +pub fn find_self(connection: &mut Telnet) -> Result { let clid = get_self_clid(connection)?; match commands::clientlist(connection) { @@ -168,28 +168,28 @@ pub fn find_self(connection: &mut Telnet) -> Result { Response::DataList(parameter_lists) => { match parameter::parameter_list_find(¶meter_lists, "clid", &clid, false) { Some(params) => { - Ok(ResponseClient::from(params)) + Ok(Client::from(params)) } None => { - Err(String::from("Could not find self in response from Teamspeak.")) + Err(String::from("Could not find self in models from Teamspeak.")) } } } - _ => Err(String::from("Received unexpected response from Teamspeak for clientlist.")) + _ => Err(String::from("Received unexpected models from Teamspeak for clientlist.")) } } Err(msg) => Err(msg) } } -pub fn fetch_client(connection: &mut Telnet, clients: &[ResponseClient]) -> Result { +pub fn fetch_client(connection: &mut Telnet, clients: &[Client]) -> Result { let cid = find_self(connection)?.cid; let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect(); commands::clientmove(connection, &cid, clid_list) } -pub fn fetch_channel(connection: &mut Telnet, channel: ResponseChannel) -> Result { +pub fn fetch_channel(connection: &mut Telnet, channel: Channel) -> Result { let cid = find_self(connection)?.cid; let clients = get_channel_clients(connection, &channel)?; @@ -198,7 +198,7 @@ pub fn fetch_channel(connection: &mut Telnet, channel: ResponseChannel) -> Resul commands::clientmove(connection, &cid, clid_list) } -pub fn move_client(connection: &mut Telnet, channel: &ResponseChannel, clients: &[ResponseClient]) -> Result { +pub fn move_client(connection: &mut Telnet, channel: &Channel, clients: &[Client]) -> Result { let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect(); commands::clientmove(connection, &channel.cid, clid_list)