first commmit

This commit is contained in:
Vladimir Rubin 2024-12-28 15:29:06 +02:00
commit cf5999a1c7
Signed by: vavakado
GPG key ID: CAB744727F36B524
6 changed files with 241 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

145
Cargo.lock generated Normal file
View file

@ -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",
]

7
Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "filesorters"
version = "0.1.0"
edition = "2021"
[dependencies]
toml = "0.8.19"

2
sorter_config.toml Normal file
View file

@ -0,0 +1,2 @@
[sources]
"Downloads" = "/home/vavakado/Downloads/"

74
src/lib.rs Normal file
View file

@ -0,0 +1,74 @@
use std::{collections::HashMap, path::PathBuf};
use toml::Table;
#[derive(Debug)]
pub struct Config {
pub pictures_dir: Option<PathBuf>,
pub videos_dir: Option<PathBuf>,
pub music_dir: Option<PathBuf>,
pub books_dir: Option<PathBuf>,
pub sources: HashMap<String, PathBuf>,
}
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<Self, Box<dyn std::error::Error>> {
let mut sources: HashMap<String, PathBuf> = HashMap::new();
let config_str = std::fs::read_to_string("sorter_config.toml")?;
let config_parsed = &config_str.parse::<Table>()?;
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<PathBuf> {
config
.get(key)
.map(|value| PathBuf::from(value.as_str().unwrap()))
}

12
src/main.rs Normal file
View file

@ -0,0 +1,12 @@
fn main() {
println!("Hello, world!");
match filesorters::Config::parse() {
Ok(config) => {
println!("{:#?}", config);
}
Err(err) => {
println!("Error: {}", err);
}
}
}