diff --git a/src/authors/csv/reader.rs b/src/authors/csv/reader.rs index 284d58d..2a947b1 100644 --- a/src/authors/csv/reader.rs +++ b/src/authors/csv/reader.rs @@ -1,11 +1,9 @@ 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::common::{conf, env}; use crate::Result; -use std::env; use std::path::{Path, PathBuf}; pub enum LoadMode<'a> { @@ -35,7 +33,7 @@ impl CSVReader { fn from_cwd_fallback_home(file_reader: &dyn Reader) -> Result { let file_path = &conf::authors_file(); let dir_path = &conf::authors_dir(); - let cwd = env::current_dir().map_err(|_| SystemError::EnvVar("CWD".to_string()))?; + let cwd = env::cwd()?; file_reader .read_non_empty_lines(&cwd.join(file_path)) @@ -48,12 +46,12 @@ impl CSVReader { } fn xdg_or_home_fallback(file_reader: &dyn Reader, authors_dir: &str, file_path: &str) -> Result { - let home = PathBuf::from(env::var("XDG_CONFIG_HOME")?); + let home = PathBuf::from(env::xdg_home()?); file_reader.read_non_empty_lines(&home.join(authors_dir).join(file_path)) } fn home_fallback(file_reader: &dyn Reader, authors_dir: &str, file_path: &str) -> Result { - let home = env::var("HOME")?; + let home = env::home()?; file_reader .read_non_empty_lines(&PathBuf::from(format!("{home}/.config/{authors_dir}/{file_path}"))) .or_else(|_| file_reader.read_non_empty_lines(&PathBuf::from(format!("{home}/.{authors_dir}/{file_path}")))) diff --git a/src/common/env.rs b/src/common/env.rs new file mode 100644 index 0000000..b213145 --- /dev/null +++ b/src/common/env.rs @@ -0,0 +1,19 @@ +use super::err::SystemError; +use crate::Result; +use std::path::PathBuf; + +pub fn cwd() -> Result { + Ok(std::env::current_dir().map_err(|_| SystemError::EnvVar("CWD".to_string()))?) +} + +pub fn home() -> Result { + Ok(std::env::var("HOME").map_err(|_| SystemError::EnvVar("HOME".to_string()))?) +} + +pub fn xdg_home() -> Result { + Ok(std::env::var("XDG_CONFIG_HOME").map_err(|_| SystemError::EnvVar("XDG_CONFIG_HOME".to_string()))?) +} + +pub fn editor() -> Result { + Ok(std::env::var("EDITOR").map_err(|_| SystemError::EnvVar("EDITOR".to_string()))?) +} diff --git a/src/common/mod.rs b/src/common/mod.rs index 5db6d82..abf6957 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,4 +1,5 @@ pub mod conf; +pub mod env; pub mod err; pub mod runner; diff --git a/src/git/core/editor/file_editor.rs b/src/git/core/editor/file_editor.rs index 833751e..55a8bdb 100644 --- a/src/git/core/editor/file_editor.rs +++ b/src/git/core/editor/file_editor.rs @@ -1,9 +1,8 @@ use crate::{ - common::runner::Runner, + common::{env, runner::Runner}, git::{core::conf_provider::ConfProvider, err::GitError}, Result, }; -use std::env; #[cfg_attr(test, mockall::automock)] pub trait Editor { @@ -30,7 +29,7 @@ impl FileEditor { } fn env_fallback(&self, path: &str) -> Result<()> { - match env::var("EDITOR") { + match env::editor() { Err(_) => self.vim_fallback(path), Ok(editor) => Ok(self.runner.spawn(&editor, path).map_err(|_| GitError::Editor)?), } diff --git a/src/git/di.rs b/src/git/di.rs index b84f8db..f3bd55a 100644 --- a/src/git/di.rs +++ b/src/git/di.rs @@ -2,8 +2,8 @@ use super::core::conf_provider::GitConfProvider; use super::core::editor::file_editor::FileEditor; use super::core::hook::Hook; use super::core::libgit::wrapper::LibGitWrapper; -use super::err::GitError; use super::service::GitService; +use crate::common::env; use crate::common::fs::file_reader::SimpleReader; use crate::common::fs::file_writer::SimpleWriter; use crate::common::runner::CommandRunner; @@ -14,7 +14,7 @@ type GitHook = Hook; pub type Service = GitService, GitHook, Editor, SimpleWriter>; pub fn init() -> Result { - let cwd = std::env::current_dir().map_err(|_| GitError::InvalidRepo)?; + let cwd = env::cwd()?; let wrapper = LibGitWrapper::from(&cwd, SimpleReader::new())?; Ok(GitService::new(