From bf12b9e86436f0d616dd7ed06c8ae37256d490ce Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:30:05 +0300 Subject: [PATCH] begin working on file indexing --- src/main.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index d3bac60..befbf21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::fs; +use std::{fs::{self, DirEntry}, path::Path}; use clap::{Parser, Subcommand}; use rusqlite::Connection; @@ -22,19 +22,46 @@ struct Args { command: Command } -fn index(path: &String) { +fn index(db: Connection, path: &String) { + // iterate resume files + let entries: Vec = fs::read_dir(Path::new(path).join("resume")) + .unwrap() + .filter_map(|f| f.ok()) + .collect(); + for entry in entries { + let file_type = match entry.file_type() { + Ok(file_type) => file_type, + Err(_) => continue + }; + if !file_type.is_file() { + continue; + } + let hash = match Path::new(&entry.file_name()).file_stem() { + Some(stem) => match stem.to_os_string().into_string() { + Ok(str) => str, + Err(os_str) => { + eprintln!("failed to convert file name for {:#?}", os_str); + continue; + } + }, + None => { + eprintln!("file {:#?} has no extension or conversion failed", entry.file_name()); + continue; + } + }; + + println!("{}", hash); + } } fn main() { let args = Args::parse(); - let db = Connection::open(args.db_path).unwrap(); - db.execute("INSERT INTO `test` (test) VALUES (?1)", (1337,)).unwrap(); match &args.command { Command::Index { path } => { - index(path); + index(db, path); } } } \ No newline at end of file