made util function for finding files with index

This commit is contained in:
2026-08-21 16:47:13 +01:00
parent 3a4e32471a
commit 70289215a7

View File

@@ -3,6 +3,7 @@ use std::{
fs, fs,
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
process,
}; };
use xdg::BaseDirectories; use xdg::BaseDirectories;
@@ -118,3 +119,23 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name), format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
)) ))
} }
pub fn match_file_name_or_index(file: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
let index: usize = match file.parse::<usize>() {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index]);
index
}
Err(_) => match match_file_name(&file, shelf) {
Ok(index) => {
println!("path is: {}", &shelf.content[*&index as usize]);
index
}
Err(e) => {
eprintln!("{e}");
process::exit(1)
}
},
};
Ok(index)
}