Skip to content

Commit

Permalink
more sys errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EricDriussi committed Apr 6, 2024
1 parent 1408d48 commit e8aefed
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/authors/csv/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::super::author::{Author, AuthorsProvider};
use super::mapper;
use crate::authors::err::AuthorsError;
use crate::common::conf;
use crate::common::err::SystemError;
use crate::common::fs::file_reader::{Lines, Reader};
use crate::cositas::CustomResult;
use std::env;
Expand Down Expand Up @@ -34,7 +35,7 @@ impl CSVReader {
fn from_cwd_fallback_home(file_reader: &dyn Reader) -> CustomResult<Self> {
let file_path = &conf::authors_file();
let dir_path = &conf::authors_dir();
let cwd = env::current_dir()?;
let cwd = env::current_dir().map_err(|_| SystemError::EnvVar("CWD".to_string()))?;

file_reader
.read_non_empty_lines(&cwd.join(file_path))
Expand Down
37 changes: 29 additions & 8 deletions src/common/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::{any::Any, error::Error, fmt::Display};

#[derive(Debug)]
pub enum SystemError {
Runner(String),
Runner(String, String),
Unknown(String),
Read(String),
Write(String),
EnvVar(String),
}

impl Error for SystemError {}
Expand All @@ -17,19 +20,25 @@ impl CustomError for SystemError {

impl PartialEq for SystemError {
fn eq(&self, other: &Self) -> bool {
matches!((self, other), |(SystemError::Runner(_), SystemError::Runner(_))| (
SystemError::Unknown(_),
SystemError::Unknown(_)
))
matches!((self, other), |(
SystemError::Runner(_, _),
SystemError::Runner(_, _),
)| (SystemError::Unknown(_), SystemError::Unknown(_))
| (SystemError::Read(_), SystemError::Read(_))
| (SystemError::Write(_), SystemError::Write(_))
| (SystemError::EnvVar(_), SystemError::EnvVar(_)))
}
}

impl Display for SystemError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "System failure: ")?;
match self {
SystemError::Runner(err) => write!(f, "Command failed with: {err}"),
SystemError::Runner(cmd, err) => write!(f, "Command {cmd} failed with: {err}"),
SystemError::Unknown(err) => write!(f, "{err}"),
SystemError::Read(err) => write!(f, "Could not read {err}"),
SystemError::Write(err) => write!(f, "Could not write {err}"),
SystemError::EnvVar(var) => write!(f, "Could not get {var} from env var"),
}
}
}
Expand All @@ -41,12 +50,24 @@ mod tests {
#[test]
fn test_system_error_display() {
assert_eq!(
format!("{}", SystemError::Runner("error".to_string())),
"System failure: Command failed with: error"
format!("{}", SystemError::Runner("cmd".to_string(), "error".to_string())),
"System failure: Command cmd failed with: error"
);
assert_eq!(
format!("{}", SystemError::Unknown("oops".to_string())),
"System failure: oops"
);
assert_eq!(
format!("{}", SystemError::Read("file".to_string())),
"System failure: Could not read file"
);
assert_eq!(
format!("{}", SystemError::Write("file".to_string())),
"System failure: Could not write file"
);
assert_eq!(
format!("{}", SystemError::EnvVar("var".to_string())),
"System failure: Could not get var from env var"
);
}
}
14 changes: 8 additions & 6 deletions src/common/fs/file_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cositas::CustomResult;
use crate::{common::err::SystemError, cositas::CustomResult};
use std::{
fs::File,
io::{BufRead, BufReader},
Expand All @@ -22,10 +22,12 @@ impl SimpleReader {

impl Reader for SimpleReader {
fn read_non_empty_lines(&self, path: &Path) -> CustomResult<Lines> {
Ok(BufReader::new(File::open(path)?)
.lines()
.map_while(core::result::Result::ok)
.filter(|line| !line.trim().is_empty())
.collect())
Ok(
BufReader::new(File::open(path).map_err(|e| SystemError::Read(e.to_string()))?)
.lines()
.map_while(core::result::Result::ok)
.filter(|line| !line.trim().is_empty())
.collect(),
)
}
}
19 changes: 15 additions & 4 deletions src/common/fs/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::Path;

use crate::common::err::SystemError;
use crate::cositas::CustomResult;

#[cfg_attr(test, mockall::automock)]
Expand All @@ -18,19 +19,29 @@ impl SimpleWriter {
}

fn write(mut file: File, content: &str) -> CustomResult<()> {
file.write_all(content.as_bytes())?;
file.flush().map_err(Into::into)
file.write_all(content.as_bytes())
.map_err(|e| SystemError::Write(e.to_string()))?;
Ok(file.flush().map_err(|e| SystemError::Write(e.to_string()))?)
}
}

impl Writer for SimpleWriter {
fn overwrite(&self, path: &Path, content: &str) -> CustomResult<()> {
let file = OpenOptions::new().write(true).truncate(true).create(true).open(path)?;
let file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.map_err(|e| SystemError::Write(e.to_string()))?;
Self::write(file, content)
}

fn append(&self, path: &Path, content: &str) -> CustomResult<()> {
let file = OpenOptions::new().write(true).append(true).open(path)?;
let file = OpenOptions::new()
.write(true)
.append(true)
.open(path)
.map_err(|e| SystemError::Write(e.to_string()))?;
Self::write(file, content)
}
}
18 changes: 13 additions & 5 deletions src/common/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ impl CommandRunner {

impl Runner for CommandRunner {
fn spawn(&self, cmd: &str, arg: &str) -> CustomResult<()> {
Ok(Command::new(cmd).arg(arg).spawn().map(|_| ())?)
Ok(Command::new(cmd)
.arg(arg)
.spawn()
.map(|_| ())
.map_err(|e| SystemError::Runner(cmd.to_string(), e.to_string()))?)
}

fn run(&self, cmd: &str, arg: &str) -> CustomResult<()> {
Command::new(cmd)
if Command::new(cmd)
.arg(arg)
.status()?
.status()
.map_err(|e| SystemError::Runner(cmd.to_string(), e.to_string()))?
.success()
.then_some(())
.ok_or(SystemError::Runner("Command failed".to_string()).into())
{
Ok(())
} else {
Err(SystemError::Runner(cmd.to_string(), "exit code 1".to_string()).into())
}
}
}
8 changes: 1 addition & 7 deletions src/cositas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ impl From<git2::Error> for Box<dyn CustomError> {
}
}

impl From<std::io::Error> for Box<dyn CustomError> {
fn from(e: std::io::Error) -> Box<dyn CustomError> {
Box::new(SystemError::Unknown(e.to_string()))
}
}

impl From<std::env::VarError> for Box<dyn CustomError> {
fn from(e: std::env::VarError) -> Box<dyn CustomError> {
Box::new(SystemError::Unknown(e.to_string()))
Box::new(SystemError::EnvVar(e.to_string()))
}
}

Expand Down

0 comments on commit e8aefed

Please sign in to comment.