Make the lib work on strings and not on the filesystem
This commit is contained in:
parent
803b1cf46c
commit
1651f24ce7
21
README.md
21
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
|
||||
|
12
src/lib.rs
12
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<usize>) -> ComicBook {
|
||||
let regex = Regex::new(r"\./(?P<prefix>\D*)(?P<number>\d*)(?P<suffix>.*)").unwrap();
|
||||
pub fn new(files: Vec<String>, pad: Option<usize>) -> ComicBook {
|
||||
let regex = Regex::new(r"(?P<prefix>\D*)(?P<number>\d*)(?P<suffix>.*)").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?
|
||||
|
27
src/main.rs
27
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::<Vec<String>>();
|
||||
|
||||
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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user