Skip to content

Commit

Permalink
refactor: move AppError to lib
Browse files Browse the repository at this point in the history
Setup handle event functions to return AppError instead of generic
  • Loading branch information
roosta committed Dec 14, 2024
1 parent 697207a commit 3f43798
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 53 deletions.
59 changes: 55 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,55 @@ pub mod regex;

use config::Config;
use std::error::Error;
use std::fmt;
use std::io;

#[derive(Debug)]
pub enum AppError {
Config(config::ConfigError),
Connection(swayipc::Error),
Regex(regex::RegexError),
Event(String),
IoError(io::Error),
}

impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppError::Config(e) => write!(f, "Configuration error: {}", e),
AppError::Connection(e) => write!(f, "IPC connection error: {}", e),
AppError::Regex(e) => write!(f, "Regex compilation error: {}", e),
AppError::Event(e) => write!(f, "Event handling error: {}", e),
AppError::IoError(e) => write!(f, "IO error: {}", e),
}
}
}

impl Error for AppError {}

impl From<config::ConfigError> for AppError {
fn from(err: config::ConfigError) -> Self {
AppError::Config(err)
}
}

impl From<swayipc::Error> for AppError {
fn from(err: swayipc::Error) -> Self {
AppError::Connection(err)
}
}

impl From<regex::RegexError> for AppError {
fn from(err: regex::RegexError) -> Self {
AppError::Regex(err)
}
}

impl From<io::Error> for AppError {
fn from(err: io::Error) -> Self {
AppError::IoError(err)
}
}

/// Helper fn to get options via config
fn get_option(config: &Config, key: &str) -> bool {
Expand Down Expand Up @@ -225,10 +274,11 @@ pub fn handle_window_event(
conn: &mut Connection,
config: &Config,
res: &regex::Compiled,
) -> Result<(), Box<dyn Error>> {
) -> Result<(), AppError> {
match e.change {
WindowChange::New | WindowChange::Close | WindowChange::Move | WindowChange::Title => {
update_tree(conn, config, res)?;
update_tree(conn, config, res)
.map_err(|e| AppError::Event(format!("Tree update failed: {}", e)))?;
}
_ => (),
}
Expand All @@ -241,10 +291,11 @@ pub fn handle_ws_event(
conn: &mut Connection,
config: &Config,
res: &regex::Compiled,
) -> Result<(), Box<dyn Error>> {
) -> Result<(), AppError> {
match e.change {
WorkspaceChange::Empty | WorkspaceChange::Focus => {
update_tree(conn, config, res)?;
update_tree(conn, config, res)
.map_err(|e| AppError::Event(format!("Tree update failed: {}", e)))?;
}
_ => (),
}
Expand Down
50 changes: 1 addition & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,10 @@ use clap::{Parser, ValueEnum};
use dirs::config_dir;
use swayipc::{Connection, Event, EventType, Fallible};
use i3wsr::config::{Config, ConfigError};
use std::error::Error;
use std::fmt;
use std::io;
use std::path::Path;

#[derive(Debug)]
enum AppError {
Config(ConfigError),
Connection(swayipc::Error),
Regex(i3wsr::regex::RegexError),
Event(String),
IoError(io::Error),
}

impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppError::Config(e) => write!(f, "Configuration error: {}", e),
AppError::Connection(e) => write!(f, "IPC connection error: {}", e),
AppError::Regex(e) => write!(f, "Regex compilation error: {}", e),
AppError::Event(e) => write!(f, "Event handling error: {}", e),
AppError::IoError(e) => write!(f, "IO error: {}", e),
}
}
}

impl Error for AppError {}

impl From<ConfigError> for AppError {
fn from(err: ConfigError) -> Self {
AppError::Config(err)
}
}

impl From<swayipc::Error> for AppError {
fn from(err: swayipc::Error) -> Self {
AppError::Connection(err)
}
}

impl From<i3wsr::regex::RegexError> for AppError {
fn from(err: i3wsr::regex::RegexError) -> Self {
AppError::Regex(err)
}
}

impl From<io::Error> for AppError {
fn from(err: io::Error) -> Self {
AppError::IoError(err)
}
}

use i3wsr::AppError;

/// Window property types for display
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
Expand Down

0 comments on commit 3f43798

Please sign in to comment.