From 1651f24ce7ac30b54f69920f6bdce15a1bbe2d97 Mon Sep 17 00:00:00 2001 From: Zykino Date: Thu, 19 Sep 2019 14:02:30 +0200 Subject: [PATCH] Make the lib work on strings and not on the filesystem --- README.md | 21 +++++++++++---------- src/lib.rs | 12 ++++++------ src/main.rs | 27 ++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f0411b9..18a5af3 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,17 @@ $ cbb comic/ You may want to customize the number of leading 0, it is useful when you plan on adding more pages later. ``` $ cbb comic/ --pad=3 --dry-run -7.txt -> 07.txt -10.txt -> 10.txt -5.txt -> 05.txt -3.txt -> 03.txt -4.txt -> 04.txt -8.txt -> 08.txt -2.txt -> 02.txt -6.txt -> 06.txt -1.txt -> 01.txt -9.txt -> 09.txt +1.png -> 001.png +8.png -> 008.png +3.png -> 003.png +4.png -> 004.png +6.png -> 006.png +2.png -> 002.png +9.png -> 009.png +5.png -> 005.png +11.png -> 011.png +7.png -> 007.png +10.png -> 010.png ``` # TODO diff --git a/src/lib.rs b/src/lib.rs index b4e4ad5..d6bda34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ use std::ffi::OsString; -use std::fs::ReadDir; use std::path::PathBuf; use regex::Regex; @@ -91,15 +90,16 @@ pub struct ComicBook { } impl ComicBook { - pub fn new(files: ReadDir, pad: Option) -> ComicBook { - let regex = Regex::new(r"\./(?P\D*)(?P\d*)(?P.*)").unwrap(); + pub fn new(files: Vec, pad: Option) -> ComicBook { + let regex = Regex::new(r"(?P\D*)(?P\d*)(?P.*)").unwrap(); ComicBook { pad, pages: files - .map(|entry| entry.unwrap().path()) - .filter(|entry| entry.is_file()) + .iter() .map(|entry| { - let caps = regex.captures(entry.to_str().unwrap()).unwrap(); + dbg!(entry.as_str()); + let caps = dbg!(regex.captures(entry.as_str())).unwrap(); + Page::new( String::from(caps.name("prefix").map_or("", |c| c.as_str())), String::from(&caps["number"]), // FIXME => ignore the file? diff --git a/src/main.rs b/src/main.rs index 85dbb28..7cd8453 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use cbb::Opt; use std::env; use std::fs; +use std::path::Path; fn main() { let opt = Opt::new(env::args()); @@ -19,8 +20,28 @@ fn main() { // unimplemented!(); // } - env::set_current_dir(opt.comic_folder).unwrap(); - let mut book = ComicBook::new(fs::read_dir(".").unwrap(), opt.pad); + // TODO: improve the error message with a the name of the directory (use format) + env::set_current_dir(opt.comic_folder) + .expect("Impossible to set current directory to targeted"); + + let paths = fs::read_dir(&Path::new(&env::current_dir().unwrap())).unwrap(); + let files = paths + .filter_map(|entry| { + let entry = entry.unwrap(); + + // We want the name of the files in the directory. If the entry is not a file we do not need to fetch its filename. + if entry.file_type().unwrap().is_file() { + let file_name = entry.file_name(); + let file_name_as_str = file_name.to_str().unwrap(); + let file_name_as_string = String::from(file_name_as_str); + Some(file_name_as_string) + } else { + None + } + }) + .collect::>(); + + let mut book = ComicBook::new(files, opt.pad); let bind_func = if opt.dry_run { dry_run } else { rename }; book.bind(bind_func); @@ -34,5 +55,5 @@ fn dry_run(original: String, new: String) { } fn rename(original: String, new: String) { - fs::rename(original, new).expect("RENAME FAILED"); + fs::rename(original, new).expect("Rename failed"); }