rm function now working

This commit is contained in:
2026-08-21 15:02:45 +01:00
parent c6357a0b8c
commit 10eb4b0f10
2 changed files with 78 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
use crate::structs::{Library, Shelf};
use std::{env, fs, io, io::Read};
use std::{env, fs, io, io::Read, path::Path};
use xdg::BaseDirectories;
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
@@ -51,6 +51,8 @@ pub fn get_current_shelf(library: &mut Library) -> &mut Shelf {
// Get current shelf index and push the file
let current_shelf = &mut library.shelves[*&shelf_index];
current_shelf.content.sort();
current_shelf.content.dedup();
return current_shelf;
}
@@ -69,3 +71,36 @@ pub fn initialize_library() -> Library {
};
return default_library;
}
pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
// Turn given file into string
let match_path = Path::new(file_name);
// If the file path doesnt exist
if !match_path.exists() {
let file_ending = match_path.file_name().expect("Error getting file name");
// For every entry in current shelf
for (i, path) in shelf.content.iter().enumerate() {
// Turn the string back into a path
let shelf_path = Path::new(path).canonicalize()?;
if shelf_path.ends_with(file_ending) {
return Ok(i);
};
}
} // else if it does
let cannonical_path = match_path.canonicalize()?;
for (i, path) in shelf.content.iter().enumerate() {
let shelf_path = Path::new(path).canonicalize()?;
if cannonical_path == shelf_path {
return Ok(i);
}
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
))
}