Skip to content

Commit 3ba63de

Browse files
committed
Factor out parser into separate module.
1 parent 50f415a commit 3ba63de

2 files changed

Lines changed: 52 additions & 47 deletions

File tree

src/main.rs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std:: {
22
fs::{self, File}, io::{self, BufReader, BufWriter, Read}, path::Path, process::{exit, ExitCode}, str::{self}, sync::{Arc, Mutex}, thread
33
};
44

5-
6-
use clap::{Parser, Subcommand, ValueEnum};
5+
use parser::{Cli, Commands};
6+
use clap::{Parser, ValueEnum};
77
use xml::{self, reader::XmlEvent, EventReader};
88
use xml::common::{TextPosition, Position};
99
use colored::{Colorize};
1010

11+
mod parser;
1112
mod lexer;
1213
mod server;
1314
mod model;
@@ -127,6 +128,7 @@ fn append_folder_to_model(dir_path: &Path, model: Arc<Mutex<InMemoryModel>>, pro
127128
eprintln!("{}: Failed to read directory {dir_path} as \"{err}\"", "ERROR".bold().red(),
128129
dir_path = dir_path.to_str().unwrap().bold().bright_blue(),
129130
err = err.to_string().red());
131+
exit(-1);
130132
})?;
131133

132134
'step: for file in dir {
@@ -232,58 +234,13 @@ fn fetch_model(index_path: &str) -> Result<InMemoryModel, ()> {
232234
return Ok(model);
233235
}
234236

235-
#[derive(Parser)]
236-
#[command(name = "DocSense", version, author, about, long_about = None)]
237-
#[command(about = "A fast document indexing and search engine which runs locally on your machine.", long_about = None)]
238-
struct Cli {
239-
#[command(subcommand)]
240-
command: Commands,
241-
}
242237

243238
#[derive(ValueEnum, Clone, Debug, PartialEq)]
244239
enum RankMethod {
245240
Tfidf,
246241
Bm25
247242
}
248243

249-
#[derive(Subcommand)]
250-
enum Commands {
251-
#[command(
252-
about = "Search for a query string in an indexed file",
253-
long_about = "Search for a prompt string using BM25 (or TF-IDF) ranking algorithm across previously indexed documents."
254-
)]
255-
Search {
256-
#[arg(help = "Path to the .json index file (e.g., index.docsense.json)")]
257-
index_file_path: String,
258-
#[arg(help = "Search prompt string (e.g., 'deep neural networks')")]
259-
prompt: String,
260-
#[arg(short, long, default_value = "tfidf", value_enum, help = "Ranking algorithm to use")]
261-
rank_method: RankMethod,
262-
},
263-
264-
#[command(
265-
about = "Check how many files are indexed",
266-
long_about = "Display number of documents currently indexed in the specified index file. Useful for verifying the index state."
267-
)]
268-
Check {
269-
#[arg(default_value="index.json", help="Path to index file to inspect")]
270-
index_file_path: String,
271-
},
272-
273-
#[command(
274-
about = "Serve directory over HTTP with search interface",
275-
long_about = "Indexes the provided directory recursively and starts a web server for querying indexed files through a UI."
276-
)]
277-
Serve {
278-
#[arg(help = "Path to directory to index and serve")]
279-
dir_path: String,
280-
#[arg(default_value = "127.0.0.1:6969", help = "IP:PORT to bind HTTP server (e.g., 0.0.0.0:8080)")]
281-
address: String,
282-
#[arg(short, long, default_value = "tfidf", value_enum, help = "Ranking algorithm to use")]
283-
rank_method: RankMethod,
284-
}
285-
}
286-
287244
fn entry() -> Result<(), ()> {
288245
let cli = Cli::parse();
289246

src/parser.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use clap::{Subcommand, command, Parser};
2+
use crate::RankMethod;
3+
4+
#[derive(Parser)]
5+
#[command(name = "DocSense", version, author, about, long_about = None)]
6+
#[command(about = "A fast document indexing and search engine which runs locally on your machine.", long_about = None)]
7+
pub struct Cli {
8+
#[command(subcommand)]
9+
pub command: Commands,
10+
}
11+
12+
#[derive(Subcommand)]
13+
pub enum Commands {
14+
#[command(
15+
about = "Search for a query string in an indexed file",
16+
long_about = "Search for a prompt string using BM25 (or TF-IDF) ranking algorithm across previously indexed documents."
17+
)]
18+
Search {
19+
#[arg(help = "Path to the .json index file (e.g., index.docsense.json)")]
20+
index_file_path: String,
21+
#[arg(help = "Search prompt string (e.g., 'deep neural networks')")]
22+
prompt: String,
23+
#[arg(short, long, default_value = "tfidf", value_enum, help = "Ranking algorithm to use")]
24+
rank_method: RankMethod,
25+
},
26+
27+
#[command(
28+
about = "Check how many files are indexed",
29+
long_about = "Display number of documents currently indexed in the specified index file. Useful for verifying the index state."
30+
)]
31+
Check {
32+
#[arg(default_value="index.json", help="Path to index file to inspect")]
33+
index_file_path: String,
34+
},
35+
36+
#[command(
37+
about = "Serve directory over HTTP with search interface",
38+
long_about = "Indexes the provided directory recursively and starts a web server for querying indexed files through a UI."
39+
)]
40+
Serve {
41+
#[arg(help = "Path to directory to index and serve")]
42+
dir_path: String,
43+
#[arg(default_value = "127.0.0.1:6969", help = "IP:PORT to bind HTTP server (e.g., 0.0.0.0:8080)")]
44+
address: String,
45+
#[arg(short, long, default_value = "tfidf", value_enum, help = "Ranking algorithm to use")]
46+
rank_method: RankMethod,
47+
}
48+
}

0 commit comments

Comments
 (0)