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
This commit is contained in:
parent
c1f80f9abe
commit
3d9c39c872
4 changed files with 49 additions and 180 deletions
139
Cargo.lock
generated
139
Cargo.lock
generated
|
@ -2,145 +2,6 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "equivalent"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "filesorters"
|
name = "filesorters"
|
||||||
version = "0.1.0"
|
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",
|
|
||||||
]
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
toml = {version="0.8.19", default-features = false, features = ["parse", "preserve_order"]}
|
|
||||||
|
|
||||||
[profile.release.package."*"]
|
[profile.release.package."*"]
|
||||||
opt-level = "z"
|
opt-level = "z"
|
||||||
|
@ -13,5 +12,5 @@ opt-level = "z"
|
||||||
strip = true
|
strip = true
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
opt-level = "s"
|
opt-level = "s"
|
||||||
lto = "thin"
|
lto = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
|
78
src/lib.rs
78
src/lib.rs
|
@ -1,7 +1,9 @@
|
||||||
use std::{collections::HashMap, path::PathBuf};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
use toml::Table;
|
fs::File,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -13,8 +15,8 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const PICTURE_EXTENTIONS: [&str; 17] = [
|
pub const PICTURE_EXTENTIONS: [&str; 17] = [
|
||||||
"jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga",
|
"jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", "psd", "eps",
|
||||||
"psd", "eps", "ai", "indd", "raw", "ico",
|
"ai", "indd", "raw", "ico",
|
||||||
];
|
];
|
||||||
pub const SOUND_EXTENTIONS: [&str; 10] = [
|
pub const SOUND_EXTENTIONS: [&str; 10] = [
|
||||||
"mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus",
|
"mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus",
|
||||||
|
@ -27,35 +29,48 @@ pub const VIDEO_EXTENTIONS: [&str; 11] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
// TODO: refactor this nested match mess
|
|
||||||
pub fn parse(path: PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn parse(path: PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let mut sources: HashMap<String, PathBuf> = HashMap::new();
|
let mut sources: HashMap<String, PathBuf> = HashMap::new();
|
||||||
|
|
||||||
let config_str = std::fs::read_to_string(path)?;
|
let mut pictures_dir = None;
|
||||||
let config_parsed = &config_str.parse::<Table>()?;
|
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 config_file = File::open(path)?;
|
||||||
let videos_dir = get_path(config_parsed, "videos_dir");
|
let reader = BufReader::new(config_file);
|
||||||
let music_dir = get_path(config_parsed, "music_dir");
|
let mut current_section = String::new();
|
||||||
let books_dir = get_path(config_parsed, "books_dir");
|
|
||||||
|
|
||||||
match config_parsed.get("sources") {
|
for line in reader
|
||||||
Some(sources_value) => {
|
.lines()
|
||||||
match sources_value {
|
.map_while(Result::ok)
|
||||||
toml::Value::Table(sources_value) => {
|
.map(|l| String::from(l.trim()))
|
||||||
for (key, value) in sources_value.iter() {
|
.filter(|l| !l.is_empty())
|
||||||
sources.insert(key.clone(),PathBuf::from(value.as_str().unwrap()));
|
.filter(|l| !l.starts_with('#'))
|
||||||
}
|
{
|
||||||
}
|
if line.starts_with('[') && line.ends_with(']') {
|
||||||
_ => {
|
current_section = line[1..line.len() - 1].to_string();
|
||||||
return Err("sources should be a table".into());
|
} 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 {
|
Ok(Self {
|
||||||
pictures_dir,
|
pictures_dir,
|
||||||
|
@ -68,12 +83,13 @@ impl Config {
|
||||||
|
|
||||||
pub fn create(path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn create(path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let config_str = r#"
|
let config_str = r#"
|
||||||
|
[destinations]
|
||||||
pictures_dir = "/home/vavakado/Pictures"
|
pictures_dir = "/home/vavakado/Pictures"
|
||||||
videos_dir = "/home/vavakado/Videos"
|
videos_dir = "/home/vavakado/Videos"
|
||||||
music_dir = "/home/vavakado/Music"
|
music_dir = "/home/vavakado/Music"
|
||||||
books_dir = "/home/vavakado/Books"
|
books_dir = "/home/vavakado/Books"
|
||||||
|
|
||||||
[sources] # put your own sources here
|
[sources]
|
||||||
"Pictures" = "/home/vavakado/Pictures"
|
"Pictures" = "/home/vavakado/Pictures"
|
||||||
"#;
|
"#;
|
||||||
std::fs::write(path, config_str)?;
|
std::fs::write(path, config_str)?;
|
||||||
|
@ -81,9 +97,3 @@ books_dir = "/home/vavakado/Books"
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path(config: &Table, key: &str) -> Option<PathBuf> {
|
|
||||||
config
|
|
||||||
.get(key)
|
|
||||||
.map(|value| PathBuf::from(value.as_str().unwrap()))
|
|
||||||
}
|
|
||||||
|
|
|
@ -91,6 +91,8 @@ fn main() {
|
||||||
let mut thread_pool: Vec<thread::JoinHandle<()>> = Vec::new();
|
let mut thread_pool: Vec<thread::JoinHandle<()>> = Vec::new();
|
||||||
|
|
||||||
for selection in actual_selection {
|
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 thread = thread::spawn(move || {
|
||||||
let _ = sort_files(selection, false); // TODO: handle errors
|
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<dyn std::error::Error>> {
|
fn sort_files(selection: String, _recursive: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Sorting {}", selection);
|
println!("Sorting {}", selection);
|
||||||
|
|
||||||
|
@ -116,7 +119,6 @@ fn sort_files(selection: String, _recursive: bool) -> Result<(), Box<dyn std::er
|
||||||
|
|
||||||
let dir = fs::read_dir(search_path)?;
|
let dir = fs::read_dir(search_path)?;
|
||||||
|
|
||||||
|
|
||||||
// TODO: Simplyfy by using an array of tulpes instead of a bucket of ifs
|
// TODO: Simplyfy by using an array of tulpes instead of a bucket of ifs
|
||||||
for entry in dir.flatten().filter(|e| e.metadata().unwrap().is_file()) {
|
for entry in dir.flatten().filter(|e| e.metadata().unwrap().is_file()) {
|
||||||
if let Some(extension) = entry.path().extension().and_then(|e| e.to_str()) {
|
if let Some(extension) = entry.path().extension().and_then(|e| e.to_str()) {
|
||||||
|
@ -162,10 +164,7 @@ fn sort_files(selection: String, _recursive: bool) -> Result<(), Box<dyn std::er
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_file_to_directory(path: &std::path::Path, dir: &std::path::Path) {
|
fn move_file_to_directory(path: &std::path::Path, dir: &std::path::Path) {
|
||||||
let _ = fs::rename(
|
let _ = fs::rename(path, dir.join(path.file_name().unwrap()));
|
||||||
path,
|
|
||||||
dir.join(path.file_name().unwrap())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_config_path() -> PathBuf {
|
fn get_config_path() -> PathBuf {
|
||||||
|
|
Loading…
Reference in a new issue