Skip to content

Commit

Permalink
Refactor errors and remove unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
AP2008 committed Feb 3, 2022
1 parent 4177d72 commit eaee855
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions git-worktree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use quick_error::quick_error;
use std::convert::TryFrom;
use std::fs;
use std::fs::create_dir_all;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time::Duration;

#[cfg(unix)]
Expand All @@ -16,23 +16,23 @@ use std::os::unix::fs::PermissionsExt;
quick_error! {
#[derive(Debug)]
pub enum Error {
Utf8Error(err: git_object::bstr::Utf8Error) {
Utf8(err: git_object::bstr::Utf8Error) {
from()
display("Could not convert path to UTF8: {}", err)
}
TimeError(err: std::time::SystemTimeError) {
Time(err: std::time::SystemTimeError) {
from()
display("Could not read file time in proper format: {}", err)
}
ToU32Error(err: std::num::TryFromIntError) {
U32Conversion(err: std::num::TryFromIntError) {
from()
display("Could not convert seconds to u32: {}", err)
}
IoError(err: std::io::Error) {
Io(err: std::io::Error) {
from()
display("IO error while writing blob or reading file metadata or changing filetype: {}", err)
}
FindOidError(oid: git_hash::ObjectId, path: std::path::PathBuf) {
NotFound(oid: git_hash::ObjectId, path: PathBuf) {
display("unable to read sha1 file of {} ({})", path.display(), oid.to_hex())
}
}
Expand All @@ -56,7 +56,7 @@ where
create_dir_all(dest.parent().expect("entry paths are never empty"))?;
match entry.mode {
git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => {
let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::FindOidError(entry.id, path.to_path_buf()))?;
let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::NotFound(entry.id, path.to_path_buf()))?;
std::fs::write(&dest, obj.data)?;
if entry.mode == git_index::entry::Mode::FILE_EXECUTABLE {
#[cfg(unix)]
Expand All @@ -72,7 +72,7 @@ where
entry_time.push((ctime?, mtime?, i));
}
git_index::entry::Mode::SYMLINK => {
let obj = find(&entry.id, &mut buf).unwrap();
let obj = find(&entry.id, &mut buf).ok_or_else(|| Error::NotFound(entry.id, path.to_path_buf()))?;
let linked_to = obj.data.to_path()?;
if opts.symlinks {
#[cfg(unix)]
Expand Down

0 comments on commit eaee855

Please sign in to comment.