diff --git a/src/main.rs b/src/main.rs index afdf455..98fe9f1 100644 --- a/src/main.rs +++ b/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"), + 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: {}", ¤t_shelf.name); + println!("Content: "); + for path in ¤t_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 = 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, -} - -#[derive(Deserialize, Serialize)] -struct Shelf { - id: u64, - name: String, - content: Vec, + // process::exit(0) + return; } diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..3204c03 --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,14 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Deserialize, Serialize, Debug)] +pub struct Library { + pub current_shelf: u8, + pub shelves: Vec, +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct Shelf { + pub id: u8, + pub name: String, + pub content: Vec, +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..efd6fe9 --- /dev/null +++ b/src/utils.rs @@ -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, id: &u8) -> Option { + shelves.iter().position(|shelf| shelf.id == *id) +} + +pub fn deserialize_state_file() -> io::Result { + 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 { + 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 = Vec::new(); + shelf_vec.push(default_shelf); + let default_library: Library = Library { + current_shelf: 0, + shelves: shelf_vec, + }; + return default_library; +}