diff --git a/.gitignore b/.gitignore index 8a6a01a..71d315e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,5 @@ # will have compiled files and executables /target/ -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock - # These are backup files generated by rustfmt **/*.rs.bk - diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b327c97 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,88 @@ +[[package]] +name = "aho-corasick" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cfg-if" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "comic-book-binder" +version = "0.1.0" +dependencies = [ + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.48" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ucd-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "utf8-ranges" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" +"checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" +"checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" +"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" +"checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" +"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..929814b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "comic-book-binder" +version = "0.1.0" +authors = ["Zykino "] +edition = "2018" + +[lib] +name = "cbb" + +[[bin]] +name = "cbb" +path = "src/main.rs" + +[dependencies] +regex = "1" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d0424eb --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,138 @@ +extern crate regex; + +use regex::Regex; + +use std::fs::{self, ReadDir}; +use std::path::PathBuf; + +#[derive(Debug)] +pub struct Page { + position: Option, + prefix: String, + number: String, + suffix: String, +} + +impl Page { + fn new(prefix: String, number: String, suffix: String) -> Page { + println!("N p:{} n: {} s: {}", prefix, number, suffix); + Page { + position: None, + prefix: prefix, + number: number, + suffix: suffix, + } + } + + fn set_position(&mut self, pos: u64) { + self.position = Some(pos); + } + + fn original_filename(&self) -> String { + println!("OF p:{} n: {} s: {}", self.prefix, self.number, self.suffix); + format!("{}{}{}", self.prefix, self.number, self.suffix) + } + + fn new_filename(&self, number: &str) -> String { + println!("NF n: {} p:{} n: {} s: {}", number, self.prefix, self.number, self.suffix); + format!("{}-{}{}{}", number, self.prefix, self.number, self.suffix) + //~ TODO I think I will need this on in the end + //~ format!("{}{}{}", self.prefix, number, self.suffix) + } +} + +pub struct ComicBook { + pages: Vec, + is_binded: bool, +} + +impl ComicBook { + pub fn new(files: ReadDir) -> ComicBook { + let regex = Regex::new(r"\./(?P\D*)(?P\d*)(?P.*)").unwrap(); + ComicBook { + pages: files + .map(|entry| entry.unwrap().path()) + .filter(|entry| entry.is_file()) + .map(|entry| { + let caps = regex.captures(entry.to_str().unwrap()).unwrap(); + Page::new( + String::from(caps.name("prefix").map_or("", |c| c.as_str())), + String::from(&caps["number"]), // FIXME => ignore the file? + String::from(caps.name("suffix").map_or("", |c| c.as_str())), + ) + }) + .collect(), + //~ book: path.iter().map(|name| Page::new(name)).collect(), + is_binded: false, + } + } + + pub fn bind(&mut self) { + self.sort(); + + for page in self.pages.iter() { + if let Some(pos) = page.position { + let original_file = page.original_filename(); + let new_file = page.new_filename(&pos.to_string()); + + println!("{} X {}", original_file, new_file); + + fs::rename( + original_file, + new_file, + ) + .expect("RENAME FAILED"); + } + } + } + + fn sort(&mut self) { + //~ self.book.sort(); + self.pages.sort_by(|a, b| { + a.number + .parse::() + .unwrap() + .cmp(&b.number.parse::().unwrap()) + }); + self.is_binded = true; + + // FIXME: Do not use enumerate but the parsed value: there may be gaps in the collection + for (i, page) in self.pages.iter_mut().enumerate() { + page.set_position(i as u64); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + //~ use std::sys_common::io::test::{TempDir, tmpdir}; + + #[test] + fn comic_book_creation() { + //~ let tmpdir = tmpdir(); + //~ println!("{}", tmpdir); + //~ let comic = ComicBook::new(&[ + //~ Path::new("01.png").to_path_buf(), + //~ Path::new("02.png").to_path_buf(), + //~ ]); + + //~ println!("{:?}", comic.book); + + assert!(false); + //~ assert_eq!(comic.book, [Page {filename: "01.png", position: None}, Page {filename: "02.png", position: None}]); + } + + //~ #[test] + //~ fn detection_alpha() { + //~ let l = Unsorted(vec!["1", "2", "10"]); + //~ assert_eq!(l.detect_sort_type(), false); + //~ } + + //~ #[test] + //~ fn detection_numerical() { + //~ let l = Unsorted(vec!["01", "02", "10"]); + //~ assert_eq!(l.detect_sort_type(), true); + //~ } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4c34398 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +use cbb; +use cbb::ComicBook; + +use std::fs; +use std::env; + +fn main() { + // TODO: get the dir from the args + let dir = "examples-test/int"; + + env::set_current_dir(dir).unwrap(); + let mut book = ComicBook::new(fs::read_dir(".").unwrap()); + + book.bind(); + + //~ println!("{:?}", book); +}