Skip to content

Commit

Permalink
Removed most of the calls to unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthvk committed Jan 20, 2024
1 parent 851cbaf commit 2eb9909
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Log file to view the operations performed
3. [x] Add dry run flag
4. [x] Add confirmation (yes no for each rename)
5. [x] Add basic tests to check the working of all operations
6. [ ] Remove calls to unwrap() and handle errors correctly
6. [x] Remove calls to unwrap() and handle errors correctly
7. [x] Flag to replace all
8. [ ] Give warning if rename overwrites an already existing file
9. [ ] Add recursive flag
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod cli;
mod rename;
mod filters;
mod operations;
mod rename;
use clap::Parser;
fn main() {
let cli = cli::Cli::parse();
// println!("{:#?}", cli);
rename::rename(cli);
let err = rename::rename(cli);
if let Err(e) = err {
eprintln!("{:#?}", e);
}
}
5 changes: 4 additions & 1 deletion src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn regex_replace<'a>(
let matches: Vec<regex::Match> = re.find_iter(&filename).collect();
matches
} else {
vec![re.find(&filename).unwrap()]
match re.find(&filename) {
Some(v) => vec![v],
None => vec![],
}
};

let replaced = if replace_all {
Expand Down
32 changes: 17 additions & 15 deletions src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ use crate::cli::Cli;
use crate::filters::*;
use crate::operations::*;
use regex::Regex;
use std::error::Error;
use std::fs;
use std::path::PathBuf;

// TODO: Handle errors instead of unwrapping
// Add option to ignore or halt on error
// Also if a file exists, give an option to overwrite or not
// By default do not overwrite

fn get_dir_contents(target_directory: &PathBuf) -> Box<dyn Iterator<Item = String>> {
let entries = fs::read_dir(target_directory)
.unwrap()
fn get_dir_contents(target_directory: &PathBuf) -> Result<Box<dyn Iterator<Item = String>>, Box<dyn Error>> {
let entries = fs::read_dir(target_directory)?
.map(|entry| entry.unwrap().file_name().into_string().unwrap());
Box::new(entries)
Ok(Box::new(entries))
}

fn rename_entry(from: &str, to: &str) {
fs::rename(from, to).unwrap();
fn rename_entry(from: &str, to: &str) -> Result<(), Box<dyn Error>>{
fs::rename(from, to)?;
Ok(())
}

pub fn rename(config: Cli) {
pub fn rename(config: Cli) -> Result<(), Box<dyn Error>> {
let target_directory = match config.dir.as_deref() {
Some(p) => p.to_path_buf(),
None => PathBuf::from("."),
};
let target_directory = fs::canonicalize(&target_directory).unwrap();

std::env::set_current_dir(&target_directory).unwrap();
let files = get_dir_contents(&target_directory);
let target_directory = fs::canonicalize(&target_directory)?;

std::env::set_current_dir(&target_directory)?;
let files = get_dir_contents(&target_directory)?;
let mut filters: Vec<Regex> = Vec::new();
let re = Regex::new(&config.search_expr).unwrap();
let re = Regex::new(&config.search_expr)?;

if config.starts_with_list.len() > 0 {
let startswith: Vec<&str> = config.starts_with_list.iter().map(AsRef::as_ref).collect();
filters.push(generate_startswith_filter(&startswith).unwrap())
filters.push(generate_startswith_filter(&startswith)?)
}

'outer: for file in files {
Expand All @@ -50,12 +51,13 @@ pub fn rename(config: Cli) {
if config.execute {
if !config.noconfirm {
if ask_confirmation() {
rename_entry(&file, &replacement.replaced);
rename_entry(&file, &replacement.replaced)?;
}
} else {
rename_entry(&file, &replacement.replaced);
rename_entry(&file, &replacement.replaced)?;
}
}
}
}
Ok(())
}

0 comments on commit 2eb9909

Please sign in to comment.