Fix some small issues

Don't crash without self_clid
Improve functions in main.rs
This commit is contained in:
Tobias Reisinger 2024-06-03 01:11:05 +02:00
parent 70e0a1d0b4
commit 895f5f9373
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
3 changed files with 28 additions and 25 deletions

View file

@ -2,11 +2,13 @@
INSTALL_DIR = $(HOME)/.local/bin
export PATH := target/debug:$(PATH)
build:
@cargo build
run: build
@PATH=$(PWD)/target/debug:$(PATH) ./ts-control
./ts-control
install:
mkdir -p "$(INSTALL_DIR)"

View file

@ -27,9 +27,19 @@ pub fn handle_event_response(connection: &mut Telnet, event: Event, known_client
}
}
pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) {
fn try_get_self_clid(connection: &mut Telnet) -> Option<i32> {
wrappers::get_self_clid(connection)
.unwrap_or_default()
.parse()
.map(Some)
.unwrap_or(None)
}
pub fn loop_response_reader(connection: &mut Telnet) {
let mut known_clients = wrappers::get_clients(connection).unwrap_or_else(|_| Vec::new());
let mut self_clid: Option<i32> = try_get_self_clid(connection);
loop {
match commands::read_response(connection, true, String::new()) {
Ok(response) => {
@ -37,7 +47,10 @@ pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) {
let event = Event::new(connection, event_type, params, &known_clients);
if let Some(client) = &event.client {
if client.clid == self_clid {
if self_clid.is_none() {
self_clid = try_get_self_clid(connection);
}
if Some(client.clid) == self_clid {
continue;
}
}

View file

@ -1,11 +1,8 @@
use std::io::{Error, ErrorKind};
use std::num::ParseIntError;
use telnet::Telnet;
use crate::cli::Commands;
use crate::models::Channel;
use crate::models::Client;
mod wrappers;
mod commands;
@ -16,14 +13,10 @@ mod models;
mod response;
mod command_utils;
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
channel_res.map_err(|err| make_action_error("find channel", err))?
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
}
fn client_or_error(client_res: Result<Option<Client>, String>) -> Result<Client, Error> {
client_res.map_err(|err| make_action_error("find client", err))?
.ok_or_else(|| make_action_error("find client", String::from("Not Found.")))
fn result_or_error<T>(res: Result<Option<T>, String>, result_type: &str) -> Result<T, Error> {
let error_action = format!("find {}", result_type);
res.map_err(|err| make_action_error(&error_action, err))?
.ok_or_else(|| make_action_error(&error_action, String::from("Not Found.")))
}
fn connect() -> Result<Telnet, Error> {
@ -89,7 +82,7 @@ fn main() -> Result<(), Error> {
}
if args.want_client() {
let client = client_or_error(args.client(&mut connection))?;
let client = result_or_error(args.client(&mut connection), "client")?;
match wrappers::fetch_client(&mut connection, &[client]) {
Ok(_) => println!("Successfully fetched client."),
@ -100,7 +93,7 @@ fn main() -> Result<(), Error> {
}
if args.want_channel() {
let channel = channel_or_error(args.channel(&mut connection))?;
let channel = result_or_error(args.channel(&mut connection), "channel")?;
match wrappers::fetch_channel(&mut connection, channel) {
Ok(_) => println!("Successfully fetched channel."),
@ -124,8 +117,8 @@ fn main() -> Result<(), Error> {
}
Commands::Move(args) => {
let channel = channel_or_error(args.channel(&mut connection))?;
let client = client_or_error(args.client(&mut connection))?;
let channel = result_or_error(args.channel(&mut connection), "channel")?;
let client = result_or_error(args.client(&mut connection), "client")?;
match wrappers::move_client(&mut connection, &channel, &[client]) {
Ok(resp) => println!("Successfully moved client: {}", resp),
@ -149,12 +142,7 @@ fn main() -> Result<(), Error> {
command_utils::events::register_events(&mut connection, args.event.clone())
.map_err(to_other_error)?;
let self_clid: i32 = wrappers::get_self_clid(&mut connection)
.map_err(|msg| make_action_error("get self clid", msg))?
.parse()
.map_err(|err: ParseIntError| make_action_error("parse clid", err.to_string()))?;
command_utils::events::loop_response_reader(&mut connection, self_clid);
command_utils::events::loop_response_reader(&mut connection);
// loop_response_reader failed. Let's try to reconnect after 1 second.
std::thread::sleep(std::time::Duration::from_secs(1));