Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 12, 2020
1 parent 6bfea63 commit 87c8a2e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion git-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ publish = false
edition = "2018"

[dependencies]
failure = "0.1.1"
quick-error = "1.2.3"
18 changes: 9 additions & 9 deletions git-core/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

quick_error! {
#[derive(Debug)]
pub enum InitError {
pub enum Error {
IoOpen(err: std::io::Error, path: PathBuf) {
display("Could not open file at '{}'", path.display())
cause(err)
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> PathCursor<'a> {
}

impl<'a> NewDir<'a> {
fn at(self, component: &str) -> Result<Self, InitError> {
fn at(self, component: &str) -> Result<Self, Error> {
self.0.push(component);
create_dir(&self.0)?;
Ok(self)
Expand All @@ -93,25 +93,25 @@ impl<'a> Drop for PathCursor<'a> {
}
}

fn write_file(data: &[u8], path: &Path) -> Result<(), InitError> {
fn write_file(data: &[u8], path: &Path) -> Result<(), Error> {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.append(false)
.open(path)
.map_err(|e| InitError::IoOpen(e, path.to_owned()))?;
.map_err(|e| Error::IoOpen(e, path.to_owned()))?;
file.write_all(data)
.map_err(|e| InitError::IoWrite(e, path.to_owned()))
.map_err(|e| Error::IoWrite(e, path.to_owned()))
}

fn create_dir(p: &Path) -> Result<(), InitError> {
fs::create_dir(p).map_err(|e| InitError::CreateDirectory(e, p.to_owned()))
fn create_dir(p: &Path) -> Result<(), Error> {
fs::create_dir(p).map_err(|e| Error::CreateDirectory(e, p.to_owned()))
}

pub fn init() -> Result<(), InitError> {
pub fn repository() -> Result<(), Error> {
let mut cursor = PathBuf::from(GIT_DIR_NAME);
if cursor.is_dir() {
return Err(InitError::DirectoryExists(cursor));
return Err(Error::DirectoryExists(cursor));
}
create_dir(&cursor)?;

Expand Down
4 changes: 1 addition & 3 deletions git-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[macro_use]
extern crate quick_error;

mod init;

pub use init::{init, InitError};
pub mod init;
1 change: 1 addition & 0 deletions git-odb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"

[dependencies]
failure = "0.1.1"
quick-error = "1.2.3"
walkdir = "2.1.4"
hex = "0.3.2"
miniz_oxide = "0.3.6"
Expand Down
2 changes: 2 additions & 0 deletions git-odb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[macro_use]
extern crate failure;
// #[macro_use]
// extern crate quick_error;
extern crate byteorder;
extern crate filebuffer;
extern crate hex;
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ fn run() -> Result<(), Error> {
let app = app::new();
let matches = app.get_matches();
match matches.subcommand() {
("init", Some(_args)) => git::init().with_context(|_| "Repository initialization failed"),
("init", Some(_args)) => {
git::init::repository().with_context(|_| "Repository initialization failed")
}
_ => unreachable!(),
}
.map_err(Into::into)
Expand Down

0 comments on commit 87c8a2e

Please sign in to comment.