Skip to content

Commit 672ed65

Browse files
committed
Add support for parsing text files
1 parent 201c161 commit 672ed65

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ docs.gl/
33

44
index.json
55
index.db
6+
index-big.json

index-big.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std:: {
2-
env::{self}, fs::{self, File}, io::{self, BufReader, BufWriter}, path::{Path}, process::{exit, ExitCode}, str::{self}
2+
env::{self}, fs::{self, File}, io::{self, BufReader, BufWriter, Read}, path::Path, process::{exit, ExitCode}, str::{self}
33
};
44

55
use xml::{self, reader::XmlEvent, EventReader};
66
use xml::common::{TextPosition, Position};
7-
use colored::{ColoredString, Colorize};
7+
use colored::{Colorize};
88

99
mod lexer;
1010
mod server;
@@ -35,6 +35,36 @@ fn parse_xml_file(file_path: &Path) -> Result<String, ()> {
3535
Ok(content)
3636
}
3737

38+
fn parse_txt_file(file_path: &Path) -> Result<String, ()> {
39+
let mut content = String::new();
40+
let mut file = File::open(file_path).map_err(|err| {
41+
eprintln!("{}: Could not open file {path} as {err}", "ERROR".bold().red(), path = file_path.to_string_lossy().bright_blue(), err = err.to_string().red());
42+
})?;
43+
44+
let _ = file.read_to_string(&mut content);
45+
Ok(content)
46+
}
47+
48+
fn parse_file_by_ext(file_path: &Path) -> Result<String, ()> {
49+
let ext = file_path.extension().ok_or_else(|| {
50+
eprintln!("{}: Could not get extension of {path}", "ERROR".bold().red(), path = file_path.to_string_lossy().bright_blue());
51+
})?.to_str();
52+
53+
match ext.unwrap() {
54+
"xml" | "xhtml" => {
55+
return parse_xml_file(file_path);
56+
}
57+
58+
"txt" | "md" => {
59+
return parse_txt_file(file_path);
60+
}
61+
62+
_ => {
63+
eprintln!("{}: Extension {ext} is unsupported", "ERROR".bold().red(), ext = ext.unwrap());
64+
Err(())
65+
}
66+
}
67+
}
3868

3969
/* Save the model to a path as json file */
4070
fn save_model_as_json(model: &InMemoryModel, index_path: &str) -> Result<(), ()> {
@@ -67,10 +97,11 @@ fn append_folder_to_model(dir_path: &Path, model: &mut dyn Model) -> Result<(),
6797

6898
let file_path = file.path();
6999
let file_path_str = file_path.to_str().unwrap();
100+
let file_ext = file_path.extension();
70101

71102
// Skip unsupported files
72-
if let Some(ext) = file_path.extension() {
73-
const ALLOWED_EXTS: [&str; 2] = ["xml", "xhtml"];
103+
if let Some(ext) = file_ext {
104+
const ALLOWED_EXTS: [&str; 4] = ["xml", "xhtml", "txt", "md"];
74105
if !ALLOWED_EXTS.contains(&ext.to_str().unwrap()) {
75106
println!("{}: Skipping non-XML file {}", "INFO".cyan(), file_path_str.bright_yellow());
76107
continue 'step;
@@ -90,14 +121,14 @@ fn append_folder_to_model(dir_path: &Path, model: &mut dyn Model) -> Result<(),
90121

91122
println!("Indexing {} ...", file_path_str.bright_cyan());
92123

93-
let content = match parse_xml_file(&file_path) {
124+
let content = match parse_file_by_ext(&file_path) {
94125
Ok(content) => content.chars().collect::<Vec<_>>(),
95126
Err(err) => {
96127
eprintln!("{error}: Failed to read xml file {fp}: {msg:?}", error = "ERROR".bold().red(), fp = file_path.to_str().unwrap().bright_blue(), msg = err);
97128
continue 'step;
98129
}
99130
};
100-
131+
101132
// Core Operation
102133
model.add_document(file_path, &content)?;
103134
}

0 commit comments

Comments
 (0)