| @@ -1,6 +1,5 @@ |
| 1 | | −use failure::{Error, ResultExt}; |
| 2 | 1 | use std::{ |
| 3 | | − fs::{create_dir, OpenOptions}, |
| 2 | + fs::{self, OpenOptions}, |
| 4 | 3 | io::Write, |
| 5 | 4 | path::Path, |
| 6 | 5 | path::PathBuf, |
| @@ -10,7 +9,18 @@ quick_error! { · 3 unmodified lines |
| 10 | 9 | #[derive(Debug)] |
| 11 | 10 | pub enum InitError { |
| 12 | 11 | IoOpen(err: std::io::Error, path: PathBuf) { |
| 13 | | − display("Could not open file at {:#?}", path.display()) |
| 12 | + display("Could not open file at '{}'", path.display()) |
| 13 | + cause(err) |
| 14 | + } |
| 15 | + IoWrite(err: std::io::Error, path: PathBuf) { |
| 16 | + display("Could not write file at '{}'", path.display()) |
| 17 | + cause(err) |
| 18 | + } |
| 19 | + DirectoryExists(path: PathBuf) { |
| 20 | + display("Refusing to initialize the existing '{}' directory", path.display()) |
| 21 | + } |
| 22 | + CreateDirectory(err: std::io::Error, path: PathBuf) { |
| 23 | + display("Cold not create directory at '{}'", path.display()) |
| 14 | 24 | cause(err) |
| 15 | 25 | } |
| 16 | 26 | } |
| @@ -59,7 +69,7 @@ impl<'a> PathCursor<'a> { · 42 unmodified lines |
| 59 | 69 | } |
| 60 | 70 | |
| 61 | 71 | impl<'a> NewDir<'a> { |
| 62 | | − fn at(self, component: &str) -> Result<Self, Error> { |
| 72 | + fn at(self, component: &str) -> Result<Self, InitError> { |
| 63 | 73 | self.0.push(component); |
| 64 | 74 | create_dir(&self.0)?; |
| 65 | 75 | Ok(self) |
| @@ -81,24 +91,25 @@ impl<'a> Drop for PathCursor<'a> { · 15 unmodified lines |
| 81 | 91 | } |
| 82 | 92 | } |
| 83 | 93 | |
| 84 | | −fn write_file(data: &[u8], path: &Path) -> Result<(), Error> { |
| 94 | +fn write_file(data: &[u8], path: &Path) -> Result<(), InitError> { |
| 85 | 95 | let mut file = OpenOptions::new() |
| 86 | 96 | .write(true) |
| 87 | 97 | .create(true) |
| 88 | 98 | .append(false) |
| 89 | | − .open(path)?; |
| 99 | + .open(path) |
| 100 | + .map_err(|e| InitError::IoOpen(e, path.to_owned()))?; |
| 90 | 101 | file.write_all(data) |
| 91 | | − .with_context(|_| format!("Could not initialize file at '{}'", path.display())) |
| 92 | | − .map_err(Into::into) |
| 102 | + .map_err(|e| InitError::IoWrite(e, path.to_owned())) |
| 103 | +} |
| 104 | + |
| 105 | +fn create_dir(p: &Path) -> Result<(), InitError> { |
| 106 | + fs::create_dir(p).map_err(|e| InitError::CreateDirectory(e, p.to_owned())) |
| 93 | 107 | } |
| 94 | 108 | |
| 95 | | −pub fn init() -> Result<(), Error> { |
| 109 | +pub fn init() -> Result<(), InitError> { |
| 96 | 110 | let mut cursor = PathBuf::from(GIT_DIR_NAME); |
| 97 | 111 | if cursor.is_dir() { |
| 98 | | − bail!( |
| 99 | | − "Refusing to initialize the existing '{}' directory", |
| 100 | | − cursor.display() |
| 101 | | − ) |
| 112 | + return Err(InitError::DirectoryExists(cursor)); |
| 102 | 113 | } |
| 103 | 114 | create_dir(&cursor)?; |
| 104 | 115 | |