-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from TakaakiFuruse/i24-eliminte-unwrap
I24 eliminte unwrap
- Loading branch information
Showing
11 changed files
with
165 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "jump-kun" | ||
version = "0.3.1" | ||
version = "0.3.2" | ||
authors = ["TakaakiFuruse <[email protected]>"] | ||
edition = "2018" | ||
description = "A simple directory jumper made by rust." | ||
|
@@ -15,9 +15,9 @@ path = "src/main.rs" | |
jump-kun-macros={version = "0.1.0", path = "jump-kun-macros"} | ||
walkdir= "2.2.9" | ||
skim = "0.6.9" | ||
serde_json= "1.0.41" | ||
serde = { version = "1.0.101", features = ["derive"] } | ||
serde_json= "1.0.44" | ||
serde = { version = "1.0.103", features = ["derive"] } | ||
dirs = "2.0.2" | ||
derive_builder = "0.8.0" | ||
sled = "0.28.0" | ||
sled = "0.30.0" | ||
ignore = "0.4.10" | ||
anyhow = "1.0.25" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,54 @@ | ||
use super::enums::DirType; | ||
use super::structs::{Dir, DirBuilder, DirVec}; | ||
use super::structs::{Dir, DirVec}; | ||
use super::walker::{start_walking_around, start_walking_down, start_walking_up}; | ||
use anyhow::Result; | ||
use ignore::gitignore::Gitignore; | ||
|
||
use std::env; | ||
use std::path::{Path, PathBuf}; | ||
|
||
pub fn find_dirs(jump_kun_ignore: Gitignore) -> DirVec { | ||
pub fn find_dirs(jump_kun_ignore: Gitignore) -> Result<DirVec> { | ||
match env::var("DOWN_FROM") { | ||
Ok(dir) => start_walking_down(from_specific_directory(dir), &jump_kun_ignore), | ||
Ok(dir) => start_walking_down(from_specific_directory(dir)?, &jump_kun_ignore), | ||
Err(_) => match env::var("UP_FROM") { | ||
Ok(dir) => start_walking_up(from_parent_directory_or_root(dir), &jump_kun_ignore), | ||
Err(_) => start_walking_around(from_current_directory(), &jump_kun_ignore), | ||
Ok(dir) => start_walking_up(from_parent_directory_or_root(dir)?, &jump_kun_ignore), | ||
Err(_) => start_walking_around(from_current_directory()?, &jump_kun_ignore), | ||
}, | ||
} | ||
} | ||
|
||
fn from_specific_directory(dir: String) -> Dir { | ||
DirBuilder::default() | ||
fn from_specific_directory(dir: String) -> Result<Dir> { | ||
Ok(Dir::default() | ||
.path(PathBuf::from(dir)) | ||
.dirtype(DirType::CurrentDir) | ||
.build() | ||
.unwrap() | ||
.dirtype(DirType::CurrentDir)) | ||
} | ||
|
||
fn from_current_directory() -> Dir { | ||
DirBuilder::default() | ||
.path(env::current_dir().unwrap()) | ||
.dirtype(DirType::CurrentDir) | ||
.build() | ||
.unwrap() | ||
fn from_current_directory() -> Result<Dir> { | ||
Ok(Dir::default() | ||
.path(env::current_dir()?) | ||
.dirtype(DirType::CurrentDir)) | ||
} | ||
|
||
fn from_parent_directory_or_root(dir: String) -> Dir { | ||
DirBuilder::default() | ||
fn from_parent_directory_or_root(dir: String) -> Result<Dir> { | ||
Ok(Dir::default() | ||
.path( | ||
PathBuf::from(dir) | ||
.parent() | ||
.unwrap_or_else(|| Path::new("/")) | ||
.to_path_buf(), | ||
) | ||
.dirtype(DirType::CurrentDir) | ||
.build() | ||
.unwrap() | ||
.dirtype(DirType::CurrentDir)) | ||
} | ||
|
||
pub fn current_dir() -> Dir { | ||
pub fn current_dir() -> Result<Dir> { | ||
let pathbuf = match env::var("DOWN_FROM") { | ||
Ok(dir) => PathBuf::from(dir), | ||
Err(_) => match env::var("UP_FROM") { | ||
Ok(dir) => PathBuf::from(dir).parent().unwrap().to_path_buf(), | ||
Err(_) => env::current_dir().unwrap(), | ||
Ok(dir) => PathBuf::from(dir) | ||
.parent() | ||
.unwrap_or_else(|| Path::new("/")) | ||
.to_path_buf(), | ||
Err(_) => env::current_dir()?, | ||
}, | ||
}; | ||
DirBuilder::default() | ||
.path(pathbuf) | ||
.dirtype(DirType::CurrentDir) | ||
.build() | ||
.unwrap() | ||
Ok(Dir::default().path(pathbuf).dirtype(DirType::CurrentDir)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ pub enum DirType { | |
ChildDir, | ||
VisitedDir, | ||
NotSure, | ||
Invalid, | ||
} | ||
|
||
#[cfg(test)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
use super::structs::{Dir, DirVec}; | ||
use crate::enums::DirType; | ||
use anyhow::Result; | ||
use serde_json; | ||
use sled::Db; | ||
use std::path::PathBuf; | ||
use std::str; | ||
|
||
pub fn read(path: &str) -> DirVec { | ||
let tree = Db::open(path).unwrap(); | ||
pub fn read(path: &PathBuf) -> Result<DirVec> { | ||
let tree = Db::open(path)?; | ||
let dirvec: DirVec = tree | ||
.iter() | ||
.values() | ||
.filter_map(|elm| { | ||
let s: String = str::from_utf8(&elm.unwrap()).unwrap().to_owned(); | ||
let dir: Dir = serde_json::from_str(&s).unwrap(); | ||
if dir.path.exists() { | ||
Some(dir) | ||
.filter_map(|k| match k { | ||
Ok((k, v)) => { | ||
if PathBuf::from(str::from_utf8(&k).unwrap_or("str from error")).exists() { | ||
let d: Dir = | ||
serde_json::from_str(&str::from_utf8(&v).unwrap_or("str from error")) | ||
.unwrap_or(Dir::invalid()); | ||
|
||
Some(d) | ||
} else { | ||
None | ||
} | ||
} | ||
Err(_) => None, | ||
}) | ||
.filter(|d| { | ||
if let DirType::Invalid = d.dirtype { | ||
false | ||
} else { | ||
None | ||
true | ||
} | ||
}) | ||
.collect(); | ||
dirvec | ||
Ok(dirvec) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.