saving and loading library implemented

This commit is contained in:
2026-08-19 08:50:28 +01:00
parent 3e5079fd07
commit 412b77851b
3 changed files with 122 additions and 49 deletions

View File

@@ -1,65 +1,64 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{env, fs, path::Path, process};
use crate::utils::save_library;
mod structs;
mod utils;
fn main() {
let mut args = env::args();
let _binary = args.next();
while let Some(arg) = args.next() {
match arg.as_str() {
"add" => add(&mut args),
_other => println!("needs parameters"),
let mut library = match utils::deserialize_state_file() {
Ok(library) => library,
Err(e) => {
eprintln!("Error creating library: {}", e);
process::exit(1)
}
};
if let Some(arg) = args.next() {
match arg.as_str() {
"add" => add_file(&mut args, &mut library),
"ls" => list_library(&mut library),
&_ => println!("no command"),
}
} else {
println!("no parameters")
}
return;
dbg!(&library);
save_library(library);
process::exit(0);
}
fn deserialize_var() -> Library {
let key = "HOME";
let content: Library = match env::var(key) {
Ok(val) => match serde_json::from_str(&val) {
Ok(library) => library,
Err(..) => initialize_library(),
},
Err(..) => initialize_library(),
};
return content;
fn list_library(library: &mut structs::Library) {
let current_shelf_index =
utils::get_shelf_index(&library.shelves, &library.current_shelf).unwrap_or(0);
let current_shelf = &library.shelves[current_shelf_index];
println!("Shelves: {}", library.shelves.len());
println!("Current Shelf: {}", &current_shelf.name);
println!("Content: ");
for path in &current_shelf.content {
println!()
}
}
fn initialize_library() -> Library {
let default_shelf: Shelf = Shelf {
id: 0,
name: "default".to_string(),
content: Vec::new(),
};
let mut shelf_vec: Vec<Shelf> = Vec::new();
shelf_vec.push(default_shelf);
let default_library: Library = Library {
current_shelf: 0,
shelves: shelf_vec,
};
return default_library;
}
fn add(args: &mut env::Args) {
fn add_file(args: &mut env::Args, library: &mut structs::Library) {
for file in args {
println!("Adding {}", &file);
let file_path = Path::new(&file);
let absolute_path = Path::canonicalize(&file_path).expect("No Such File");
println!("Current path: {}", absolute_path.to_str().unwrap());
let shelves = &library.shelves;
let current_shelf = &library.current_shelf;
let shelf_index = utils::get_shelf_index(shelves, current_shelf).unwrap_or(0);
let current_shelf = &mut library.shelves[*&shelf_index];
current_shelf
.content
.push(absolute_path.to_str().unwrap().to_string())
}
process::exit(0)
}
#[derive(Default, Deserialize, Serialize)]
struct Library {
current_shelf: u16,
shelves: Vec<Shelf>,
}
#[derive(Deserialize, Serialize)]
struct Shelf {
id: u64,
name: String,
content: Vec<String>,
// process::exit(0)
return;
}