From d9c2d5284cbeef08ff2e2d764bf2a94975b12479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Mon, 27 Mar 2023 11:19:00 -0300 Subject: [PATCH] feat: Add .index.json extension --- src/main.rs | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0629447..9132047 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,24 +147,46 @@ fn entry() -> Result<(), ()> { eprintln!("ERROR: no directory is provided for {subcommand} subcommand"); })?; - // TODO: figure out index_path based on dir_path - let index_path = "index.json"; + let mut index_path = std::path::PathBuf::from(dir_path.clone()); + let new_extension = index_path + .extension() + .map(|ext| { + let mut ext = ext.to_string_lossy().into_owned(); + ext.push_str(".index.json"); + ext + }) + .unwrap_or("index.json".to_string()); + assert!( + index_path.set_extension(new_extension), + "failed to set the extension" + ); let address = args.next().unwrap_or("127.0.0.1:6969".to_string()); - let exists = Path::new(index_path).try_exists().map_err(|err| { - eprintln!("ERROR: could not check the existence of file {index_path}: {err}"); + let exists = index_path.try_exists().map_err(|err| { + eprintln!( + "ERROR: could not check the existence of file {index_path}: {err}", + index_path = index_path.display() + ); })?; let model: Arc>; if exists { let index_file = File::open(&index_path).map_err(|err| { - eprintln!("ERROR: could not open index file {index_path}: {err}"); + eprintln!( + "ERROR: could not open index file {index_path}: {err}", + index_path = index_path.display() + ); })?; - model = Arc::new(Mutex::new(serde_json::from_reader(index_file).map_err(|err| { - eprintln!("ERROR: could not parse index file {index_path}: {err}"); - })?)); + model = Arc::new(Mutex::new(serde_json::from_reader(index_file).map_err( + |err| { + eprintln!( + "ERROR: could not parse index file {index_path}: {err}", + index_path = index_path.display() + ); + }, + )?)); } else { model = Arc::new(Mutex::new(Default::default())); } @@ -177,7 +199,11 @@ fn entry() -> Result<(), ()> { add_folder_to_model(Path::new(&dir_path), Arc::clone(&model), &mut processed).unwrap(); if processed > 0 { let model = model.lock().unwrap(); - save_model_as_json(&model, index_path).unwrap(); + save_model_as_json( + &model, + index_path.as_os_str().to_str().expect("valid UTF-8"), + ) + .unwrap(); } println!("Finished indexing"); });