Skip to content

Commit 9c5faa9

Browse files
committed
Append extension to index file.
1 parent 9774afd commit 9c5faa9

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

src/lexer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use rust_stemmers::{Algorithm, Stemmer};
22

33
#[derive(Debug)]
44
pub struct Lexer<'a> {
5-
// Lifetimes implemented as content is not owned
6-
// as used it will be assigned or shifted
75
content: &'a [char]
86
}
97

src/main.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ fn parse_file_by_ext(file_path: &Path) -> Result<String, ()> {
9797

9898
/* Save the model to a path as json file */
9999
fn save_model_as_json(model: &InMemoryModel, index_path: &str) -> Result<(), ()> {
100-
println!("Saving folder index to {} ...", index_path);
100+
println!("Saving {} ...", index_path.bright_blue());
101101

102102
let index_file = File::create(index_path).map_err(|err| {
103-
eprintln!("{err}: Failed to create file {path} as {msg}", err = "ERROR".bold().red(), path = index_path.bright_blue(), msg = err.to_string().red());
103+
eprintln!("{}: Failed to create file {path} as {err}", "ERROR".bold().red(), path = index_path.bright_blue(), err = err.to_string().red());
104104
})?;
105105

106106
serde_json::to_writer(BufWriter::new(index_file), &model).unwrap_or_else(|err| {
107-
eprintln!("{err}: Failed to save file {path} as {msg}", err = "ERROR".bold().red(), path = index_path.bright_blue(), msg = err.to_string().red());
107+
eprintln!("{}: Failed to save file {path} as {err}", "ERROR".bold().red(), path = index_path.bright_blue(), err = err.to_string().red());
108108
});
109109
Ok(())
110110
}
@@ -289,18 +289,19 @@ fn entry() -> Result<(), ()> {
289289
// Default address
290290
let address = args.next().unwrap_or("127.0.0.1:6969".to_string());
291291

292-
// TODO: Figure out index_path based on dir_path
293-
let index_path = DEFAULT_INDEX_JSON_PATH;
294-
let exists = Path::new(index_path).exists();
295-
292+
// TODO: Figure out a better way to append extension ".docsense.json"
293+
let mut index_path = Path::new(&dir_path).to_path_buf();
294+
index_path.push(".docsense.json");
295+
296296
let model: Arc<Mutex<InMemoryModel>>;
297-
if exists {
297+
if index_path.exists() {
298298
// Fetch already existing model
299-
model = Arc::new(Mutex::new(fetch_model(&DEFAULT_INDEX_JSON_PATH).unwrap_or_else(|()| {
300-
eprintln!("{}: Failed to fetch model for {}.", "ERROR".bold().red(), DEFAULT_INDEX_JSON_PATH.bright_blue());
299+
model = Arc::new(Mutex::new(fetch_model(&index_path.to_str().unwrap()).unwrap_or_else(|()| {
300+
eprintln!("{}: Failed to fetch model for {}.", "ERROR".bold().red(), index_path.to_string_lossy().bright_blue());
301301
exit(1);
302302
})));
303303
} else {
304+
// Create a new model if not present
304305
model = Arc::new(Mutex::new(Default::default()));
305306
}
306307

@@ -313,7 +314,7 @@ fn entry() -> Result<(), ()> {
313314
// Save the model only when some files are processed
314315
if processed > 0 {
315316
let model= model.lock().unwrap();
316-
save_model_as_json(&model, index_path).unwrap();
317+
save_model_as_json(&model, index_path.to_str().unwrap()).unwrap();
317318
}
318319
println!("{}: Finished indexing ...", "INFO".cyan());
319320
});
@@ -332,17 +333,15 @@ fn entry() -> Result<(), ()> {
332333
}
333334

334335
fn main() -> io::Result<()> {
335-
336+
336337
match entry() {
337338
Ok(()) => ExitCode::SUCCESS,
338339
Err(_) => ExitCode::FAILURE
339340
};
340-
341-
return Ok(());
341+
Ok(())
342342
}
343343

344344
// TODO: Search result must consist of clickable links to open that file or file section
345-
// TODO: Parse pdf files
346345
// TODO: Synonym terms
347346
// TODO: Add levenstein distance or cosine similarity
348347
// TODO: Add better document ranker specifically "Okapi BM-25"

0 commit comments

Comments
 (0)