From 4f7ecbf9ff8f30b35b8156f08d06a81d46a6e6e7 Mon Sep 17 00:00:00 2001 From: Zykino Date: Thu, 23 May 2019 14:20:58 +0200 Subject: [PATCH] look for the widest number instead of trying to sort the files --- Cargo.lock | 2 ++ src/lib.rs | 64 ++++++++++++++++++------------------------------------ 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d4b589..07807aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "aho-corasick" version = "0.6.9" diff --git a/src/lib.rs b/src/lib.rs index 8c4b5e8..989df7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,6 @@ impl Opt { #[derive(Debug)] struct Page { - position: Option, // FIXME: is it usefull or not ? prefix: String, number: String, suffix: String, @@ -50,30 +49,24 @@ 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, + prefix, + number, + suffix, } } - fn set_position(&mut self, pos: u32) { - 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, pad: usize) -> String { + fn new_filename(&self, pad: usize) -> String { println!( - "NF n: '{}' p: '{}' n: '{}' s: '{}'", - number, self.prefix, self.number, self.suffix + "NF p: '{}' n: '{}' s: '{}'", + self.prefix, self.number, self.suffix ); // format!("{:0pad$}-{}{}{}", number, self.prefix, self.number, self.suffix, pad = pad) // TODO I think I will need this on in the end - // FIXME position is bad format!( "{}{:0pad$}{}", self.prefix, @@ -85,14 +78,15 @@ impl Page { } pub struct ComicBook { + pad: Option, 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 { + pad: None, pages: files .map(|entry| entry.unwrap().path()) .filter(|entry| entry.is_file()) @@ -106,48 +100,32 @@ impl ComicBook { }) .collect(), //~ book: path.iter().map(|name| Page::new(name)).collect(), - is_binded: false, } } pub fn bind(&mut self) { - self.sort(); let pad = self.get_pad_size().unwrap(); 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(), dbg!(pad)); + //if let Some(pos) = page.position { + let original_file = page.original_filename(); + let new_file = page.new_filename(dbg!(pad)); - println!("{} -> {}", original_file, dbg!(new_file)); + println!("{} -> {}", original_file, dbg!(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 u32); + // fs::rename(original_file, new_file).expect("RENAME FAILED"); + //} } } fn get_pad_size(&self) -> Option { - if !self.is_binded { - return None; - } - - Some(self.pages.last().unwrap().number.len()) + Some( + self.pages + .iter() + .max_by_key(|x| x.number.len())? + .number + .len(), + ) } }