commit cf5999a1c785cdb61d21abee584cc93697336850 Author: Vladimir Rubin Date: Sat Dec 28 15:29:06 2024 +0200 first commmit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6e9ed43 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,145 @@ +# This file is automatically @generated by Cargo. +# 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 = [ + "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 new file mode 100644 index 0000000..75f0da3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "filesorters" +version = "0.1.0" +edition = "2021" + +[dependencies] +toml = "0.8.19" diff --git a/sorter_config.toml b/sorter_config.toml new file mode 100644 index 0000000..8eb30c6 --- /dev/null +++ b/sorter_config.toml @@ -0,0 +1,2 @@ +[sources] +"Downloads" = "/home/vavakado/Downloads/" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..6e49251 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,74 @@ +use std::{collections::HashMap, path::PathBuf}; + +use toml::Table; + + +#[derive(Debug)] +pub struct Config { + pub pictures_dir: Option, + pub videos_dir: Option, + pub music_dir: Option, + pub books_dir: Option, + pub sources: HashMap, +} + +pub const PICTURE_EXTENTIONS: [&str; 17] = [ + ".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", +]; +pub const BOOK_EXTENTIONS: [&str; 11] = [ + ".pdf", ".epub", ".mobi", ".cbz", ".cbr", ".chm", ".djvu", ".fb2", ".lit", ".prc", ".xps", +]; +pub const VIDEO_EXTENTIONS: [&str; 11] = [ + ".mp4", ".mkv", ".avi", ".flv", ".webm", ".wmv", ".mov", ".m4v", ".3gp", ".3g2", ".swf", +]; + +impl Config { + // TODO: refactor this nested match mess + pub fn parse() -> Result> { + let mut sources: HashMap = HashMap::new(); + + let config_str = std::fs::read_to_string("sorter_config.toml")?; + let config_parsed = &config_str.parse::()?; + + 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"); + + 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()); + } + } + } + None => { + return Err("sources not found".into()); + } + }; + + Ok(Self { + pictures_dir, + videos_dir, + music_dir, + books_dir, + sources, + }) + } +} + +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 new file mode 100644 index 0000000..b8b3d25 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,12 @@ +fn main() { + println!("Hello, world!"); + + match filesorters::Config::parse() { + Ok(config) => { + println!("{:#?}", config); + } + Err(err) => { + println!("Error: {}", err); + } + } +}