History

git-core: get rid of failure crate in favor of quick-error

Sebastian Thiel committed 6 years ago

Commit 91c8fc11 parent f037c4d3 files · +26 −18Browse files

Unified · Split

3 files
  1. git-core/src/init.rs +24 −13
  2. git-core/src/lib.rs +1 −4
  3. git-odb/src/object/parsed.rs +1 −1
git-core/src/init.rs+24−13
@@ -1,6 +1,5 @@
1−use failure::{Error, ResultExt};
21 use std::{
3− fs::{create_dir, OpenOptions},
2+ fs::{self, OpenOptions},
43 io::Write,
54 path::Path,
65 path::PathBuf,
@@ -10,7 +9,18 @@ quick_error! { · 3 unmodified lines
109 #[derive(Debug)]
1110 pub enum InitError {
1211 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())
1424 cause(err)
1525 }
1626 }
@@ -59,7 +69,7 @@ impl<'a> PathCursor<'a> { · 42 unmodified lines
5969 }
6070
6171 impl<'a> NewDir<'a> {
62− fn at(self, component: &str) -> Result<Self, Error> {
72+ fn at(self, component: &str) -> Result<Self, InitError> {
6373 self.0.push(component);
6474 create_dir(&self.0)?;
6575 Ok(self)
@@ -81,24 +91,25 @@ impl<'a> Drop for PathCursor<'a> { · 15 unmodified lines
8191 }
8292 }
8393
84−fn write_file(data: &[u8], path: &Path) -> Result<(), Error> {
94+fn write_file(data: &[u8], path: &Path) -> Result<(), InitError> {
8595 let mut file = OpenOptions::new()
8696 .write(true)
8797 .create(true)
8898 .append(false)
89− .open(path)?;
99+ .open(path)
100+ .map_err(|e| InitError::IoOpen(e, path.to_owned()))?;
90101 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()))
93107 }
94108
95−pub fn init() -> Result<(), Error> {
109+pub fn init() -> Result<(), InitError> {
96110 let mut cursor = PathBuf::from(GIT_DIR_NAME);
97111 if cursor.is_dir() {
98− bail!(
99− "Refusing to initialize the existing '{}' directory",
100− cursor.display()
101− )
112+ return Err(InitError::DirectoryExists(cursor));
102113 }
103114 create_dir(&cursor)?;
104115
git-core/src/lib.rs+1−4
@@ -1,9 +1,6 @@
1−#[macro_use]
2−extern crate failure;
3−
41 #[macro_use]
52 extern crate quick_error;
63
74 mod init;
85
9−pub use init::init;
6+pub use init::{init, InitError};
git-odb/src/object/parsed.rs+1−1
@@ -137,7 +137,7 @@ fn parse_message<'data>( · 136 unmodified lines
137137 }
138138 let mut msg_end = d.len();
139139 let mut pgp_signature = None;
140− if let Some(pgp_begin_line) = lines.find(|l| l.starts_with(PGP_SIGNATURE_BEGIN)) {
140+ if let Some(_pgp_begin_line) = lines.find(|l| l.starts_with(PGP_SIGNATURE_BEGIN)) {
141141 match lines.find(|l| l.starts_with(PGP_SIGNATURE_END)) {
142142 None => bail!("Didn't find end of signature marker"),
143143 Some(_) => {