Refactor naming

This commit is contained in:
Tobias Reisinger 2023-11-26 22:46:08 +01:00
parent a9fd240bd3
commit ad58ab9bdd
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
10 changed files with 66 additions and 62 deletions

View file

@ -2,8 +2,8 @@ use clap::{Args, Parser, Subcommand};
use telnet::Telnet; use telnet::Telnet;
use crate::parameter::{Parameter, ParameterList}; use crate::parameter::{Parameter, ParameterList};
use crate::response::channel::ResponseChannel; use crate::models::Channel;
use crate::response::client::ResponseClient; use crate::models::Client;
use crate::utils::SendTextMessageTarget; use crate::utils::SendTextMessageTarget;
use crate::wrappers; use crate::wrappers;
@ -87,14 +87,14 @@ impl FetchArgs {
self.client.is_some() self.client.is_some()
} }
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> { pub fn channel(&self, connection: &mut Telnet) -> Result<Option<Channel>, String> {
if let Some(channel) = &self.channel { if let Some(channel) = &self.channel {
wrappers::find_channel(connection, channel, self.strict_channel) wrappers::find_channel(connection, channel, self.strict_channel)
} else { } else {
Err("No channel specified.".to_string()) Err("No channel specified.".to_string())
} }
} }
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> { pub fn client(&self, connection: &mut Telnet) -> Result<Option<Client>, String> {
if let Some(client) = &self.client { if let Some(client) = &self.client {
wrappers::find_client(connection, client, self.strict_client) wrappers::find_client(connection, client, self.strict_client)
} else { } else {
@ -109,7 +109,7 @@ impl MessageArgs {
Ok(SendTextMessageTarget::Server) Ok(SendTextMessageTarget::Server)
} else if let Some(client) = &self.client { } else if let Some(client) = &self.client {
if let Some(client) = wrappers::find_client(connection, client, self.strict_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()); return Err("Could not find client.".to_string());
} else { } else {
@ -119,10 +119,10 @@ impl MessageArgs {
} }
impl MoveArgs { impl MoveArgs {
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> { pub fn channel(&self, connection: &mut Telnet) -> Result<Option<Channel>, String> {
wrappers::find_channel(connection, &self.channel, self.strict_channel) wrappers::find_channel(connection, &self.channel, self.strict_channel)
} }
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> { pub fn client(&self, connection: &mut Telnet) -> Result<Option<Client>, String> {
match &self.client { match &self.client {
Some(client) => { Some(client) => {
wrappers::find_client(connection, client, self.strict_client) wrappers::find_client(connection, client, self.strict_client)

View file

@ -17,7 +17,7 @@ fn read_part(connection: &mut Telnet) -> Result<String, String> {
Ok(event) => { Ok(event) => {
match event { match event {
Data(bytes) => Ok(String::from_utf8(bytes.to_vec()) 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.")) 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) Ok(Response::Ok)
} }
} else { } else {
Err(format!("Received error response from Teamspeak: {} ({})", err.msg, err.id)) Err(format!("Received error models from Teamspeak: {} ({})", err.msg, err.id))
} }
} }
} }

View file

@ -3,17 +3,18 @@ use std::process::exit;
use telnet::Telnet; use telnet::Telnet;
use crate::cli::Commands; use crate::cli::Commands;
use crate::response::channel::ResponseChannel; use crate::models::Channel;
use crate::response::client::ResponseClient; use crate::models::Client;
mod response;
mod wrappers; mod wrappers;
mod commands; mod commands;
mod parameter; mod parameter;
mod cli; mod cli;
mod utils; mod utils;
mod models;
mod response;
fn channel_or_exit(channel_res: Result<Option<ResponseChannel>, String>) -> ResponseChannel { fn channel_or_exit(channel_res: Result<Option<Channel>, String>) -> Channel {
channel_res.unwrap_or_else(|err| { channel_res.unwrap_or_else(|err| {
println!("Failed to find channel: {}", err); println!("Failed to find channel: {}", err);
exit(1); exit(1);
@ -24,7 +25,7 @@ fn channel_or_exit(channel_res: Result<Option<ResponseChannel>, String>) -> Resp
}) })
} }
fn client_or_exit(client_res: Result<Option<ResponseClient>, String>) -> ResponseClient { fn client_or_exit(client_res: Result<Option<Client>, String>) -> Client {
client_res.unwrap_or_else(|err| { client_res.unwrap_or_else(|err| {
println!("Failed to find client: {}", err); println!("Failed to find client: {}", err);
exit(1); exit(1);

View file

@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
use crate::parameter::{parameter_find, ParameterList}; use crate::parameter::{parameter_find, ParameterList};
#[derive(Debug)] #[derive(Debug)]
pub struct ResponseChannel { pub struct Channel {
pub cid: i32, pub cid: i32,
pub pid: i32, pub pid: i32,
pub channel_order: i32, pub channel_order: i32,
@ -12,15 +12,15 @@ pub struct ResponseChannel {
pub channel_flag_are_subscribed: bool, pub channel_flag_are_subscribed: bool,
} }
impl Display for ResponseChannel { impl Display for Channel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.channel_name, self.cid) write!(f, "{} ({})", self.channel_name, self.cid)
} }
} }
impl From<ParameterList> for ResponseChannel { impl From<ParameterList> for Channel {
fn from(value: ParameterList) -> Self { fn from(value: ParameterList) -> Self {
ResponseChannel { Channel {
cid: parameter_find(&value, "cid") cid: parameter_find(&value, "cid")
.unwrap_or_default() .unwrap_or_default()
.to_i32(-1), .to_i32(-1),
@ -43,7 +43,7 @@ impl From<ParameterList> for ResponseChannel {
} }
} }
impl ResponseChannel { impl Channel {
pub fn is_spacer(&self) -> bool { pub fn is_spacer(&self) -> bool {
self.channel_name.starts_with("[spacer") self.channel_name.starts_with("[spacer")
|| self.channel_name.starts_with("[*spacer") || self.channel_name.starts_with("[*spacer")
@ -52,19 +52,19 @@ impl ResponseChannel {
|| self.channel_name.starts_with("[rspacer") || self.channel_name.starts_with("[rspacer")
} }
fn list_find_next(list: &mut Vec<ResponseChannel>, order: i32, pid: i32) -> Option<ResponseChannel> { fn list_find_next(list: &mut Vec<Channel>, order: i32, pid: i32) -> Option<Channel> {
let index = list.iter().position(|a| a.channel_order == order && a.pid == pid)?; let index = list.iter().position(|a| a.channel_order == order && a.pid == pid)?;
Some(list.swap_remove(index)) Some(list.swap_remove(index))
} }
pub fn sort_list(mut list: Vec<ResponseChannel>) -> Vec<ResponseChannel> { pub fn sort_list(mut list: Vec<Channel>) -> Vec<Channel> {
let mut list_sorted: Vec<ResponseChannel> = Vec::new(); let mut list_sorted: Vec<Channel> = Vec::new();
let mut pids: Vec<i32> = Vec::new(); let mut pids: Vec<i32> = Vec::new();
let mut pid = 0; let mut pid = 0;
let mut order = 0; let mut order = 0;
while !list.is_empty() { while !list.is_empty() {
match ResponseChannel::list_find_next(&mut list, order, pid) { match Channel::list_find_next(&mut list, order, pid) {
None => { None => {
order = pid; order = pid;
pid = pids.pop().unwrap_or(0); pid = pids.pop().unwrap_or(0);

View file

@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
use crate::parameter::{parameter_find, ParameterList}; use crate::parameter::{parameter_find, ParameterList};
#[derive(Debug)] #[derive(Debug)]
pub struct ResponseClient { pub struct Client {
pub cid: i32, pub cid: i32,
pub clid: i32, pub clid: i32,
pub client_database_id: i32, pub client_database_id: i32,
@ -11,15 +11,15 @@ pub struct ResponseClient {
pub client_type: i32, pub client_type: i32,
} }
impl Display for ResponseClient { impl Display for Client {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.client_nickname, self.clid) write!(f, "{} ({})", self.client_nickname, self.clid)
} }
} }
impl From<ParameterList> for ResponseClient { impl From<ParameterList> for Client {
fn from(value: ParameterList) -> Self { fn from(value: ParameterList) -> Self {
ResponseClient { Client {
cid: parameter_find(&value, "cid") cid: parameter_find(&value, "cid")
.unwrap_or_default() .unwrap_or_default()
.to_i32(-1), .to_i32(-1),

5
src/models/mod.rs Normal file
View file

@ -0,0 +1,5 @@
mod channel;
mod client;
pub use channel::Channel;
pub use client::Client;

View file

@ -2,9 +2,6 @@ use std::fmt::{Debug, Display, Formatter};
use crate::parameter::*; use crate::parameter::*;
pub mod channel;
pub mod client;
pub enum Response { pub enum Response {
Ok, Ok,
Data(ParameterList), Data(ParameterList),

View file

@ -1,3 +1,4 @@
use crate::models::Client;
use crate::parameter::Parameter; use crate::parameter::Parameter;
pub fn decode_value(value: &str) -> String { pub fn decode_value(value: &str) -> String {
@ -13,7 +14,7 @@ pub fn encode_value(value: &str) -> String {
} }
pub enum SendTextMessageTarget { pub enum SendTextMessageTarget {
Client(i32), Client(Client),
Channel, Channel,
Server, Server,
} }
@ -27,7 +28,7 @@ impl From<SendTextMessageTarget> for String {
}; };
let target = match value { let target = match value {
SendTextMessageTarget::Client(id) => id.to_string(), SendTextMessageTarget::Client(client) => client.clid.to_string(),
_ => String::from("0"), _ => String::from("0"),
}; };

View file

@ -6,8 +6,8 @@ use telnet::Telnet;
use crate::{commands, parameter}; use crate::{commands, parameter};
use crate::parameter::{parameter_list_find_all, ParameterList}; use crate::parameter::{parameter_list_find_all, ParameterList};
use crate::response::channel::ResponseChannel; use crate::models::Channel;
use crate::response::client::ResponseClient; use crate::models::Client;
use crate::response::Response; use crate::response::Response;
use crate::utils::SendTextMessageTarget; use crate::utils::SendTextMessageTarget;
@ -41,101 +41,101 @@ pub fn login(connection: &mut Telnet) {
} }
} }
pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result<Vec<ResponseChannel>, String> { pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result<Vec<Channel>, String> {
match commands::channellist(connection) { match commands::channellist(connection) {
Ok(response) => { Ok(response) => {
match response { match response {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
let channels: Vec<ResponseChannel> = parameter_lists.iter() let channels: Vec<Channel> = parameter_lists.iter()
.map(|params| ResponseChannel::from(params.clone())) .map(|params| Channel::from(params.clone()))
.collect(); .collect();
let mut channels = ResponseChannel::sort_list(channels); let mut channels = Channel::sort_list(channels);
if !spacers { if !spacers {
channels.retain(|c| !c.is_spacer()); channels.retain(|c| !c.is_spacer());
} }
Ok(channels) Ok(channels)
} }
_ => Err(String::from("Received unexpected response from Teamspeak.")) _ => Err(String::from("Received unexpected models from Teamspeak."))
} }
} }
Err(msg) => Err(msg) Err(msg) => Err(msg)
} }
} }
pub fn find_channel(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<ResponseChannel>, String> { pub fn find_channel(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<Channel>, String> {
match commands::channellist(connection) { match commands::channellist(connection) {
Ok(response) => { Ok(response) => {
match response { match response {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
match parameter::parameter_list_find(&parameter_lists, "channel_name", name, strict) { match parameter::parameter_list_find(&parameter_lists, "channel_name", name, strict) {
Some(params) => { Some(params) => {
Ok(Some(ResponseChannel::from(params))) Ok(Some(Channel::from(params)))
} }
None => { None => {
Ok(None) Ok(None)
} }
} }
} }
_ => Err(String::from("Received unexpected response from Teamspeak.")) _ => Err(String::from("Received unexpected models from Teamspeak."))
} }
} }
Err(msg) => Err(msg) Err(msg) => Err(msg)
} }
} }
pub fn get_clients(connection: &mut Telnet) -> Result<Vec<ResponseClient>, String> { pub fn get_clients(connection: &mut Telnet) -> Result<Vec<Client>, String> {
match commands::clientlist(connection) { match commands::clientlist(connection) {
Ok(response) => { Ok(response) => {
match response { match response {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
let mut clients: Vec<ResponseClient> = parameter_lists.iter() let mut clients: Vec<Client> = parameter_lists.iter()
.map(|params| ResponseClient::from(params.clone())) .map(|params| Client::from(params.clone()))
.collect(); .collect();
clients.sort_by(|a, b| a.client_nickname.cmp(&b.client_nickname)); clients.sort_by(|a, b| a.client_nickname.cmp(&b.client_nickname));
Ok(clients) Ok(clients)
} }
_ => Err(String::from("Received unexpected response from Teamspeak.")) _ => Err(String::from("Received unexpected models from Teamspeak."))
} }
} }
Err(msg) => Err(msg) Err(msg) => Err(msg)
} }
} }
pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<ResponseClient>, String> { pub fn find_client(connection: &mut Telnet, name: &str, strict: bool) -> Result<Option<Client>, String> {
match commands::clientlist(connection) { match commands::clientlist(connection) {
Ok(response) => { Ok(response) => {
match response { match response {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
match parameter::parameter_list_find(&parameter_lists, "client_nickname", name, strict) { match parameter::parameter_list_find(&parameter_lists, "client_nickname", name, strict) {
Some(params) => { Some(params) => {
Ok(Some(ResponseClient::from(params))) Ok(Some(Client::from(params)))
} }
None => { None => {
Ok(None) Ok(None)
} }
} }
} }
_ => Err(String::from("Received unexpected response from Teamspeak.")) _ => Err(String::from("Received unexpected models from Teamspeak."))
} }
} }
Err(msg) => Err(msg) Err(msg) => Err(msg)
} }
} }
pub fn get_channel_clients(connection: &mut Telnet, channel: &ResponseChannel) -> Result<Vec<ResponseClient>, String> { pub fn get_channel_clients(connection: &mut Telnet, channel: &Channel) -> Result<Vec<Client>, String> {
match commands::clientlist(connection) { match commands::clientlist(connection) {
Ok(response) => { Ok(response) => {
match response { match response {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
let mut clients: Vec<ResponseClient> = Vec::new(); let mut clients: Vec<Client> = Vec::new();
for client_params in parameter_list_find_all(&parameter_lists, "cid", &channel.cid.to_string(), true) { for client_params in parameter_list_find_all(&parameter_lists, "cid", &channel.cid.to_string(), true) {
clients.push(ResponseClient::from(client_params)); clients.push(Client::from(client_params));
} }
Ok(clients) Ok(clients)
} }
_ => Err(String::from("Received unexpected response from Teamspeak.")) _ => Err(String::from("Received unexpected models from Teamspeak."))
} }
} }
Err(msg) => Err(msg) Err(msg) => Err(msg)
@ -148,18 +148,18 @@ fn get_self_clid(connection: &mut Telnet) -> Result<String, String> {
match response { match response {
Response::Data(params) => { Response::Data(params) => {
match parameter::parameter_find(&params, "clid") { match parameter::parameter_find(&params, "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) 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) Err(msg) => Err(msg)
} }
} }
pub fn find_self(connection: &mut Telnet) -> Result<ResponseClient, String> { pub fn find_self(connection: &mut Telnet) -> Result<Client, String> {
let clid = get_self_clid(connection)?; let clid = get_self_clid(connection)?;
match commands::clientlist(connection) { match commands::clientlist(connection) {
@ -168,28 +168,28 @@ pub fn find_self(connection: &mut Telnet) -> Result<ResponseClient, String> {
Response::DataList(parameter_lists) => { Response::DataList(parameter_lists) => {
match parameter::parameter_list_find(&parameter_lists, "clid", &clid, false) { match parameter::parameter_list_find(&parameter_lists, "clid", &clid, false) {
Some(params) => { Some(params) => {
Ok(ResponseClient::from(params)) Ok(Client::from(params))
} }
None => { 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) Err(msg) => Err(msg)
} }
} }
pub fn fetch_client(connection: &mut Telnet, clients: &[ResponseClient]) -> Result<Response, String> { pub fn fetch_client(connection: &mut Telnet, clients: &[Client]) -> Result<Response, String> {
let cid = find_self(connection)?.cid; let cid = find_self(connection)?.cid;
let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect(); let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect();
commands::clientmove(connection, &cid, clid_list) commands::clientmove(connection, &cid, clid_list)
} }
pub fn fetch_channel(connection: &mut Telnet, channel: ResponseChannel) -> Result<Response, String> { pub fn fetch_channel(connection: &mut Telnet, channel: Channel) -> Result<Response, String> {
let cid = find_self(connection)?.cid; let cid = find_self(connection)?.cid;
let clients = get_channel_clients(connection, &channel)?; 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) commands::clientmove(connection, &cid, clid_list)
} }
pub fn move_client(connection: &mut Telnet, channel: &ResponseChannel, clients: &[ResponseClient]) -> Result<Response, String> { pub fn move_client(connection: &mut Telnet, channel: &Channel, clients: &[Client]) -> Result<Response, String> {
let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect(); let clid_list: Vec<&i32> = clients.iter().map(|c| &c.clid).collect();
commands::clientmove(connection, &channel.cid, clid_list) commands::clientmove(connection, &channel.cid, clid_list)