11use 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
55use xml:: { self , reader:: XmlEvent , EventReader } ;
66use xml:: common:: { TextPosition , Position } ;
7- use colored:: { ColoredString , Colorize } ;
7+ use colored:: { Colorize } ;
88
99mod lexer;
1010mod 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 */
4070fn 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