saving and loading library implemented
This commit is contained in:
97
src/main.rs
97
src/main.rs
@@ -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"),
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
fn deserialize_var() -> Library {
|
||||
let key = "HOME";
|
||||
let content: Library = match env::var(key) {
|
||||
Ok(val) => match serde_json::from_str(&val) {
|
||||
let mut library = match utils::deserialize_state_file() {
|
||||
Ok(library) => library,
|
||||
Err(..) => initialize_library(),
|
||||
},
|
||||
Err(..) => initialize_library(),
|
||||
Err(e) => {
|
||||
eprintln!("Error creating library: {}", e);
|
||||
process::exit(1)
|
||||
}
|
||||
};
|
||||
return content;
|
||||
|
||||
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")
|
||||
}
|
||||
dbg!(&library);
|
||||
save_library(library);
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
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 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: {}", ¤t_shelf.name);
|
||||
println!("Content: ");
|
||||
for path in ¤t_shelf.content {
|
||||
println!()
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
14
src/structs.rs
Normal file
14
src/structs.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, Debug)]
|
||||
pub struct Library {
|
||||
pub current_shelf: u8,
|
||||
pub shelves: Vec<Shelf>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Shelf {
|
||||
pub id: u8,
|
||||
pub name: String,
|
||||
pub content: Vec<String>,
|
||||
}
|
||||
60
src/utils.rs
Normal file
60
src/utils.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use crate::structs::{Library, Shelf};
|
||||
use std::{env, fs, io, io::Read};
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
|
||||
shelves.iter().position(|shelf| shelf.id == *id)
|
||||
}
|
||||
|
||||
pub fn deserialize_state_file() -> io::Result<Library> {
|
||||
let xdg_dirs = get_xdg();
|
||||
let state_file_path = xdg_dirs.place_state_file("shelf-state.json")?;
|
||||
|
||||
let state_file_content = match fs::read_to_string(state_file_path) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
eprintln!("Could not read state file: {}", e);
|
||||
return Ok(initialize_library());
|
||||
}
|
||||
};
|
||||
|
||||
let state_file_decoded: Library = serde_json::from_str(&state_file_content)?;
|
||||
|
||||
Ok(state_file_decoded)
|
||||
}
|
||||
|
||||
pub fn get_xdg() -> BaseDirectories {
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("shelf");
|
||||
return xdg_dirs;
|
||||
}
|
||||
|
||||
pub fn get_state_file() -> io::Result<std::path::PathBuf> {
|
||||
let xdg_dirs = get_xdg();
|
||||
let state_file_path = xdg_dirs.place_state_file("shelf-state.json")?;
|
||||
Ok(state_file_path)
|
||||
}
|
||||
|
||||
pub fn save_library(library: Library) -> io::Result<()> {
|
||||
let seralized_library = serde_json::to_string(&library).unwrap_or_default();
|
||||
let state_file = get_state_file()?;
|
||||
|
||||
fs::write(state_file, seralized_library)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub 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;
|
||||
}
|
||||
Reference in New Issue
Block a user