Skip to content

Commit

Permalink
GH-29: Replace error handling result with type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
SetZero committed May 17, 2023
1 parent b3c57ef commit 1e8b4d3
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 95 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,3 @@ jobs:
- name: Build the app
uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
releaseId: ${{ needs.create-release.outputs.release_id }}
26 changes: 9 additions & 17 deletions src-tauri/src/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod threads;
pub mod traits;
use crate::connection::traits::Shutdown;
use crate::errors::AnyError;
use crate::mumble;
use crate::protocol::init_connection;
use crate::protocol::stream_reader::StreamReader;
Expand All @@ -11,7 +12,6 @@ use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine;
use std::collections::HashMap;
use std::error::Error;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use tauri::PackageInfo;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Connection {

async fn setup_connection(
&mut self,
) -> Result<Option<tokio_native_tls::TlsStream<TcpStream>>, Box<dyn Error>> {
) -> AnyError<Option<tokio_native_tls::TlsStream<TcpStream>>> {
let server_uri = format!(
"{}:{}",
self.server_data.server_host, self.server_data.server_port
Expand All @@ -110,7 +110,7 @@ impl Connection {
))
}

pub async fn connect(&mut self) -> Result<(), Box<dyn Error>> {
pub async fn connect(&mut self) -> AnyError<()> {
{
self.running
.store(true, std::sync::atomic::Ordering::Relaxed);
Expand All @@ -127,11 +127,7 @@ impl Connection {
Ok(())
}

pub fn send_message(
&self,
channel_id: Option<u32>,
message: &str,
) -> Result<(), Box<dyn Error>> {
pub fn send_message(&self, channel_id: Option<u32>, message: &str) -> AnyError<()> {
self.tx_message_channel.send(TextMessage {
message: message.to_string(),
channel_id,
Expand All @@ -145,7 +141,7 @@ impl Connection {
}

//TODO: Move to output Thread
pub fn like_message(&self, message_id: &str) -> Result<(), Box<dyn Error>> {
pub fn like_message(&self, message_id: &str) -> AnyError<()> {
let like_message = mumble::proto::PluginDataTransmission {
sender_session: None,
receiver_sessions: Vec::new(),
Expand All @@ -157,11 +153,7 @@ impl Connection {
Ok(())
}

pub async fn set_user_image(
&self,
image_path: &str,
image_type: &str,
) -> Result<(), Box<dyn Error>> {
pub async fn set_user_image(&self, image_path: &str, image_type: &str) -> AnyError<()> {
let image = get_file_as_byte_vec(image_path).await?;

match image_type {
Expand Down Expand Up @@ -189,7 +181,7 @@ impl Connection {
Ok(())
}

pub fn join_channel(&self, channel_id: u32) -> Result<(), Box<dyn Error>> {
pub fn join_channel(&self, channel_id: u32) -> AnyError<()> {
let join_channel = mumble::proto::UserState {
session: None,
actor: None,
Expand Down Expand Up @@ -220,14 +212,14 @@ impl Connection {
Ok(())
}

pub fn update_user_info(&self) -> Result<(), Box<dyn Error>> {
pub fn update_user_info(&self) -> AnyError<()> {
todo!()
}
}

#[async_trait]
impl Shutdown for Connection {
async fn shutdown(&mut self) -> Result<(), Box<dyn Error>> {
async fn shutdown(&mut self) -> AnyError<()> {
info!("Sending Shutdown Request");
self.running
.store(false, std::sync::atomic::Ordering::Relaxed);
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/connection/threads/main_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use async_trait::async_trait;

use crate::connection::{Connection, BUFFER_SIZE};
use crate::errors::application_error::ApplicationError;
use crate::errors::AnyError;

use super::{ConnectionThread, MainThread, DEADMAN_INTERVAL};
use std::cmp;
use std::error::Error;
use std::sync::atomic::Ordering;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
Expand All @@ -19,7 +19,7 @@ impl MainThread for Connection {
async fn init_main_thread(
&mut self,
stream: Option<tokio_native_tls::TlsStream<TcpStream>>,
) -> Result<(), Box<dyn Error>> {
) -> AnyError<()> {
if self.threads.get(&ConnectionThread::Main).is_some() {
return Err(Box::new(ApplicationError::new(
"MainThread already running",
Expand Down
6 changes: 4 additions & 2 deletions src-tauri/src/connection/threads/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ mod input_thread;
mod main_thread;
mod output_thread;
mod ping_thread;
use std::{error::Error, time::Duration};
use std::time::Duration;
use tokio::net::TcpStream;

use crate::errors::AnyError;

pub const DEADMAN_INTERVAL: Duration = Duration::from_millis(500);

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -45,5 +47,5 @@ pub trait MainThread {
async fn init_main_thread(
&mut self,
stream: Option<tokio_native_tls::TlsStream<TcpStream>>,
) -> Result<(), Box<dyn Error>>;
) -> AnyError<()>;
}
6 changes: 3 additions & 3 deletions src-tauri/src/connection/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::error::Error;

use async_trait::async_trait;

use crate::errors::AnyError;

#[async_trait]
pub trait Shutdown {
async fn shutdown(&mut self) -> Result<(), Box<dyn Error>>;
async fn shutdown(&mut self) -> AnyError<()>;
}

#[async_trait]
Expand Down
4 changes: 4 additions & 0 deletions src-tauri/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
use std::error::Error;

pub mod application_error;
pub mod voice_error;

pub type AnyError<T> = Result<T, Box<dyn Error>>;
7 changes: 3 additions & 4 deletions src-tauri/src/manager/channel.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::{
collections::{hash_map::Entry, HashMap},
error::Error,
mem,
};

use serde::Serialize;
use tracing::{debug, error, info};

use crate::{
mumble, protocol::serialize::message_container::FrontendMessage,
errors::AnyError, mumble, protocol::serialize::message_container::FrontendMessage,
utils::messages::message_builder,
};

Expand Down Expand Up @@ -103,7 +102,7 @@ impl Manager {
&self,
channel_info: &Data,
description_hash: &Vec<u8>,
) -> Result<(), Box<dyn Error>> {
) -> AnyError<()> {
let channel_id = channel_info.channel_id;
let cached_channel_description_hash = &self
.channels
Expand Down Expand Up @@ -152,7 +151,7 @@ impl Manager {
pub fn update_channel(
&mut self,
channel_info: &mut mumble::proto::ChannelState,
) -> Result<(), Box<dyn Error>> {
) -> AnyError<()> {
let has_description = channel_info.description.is_some()
&& !channel_info
.description
Expand Down
9 changes: 3 additions & 6 deletions src-tauri/src/manager/text_message.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{
error::Error,
time::{SystemTime, UNIX_EPOCH},
};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::Serialize;
use tokio::sync::broadcast::Sender;
use tracing::error;

use crate::{mumble, protocol::serialize::message_container::FrontendMessage};
use crate::{errors::AnyError, mumble, protocol::serialize::message_container::FrontendMessage};

use super::user::User;

Expand Down Expand Up @@ -71,7 +68,7 @@ impl Manager {
&mut self,
text_message: mumble::proto::TextMessage,
user: &User,
) -> Result<(), Box<dyn Error>> {
) -> AnyError<()> {
let message = TextMessage {
sender: SenderInfo {
user_id: user.id,
Expand Down
16 changes: 4 additions & 12 deletions src-tauri/src/manager/user.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use base64::{engine::general_purpose, Engine as _};
use std::{
collections::{hash_map::Entry, HashMap},
error::Error,
mem,
};

use serde::Serialize;
use tracing::{debug, error, info, trace};

use crate::{
mumble, protocol::serialize::message_container::FrontendMessage,
errors::AnyError, mumble, protocol::serialize::message_container::FrontendMessage,
utils::messages::message_builder,
};

Expand Down Expand Up @@ -138,7 +137,7 @@ impl Manager {
}
}

fn fill_user_images(&self, user_id: u32, texture_hash: &Vec<u8>) -> Result<(), Box<dyn Error>> {
fn fill_user_images(&self, user_id: u32, texture_hash: &Vec<u8>) -> AnyError<()> {
let user = self.users.get(&user_id);
if user.is_none() {
return Err(format!("User {user_id} not found").into());
Expand Down Expand Up @@ -172,11 +171,7 @@ impl Manager {
Ok(())
}

fn fill_user_comment(
&self,
user_id: u32,
comment_hash: &Vec<u8>,
) -> Result<(), Box<dyn Error>> {
fn fill_user_comment(&self, user_id: u32, comment_hash: &Vec<u8>) -> AnyError<()> {
let cached_user_comment_hash = &self
.users
.get(&user_id)
Expand Down Expand Up @@ -207,10 +202,7 @@ impl Manager {
Ok(())
}

pub fn update_user(
&mut self,
user_info: &mut mumble::proto::UserState,
) -> Result<(), Box<dyn Error>> {
pub fn update_user(&mut self, user_info: &mut mumble::proto::UserState) -> AnyError<()> {
let has_texture = user_info.texture.is_some()
&& !user_info
.texture
Expand Down
15 changes: 5 additions & 10 deletions src-tauri/src/manager/voice.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::errors::AnyError;
use crate::protocol::serialize::message_container::FrontendMessage;
use crate::utils::audio;
use crate::utils::audio::player::Player;
use crate::{connection::traits::Shutdown, errors::voice_error::VoiceError};
use async_trait::async_trait;
use serde::Serialize;
use std::{
collections::{hash_map::Entry, HashMap},
error::Error,
};
use std::collections::{hash_map::Entry, HashMap};
use tokio::sync::broadcast::Sender;
use tracing::error;

Expand All @@ -29,10 +27,7 @@ pub struct Manager {
}

impl Manager {
pub fn new(
send_to: Sender<String>,
server_channel: Sender<Vec<u8>>,
) -> Result<Self, Box<dyn Error>> {
pub fn new(send_to: Sender<String>, server_channel: Sender<Vec<u8>>) -> AnyError<Self> {
let mut player = Player::new();
if let Err(error) = player.start() {
error!("Failed to start audio player: {}", error);
Expand Down Expand Up @@ -65,7 +60,7 @@ impl Manager {
}
}

pub fn notify_audio(&mut self, audio_data: &[u8]) -> Result<(), Box<dyn Error>> {
pub fn notify_audio(&mut self, audio_data: &[u8]) -> AnyError<()> {
let audio_data = self.decoder.decode_audio(audio_data)?;
self.send_taking_information(audio_data.user_id, audio_data.talking);
if let Err(error) = self
Expand Down Expand Up @@ -104,7 +99,7 @@ impl Manager {

#[async_trait]
impl Shutdown for Manager {
async fn shutdown(&mut self) -> Result<(), Box<dyn Error>> {
async fn shutdown(&mut self) -> AnyError<()> {
self.audio_player.stop();

Ok(())
Expand Down
15 changes: 6 additions & 9 deletions src-tauri/src/protocol/message_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::{error, trace};

use crate::{
connection::{traits::Shutdown, MessageChannels},
errors::application_error::ApplicationError,
errors::{application_error::ApplicationError, AnyError},
manager::{
channel::{self},
connection_state, text_message,
Expand All @@ -27,10 +27,7 @@ pub struct MessageRouter {
}

impl MessageRouter {
pub fn new(
sender: MessageChannels,
server_channel: Sender<Vec<u8>>,
) -> Result<Self, Box<dyn Error>> {
pub fn new(sender: MessageChannels, server_channel: Sender<Vec<u8>>) -> AnyError<Self> {
Ok(Self {
user_manager: user::Manager::new(
sender.message_channel.clone(),
Expand All @@ -49,7 +46,7 @@ impl MessageRouter {
})
}

fn handle_downcast<T: 'static>(message_info: MessageInfo) -> Result<T, Box<dyn Error>> {
fn handle_downcast<T: 'static>(message_info: MessageInfo) -> AnyError<T> {
match message_info.message_data.downcast::<T>() {
Ok(a) => Ok(*a),
Err(e) => Err(Box::new(ApplicationError::new(
Expand All @@ -58,7 +55,7 @@ impl MessageRouter {
}
}

fn handle_text_message(&mut self, message: MessageInfo) -> Result<(), Box<dyn Error>> {
fn handle_text_message(&mut self, message: MessageInfo) -> AnyError<()> {
let text_message = Self::handle_downcast::<mumble::proto::TextMessage>(message)?;
match text_message.actor {
Some(actor) => {
Expand All @@ -75,7 +72,7 @@ impl MessageRouter {
Ok(())
}

pub fn recv_message(&mut self, message: MessageInfo) -> Result<(), Box<dyn Error>> {
pub fn recv_message(&mut self, message: MessageInfo) -> AnyError<()> {
if message.message_type != crate::utils::messages::MessageTypes::UdpTunnel {
trace!("Received message: {:<100?}", message);
}
Expand Down Expand Up @@ -142,7 +139,7 @@ impl MessageRouter {
Ok(())
}

pub async fn shutdown(&mut self) -> Result<(), Box<dyn Error>> {
pub async fn shutdown(&mut self) -> AnyError<()> {
self.voice_manager.shutdown().await?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/protocol/message_transmitter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::error::Error;
use std::sync::{Arc, RwLock};

use crate::connection::threads::DEADMAN_INTERVAL;
use crate::connection::traits::{HandleMessage, Shutdown};
use crate::errors::AnyError;
use async_trait::async_trait;
use tauri::Manager;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl MessageTransmitter {

#[async_trait]
impl Shutdown for MessageTransmitter {
async fn shutdown(&mut self) -> Result<(), Box<dyn Error>> {
async fn shutdown(&mut self) -> AnyError<()> {
trace!("Sending Shutdown Request");
if let Ok(mut running) = self.running.write() {
*running = false;
Expand Down
Loading

0 comments on commit 1e8b4d3

Please sign in to comment.