-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
103 lines (91 loc) · 2.98 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::{
env,
path::Path,
process,
};
mod btree;
mod commands;
mod constants;
mod database;
mod parse;
mod rustyline_config;
mod utils;
use utils::init_file::file_init;
use commands::{
meta_command::run_meta_command, process_command, sql_command::run_sql_command, CommandType,
};
use rustyline_config::{get_config, REPLHelper};
use crate::commands::sql_command::SQLCommand;
fn main() -> rustyline::Result<()> {
let args: Vec<String> = env::args().collect();
println!("{:?}", args);
if args.len() == 1 {
println!("expected database file name");
process::exit(1)
} else if args.len() > 2 {
println!("unexpected arguments");
process::exit(1)
}
if args[0] == "--help" || args[0] == "-h" {
println!("help message");
process::exit(1)
} else if args[0] == "--version" || args[0] == "-v" {
println!("0.1.0");
process::exit(1)
}
let mut database = if Path::new(&args[1]).exists() {
// maybe match here
let file = file_init(&args[1]).unwrap();
database::database::Database::new(args[1].to_string(), file)
} else {
println!("invalid database file");
process::exit(1)
};
let config = get_config();
let helper = REPLHelper::default();
let mut repl = Editor::with_config(config).unwrap();
repl.set_helper(Some(helper));
if repl.load_history("history.txt").is_err() {
println!("No previous history.");
}
let prompt = format!("RSQL> ");
repl.helper_mut().expect("No helper found").colored_prompt =
format!("\x1b[1;32m{}\x1b[0m", prompt);
loop {
let readline = repl.readline(&prompt);
match readline {
Ok(command) => {
let _ = repl.add_history_entry(command.as_str());
match process_command(&command) {
CommandType::TypeSQL(cmd) => match cmd {
SQLCommand::Invalid(err) => println!("an error occured: {}", err),
_ => match run_sql_command(command, &mut database) {
Ok(result) => println!("{}", result),
Err(err) => println!("an error occured: \n{}", err),
},
},
CommandType::TypeMeta(cmd) => match run_meta_command(cmd) {
Ok(result) => println!("{}", result),
Err(err) => println!("an error occured: {}", err),
},
};
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
break;
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
let _ = repl.save_history("history.txt");
Ok(())
}