1#![forbid(unsafe_code)]
16
17pub mod args;
18pub mod format;
19pub mod help;
20pub mod shell;
21
22use std::io::{IsTerminal, Read, Write};
23use std::process::ExitCode;
24
25use rudb::Database;
26
27pub use args::{Action, Command, Options, parse};
28pub use format::{Format, Settings};
29pub use shell::{Shell, Stop};
30
31pub const VERSION: &str = env!("CARGO_PKG_VERSION");
33
34pub fn run(arguments: &[String], out: Box<dyn Write>, err: Box<dyn Write>) -> ExitCode {
39 let mut err = err;
40 match parse(arguments) {
41 Action::Version => {
42 let mut out = out;
43 let _ = writeln!(out, "rudb {VERSION}");
44 ExitCode::SUCCESS
45 }
46 Action::Help => {
47 let mut out = out;
48 let _ = write!(out, "{}", help::USAGE);
49 ExitCode::SUCCESS
50 }
51 Action::Config => {
52 let mut out = out;
53 print_config(&mut out);
54 ExitCode::SUCCESS
55 }
56 Action::Wrong(why) => {
57 let _ = writeln!(err, "rudb: {why}");
58 let _ = writeln!(err, "rudb: try `rudb -help`");
59 ExitCode::FAILURE
60 }
61 Action::Run(options) => {
62 let database = match Database::open(&options.database) {
65 Ok(database) => database,
66 Err(problem) => {
67 let _ = writeln!(err, "rudb: {}", problem.message());
68 return ExitCode::FAILURE;
69 }
70 };
71 let mut shell = Shell::new(&options, database, out, err);
72 let mut stop = shell.run_commands(&options.commands);
73 if stop == Stop::Done && !options.stop_after_commands {
74 stop = read_input(&mut shell, &options);
75 }
76 let _ = stop;
77 if shell.failed() { ExitCode::FAILURE } else { ExitCode::SUCCESS }
78 }
79 }
80}
81
82fn read_input(shell: &mut Shell, options: &Options) -> Stop {
89 let stdin = std::io::stdin();
90 let interactive = options.interactive.unwrap_or_else(|| stdin.is_terminal());
91 if !interactive {
92 let mut text = String::new();
93 if stdin.lock().read_to_string(&mut text).is_err() {
94 return Stop::Done;
95 }
96 return shell.run_input(&text);
97 }
98 shell.greet();
99 shell.prompt(&stdin)
100}
101
102fn print_config(out: &mut dyn Write) {
105 let _ = writeln!(out, "version: {VERSION}");
106 let _ = writeln!(out, "vector-size: 1024");
107 let _ = writeln!(out, "row-group-size: 122880");
108 let _ = writeln!(out, "storage-format: native (rudb v1), DuckDB import and export");
109 let _ = writeln!(out, "execution-tiers: interpreted");
110 let _ = writeln!(out, "duckdb-compat-level: 0 (nothing is implemented yet)");
111 let _ = writeln!(out, "target: {}", std::env::consts::ARCH);
112 let _ = writeln!(out, "os: {}", std::env::consts::OS);
113}