From 3d9c39c87255f75ebff8f347988c00b7016d67c4 Mon Sep 17 00:00:00 2001 From: Vladimir Rubin Date: Mon, 30 Dec 2024 02:05:33 +0200 Subject: [PATCH] refactor: get rid of toml i decided that the binary size is too big and we shouldn't use any libraries so i wrote my own parser that even seems to work --- Cargo.lock | 139 ---------------------------------------------------- Cargo.toml | 3 +- src/lib.rs | 78 ++++++++++++++++------------- src/main.rs | 9 ++-- 4 files changed, 49 insertions(+), 180 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc7e58b..7848529 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,145 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "filesorters" version = "0.1.0" -dependencies = [ - "toml", -] - -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - -[[package]] -name = "indexmap" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "proc-macro2" -version = "1.0.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "serde" -version = "1.0.217" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.217" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_spanned" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" -dependencies = [ - "serde", -] - -[[package]] -name = "syn" -version = "2.0.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "toml" -version = "0.8.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.22.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "winnow", -] - -[[package]] -name = "unicode-ident" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" - -[[package]] -name = "winnow" -version = "0.6.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" -dependencies = [ - "memchr", -] diff --git a/Cargo.toml b/Cargo.toml index 1cbfd5b..0a4029b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -toml = {version="0.8.19", default-features = false, features = ["parse", "preserve_order"]} [profile.release.package."*"] opt-level = "z" @@ -13,5 +12,5 @@ opt-level = "z" strip = true codegen-units = 1 opt-level = "s" -lto = "thin" +lto = true panic = "abort" diff --git a/src/lib.rs b/src/lib.rs index 9890683..52da4d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,9 @@ -use std::{collections::HashMap, path::PathBuf}; - -use toml::Table; - +use std::{ + collections::HashMap, + fs::File, + io::{BufRead, BufReader}, + path::PathBuf, +}; #[derive(Debug)] pub struct Config { @@ -13,8 +15,8 @@ pub struct Config { } pub const PICTURE_EXTENTIONS: [&str; 17] = [ - "jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", - "psd", "eps", "ai", "indd", "raw", "ico", + "jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", "psd", "eps", + "ai", "indd", "raw", "ico", ]; pub const SOUND_EXTENTIONS: [&str; 10] = [ "mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus", @@ -27,35 +29,48 @@ pub const VIDEO_EXTENTIONS: [&str; 11] = [ ]; impl Config { - // TODO: refactor this nested match mess pub fn parse(path: PathBuf) -> Result> { let mut sources: HashMap = HashMap::new(); - let config_str = std::fs::read_to_string(path)?; - let config_parsed = &config_str.parse::()?; + let mut pictures_dir = None; + let mut videos_dir = None; + let mut music_dir = None; + let mut books_dir = None; - let pictures_dir = get_path(config_parsed, "pictures_dir"); - let videos_dir = get_path(config_parsed, "videos_dir"); - let music_dir = get_path(config_parsed, "music_dir"); - let books_dir = get_path(config_parsed, "books_dir"); + let config_file = File::open(path)?; + let reader = BufReader::new(config_file); + let mut current_section = String::new(); - match config_parsed.get("sources") { - Some(sources_value) => { - match sources_value { - toml::Value::Table(sources_value) => { - for (key, value) in sources_value.iter() { - sources.insert(key.clone(),PathBuf::from(value.as_str().unwrap())); - } - } - _ => { - return Err("sources should be a table".into()); + for line in reader + .lines() + .map_while(Result::ok) + .map(|l| String::from(l.trim())) + .filter(|l| !l.is_empty()) + .filter(|l| !l.starts_with('#')) + { + if line.starts_with('[') && line.ends_with(']') { + current_section = line[1..line.len() - 1].to_string(); + } else if let Some((key, value)) = line.split_once('=') { + let key_trimmed_string = key.trim().replace('"', ""); + let key_trimmed = key_trimmed_string.as_str(); + + let value_trimmed_string = value.trim().replace('"', ""); + let value_trimmed = value_trimmed_string.as_str(); + match current_section.as_str() { + "sources" => { + sources.insert(key_trimmed.into(), value_trimmed.into()); } + "destinations" => match key_trimmed { + "music_dir" => music_dir = Some(PathBuf::from(value_trimmed)), + "video_dir" => videos_dir = Some(PathBuf::from(value_trimmed)), + "books_dir" => books_dir = Some(PathBuf::from(value_trimmed)), + "pictures_dir" => pictures_dir = Some(PathBuf::from(value_trimmed)), + _ => {} + }, + _ => {} } } - None => { - return Err("sources not found".into()); - } - }; + } Ok(Self { pictures_dir, @@ -68,12 +83,13 @@ impl Config { pub fn create(path: &PathBuf) -> Result<(), Box> { let config_str = r#" +[destinations] pictures_dir = "/home/vavakado/Pictures" videos_dir = "/home/vavakado/Videos" music_dir = "/home/vavakado/Music" books_dir = "/home/vavakado/Books" -[sources] # put your own sources here +[sources] "Pictures" = "/home/vavakado/Pictures" "#; std::fs::write(path, config_str)?; @@ -81,9 +97,3 @@ books_dir = "/home/vavakado/Books" Ok(()) } } - -fn get_path(config: &Table, key: &str) -> Option { - config - .get(key) - .map(|value| PathBuf::from(value.as_str().unwrap())) -} diff --git a/src/main.rs b/src/main.rs index 0024eb1..8ab1d02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,8 @@ fn main() { let mut thread_pool: Vec> = Vec::new(); for selection in actual_selection { + // TODO: forget the assumption that the user won't try to sort like 100 dirs at the same + // time let thread = thread::spawn(move || { let _ = sort_files(selection, false); // TODO: handle errors }); @@ -103,6 +105,7 @@ fn main() { } } +// TODO: implement recuriveness fn sort_files(selection: String, _recursive: bool) -> Result<(), Box> { println!("Sorting {}", selection); @@ -116,7 +119,6 @@ fn sort_files(selection: String, _recursive: bool) -> Result<(), Box Result<(), Box PathBuf {