Skip to content

Commit

Permalink
Refactor codebase to be more testable & add some initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Aug 26, 2021
1 parent 1ca3cd7 commit ba6ebcf
Show file tree
Hide file tree
Showing 31 changed files with 2,437 additions and 1,086 deletions.
125 changes: 125 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ fork = "0.1.18"
lazy_static = "1.4.0"
structopt = "0.3.22"
whoami = "1.1.2"

[dev-dependencies]
assert_cmd = "2.0.0"
tempfile = "3.2.0"
52 changes: 40 additions & 12 deletions src/cli/exit.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
use crate::core::net::TransportError;
use crate::core::{client::RemoteProcessError, net::TransportError};

/// Exit codes following https://www.freebsd.org/cgi/man.cgi?query=sysexits&sektion=3
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum ExitCode {
/// EX_USAGE (64) - being used when arguments missing or bad arguments provided to CLI
Usage = 64,
Usage,

/// EX_DATAERR (65) - being used when bad data received not in UTF-8 format or transport data
/// is bad
DataErr = 65,
DataErr,

/// EX_NOINPUT (66) - being used when not getting expected data from launch
NoInput = 66,
NoInput,

/// EX_NOHOST (68) - being used when failed to resolve a host
NoHost = 68,
NoHost,

/// EX_UNAVAILABLE (69) - being used when IO error encountered where connection is problem
Unavailable = 69,
Unavailable,

/// EX_SOFTWARE (70) - being used for internal errors that can occur like joining a task
Software = 70,
Software,

/// EX_OSERR (71) - being used when fork failed
OsErr = 71,
OsErr,

/// EX_IOERR (74) - being used as catchall for IO errors
IoError = 74,
IoError,

/// EX_TEMPFAIL (75) - being used when we get a timeout
TempFail = 75,
TempFail,

/// EX_PROTOCOL (76) - being used as catchall for transport errors
Protocol = 76,
Protocol,

/// Custom exit code to pass back verbatim
Custom(i32),
}

/// Represents an error that can be converted into an exit code
pub trait ExitCodeError: std::error::Error {
fn to_exit_code(&self) -> ExitCode;

fn to_i32(&self) -> i32 {
self.to_exit_code() as i32
match self.to_exit_code() {
ExitCode::Usage => 64,
ExitCode::DataErr => 65,
ExitCode::NoInput => 66,
ExitCode::NoHost => 68,
ExitCode::Unavailable => 69,
ExitCode::Software => 70,
ExitCode::OsErr => 71,
ExitCode::IoError => 74,
ExitCode::TempFail => 75,
ExitCode::Protocol => 76,
ExitCode::Custom(x) => x,
}
}
}

Expand Down Expand Up @@ -68,6 +83,19 @@ impl ExitCodeError for TransportError {
}
}

impl ExitCodeError for RemoteProcessError {
fn to_exit_code(&self) -> ExitCode {
match self {
Self::BadResponse => ExitCode::DataErr,
Self::ChannelDead => ExitCode::Unavailable,
Self::Overloaded => ExitCode::Software,
Self::TransportError(x) => x.to_exit_code(),
Self::UnexpectedEof => ExitCode::IoError,
Self::WaitFailed(_) => ExitCode::Software,
}
}
}

impl<T: ExitCodeError + 'static> From<T> for Box<dyn ExitCodeError> {
fn from(x: T) -> Self {
Box::new(x)
Expand Down

0 comments on commit ba6ebcf

Please sign in to comment.