wip: convert to thiserror and anyhow

This commit is contained in:
Suyono 2023-11-26 11:34:26 +11:00
parent c03adb808a
commit aabf39f041
5 changed files with 50 additions and 52 deletions

28
Cargo.lock generated
View File

@ -26,6 +26,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -315,6 +321,26 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio"
version = "1.34.0"
@ -440,9 +466,11 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
name = "wingmate-rs"
version = "0.1.0"
dependencies = [
"anyhow",
"lazy_static",
"nix",
"regex",
"thiserror",
"tokio",
"tokio-util",
]

View File

@ -6,8 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
lazy_static = "1.4.0"
nix = {version = "0.27.1", features = ["process", "signal", "fs"]}
regex = "1.10.2"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["full"] }
tokio-util = "0.7.10"

View File

@ -42,7 +42,7 @@ pub struct Config {
impl Config {
pub fn find(search_path: Vec<String>) -> Result<Config, Box<dyn std_error::Error>> {
if search_path.is_empty() {
return Err(wingmate_error::InvalidConfigSearchPathError.into());
return Err(wingmate_error::WingmateInitError::InvalidConfigSearchPath.into());
}
let mut svc_commands: Vec<Command> = Vec::new();
@ -81,7 +81,7 @@ impl Config {
}
if svc_commands.is_empty() && cron.is_empty() {
return Err(wingmate_error::NoServiceOrCronFoundError.into());
return Err(wingmate_error::WingmateInitError::NoServiceOrCron.into());
}
let mut config = Config {

View File

@ -11,7 +11,7 @@ use nix::sys::signal::{kill, Signal};
use nix::errno::Errno;
use nix::unistd::Pid;
use crate::init::config;
use crate::init::error::{NoShellAvailableError, SpawnError};
use crate::init::error::{NoShellAvailableError, WingmateInitError};
pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken)
@ -32,7 +32,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
config::Command::Direct(c) => {
let exp_str = c.clone();
child = Command::new(c).spawn().map_err(|e| {
Box::new(SpawnError(format!("{}: {}", exp_str, e)))
Box::new(WingmateInitError::SpawnError { source: e, message: exp_str })
})?;
},
config::Command::ShellPrefixed(s) => {
@ -40,7 +40,7 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
let exp_str = s.clone();
let exp_shell = shell.clone();
child = Command::new(shell).arg(s).spawn().map_err(|e| {
Box::new(SpawnError(format!("{} {}: {}", exp_shell, exp_str, e)))
Box::new(WingmateInitError::SpawnError { source: e, message: format!("{} {}", exp_shell, exp_str) })
})?;
}
}

View File

@ -1,40 +1,25 @@
use thiserror::Error;
use std::fmt;
use std::error;
pub enum WingmateErrorKind {
SpawnError,
#[derive(Error,Debug)]
pub enum WingmateInitError {
#[error("invalid config search path")]
InvalidConfigSearchPath,
#[error("no service or cron found")]
NoServiceOrCron,
#[allow(dead_code)]
Other,
}
pub trait WingmateError {
fn wingmate_error_kind(&self) -> WingmateErrorKind;
}
#[derive(Debug, Clone)]
pub struct InvalidConfigSearchPathError;
impl fmt::Display for InvalidConfigSearchPathError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid config search path")
#[error("failed to spawn: {}", message)]
SpawnError {
#[source]
source: std::io::Error,
message: String,
}
}
impl error::Error for InvalidConfigSearchPathError {}
#[derive(Debug,Clone)]
pub struct NoServiceOrCronFoundError;
impl fmt::Display for NoServiceOrCronFoundError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "no service or cron found")
}
}
impl error::Error for NoServiceOrCronFoundError {}
#[derive(Debug,Clone)]
pub struct CronSyntaxError(pub String);
@ -67,20 +52,3 @@ impl fmt::Display for NoShellAvailableError {
}
impl error::Error for NoShellAvailableError {}
#[derive(Debug,Clone)]
pub struct SpawnError(pub String);
impl fmt::Display for SpawnError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to spawn: {}", self.0)
}
}
impl error::Error for SpawnError {}
impl WingmateError for SpawnError {
fn wingmate_error_kind(&self) -> WingmateErrorKind {
WingmateErrorKind::SpawnError
}
}