comic-book-binder/src/main.rs

60 lines
1.9 KiB
Rust

//! This software aim at helping you generate comic book archive. You can use it to generate comic
//! book archive, or just making sure your list of files will be ordered the same way on any
//! operating system.
use cbb;
use cbb::ComicBook;
use cbb::Opt;
use std::env;
use std::fs;
use std::path::Path;
fn main() {
let opt = Opt::new(env::args());
dbg!(&opt);
// if opt.recursive
// /*|| opt.prefix*/
// {
// unimplemented!();
// }
// 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);
}
fn dry_run(original: String, new: String) {
// TODO: Is it possible to be certain that the alignment is kept (like in a table)?
// I think there is at least a crate doing that. But I do not want to add bloat only for this
// => To benchmark (binary size, clean compile)
println!("{}\t-> {}", original, new);
}
fn rename(original: String, new: String) {
fs::rename(original, new).expect("Rename failed");
}