#![forbid(unsafe_code)]
pub mod args;
pub mod format;
pub mod help;
pub mod shell;
use std::io::{IsTerminal, Read, Write};
use std::process::ExitCode;
use rudb::{Config, Database};
pub use args::{Action, Command, Options, parse};
pub use format::{Format, Settings};
pub use shell::{Shell, Stop};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub fn run(arguments: &[String], out: Box<dyn Write>, err: Box<dyn Write>) -> ExitCode {
let mut err = err;
match parse(arguments) {
Action::Version => {
let mut out = out;
let _ = writeln!(out, "rudb {VERSION}");
ExitCode::SUCCESS
}
Action::Help => {
let mut out = out;
let _ = write!(out, "{}", help::USAGE);
ExitCode::SUCCESS
}
Action::Config => {
let mut out = out;
print_config(&mut out);
ExitCode::SUCCESS
}
Action::Wrong(why) => {
let _ = writeln!(err, "rudb: {why}");
let _ = writeln!(err, "rudb: try `rudb -help`");
ExitCode::FAILURE
}
Action::Run(options) => {
let database = match Database::open(&options.database) {
Ok(database) => database,
Err(problem) => {
let _ = writeln!(err, "rudb: {}", problem.message());
return ExitCode::FAILURE;
}
};
let mut shell = Shell::new(&options, database, out, err);
let mut stop = shell.run_commands(&options.commands);
if stop == Stop::Done && !options.stop_after_commands {
stop = read_input(&mut shell, &options);
}
let _ = stop;
if shell.failed() { ExitCode::FAILURE } else { ExitCode::SUCCESS }
}
}
}
fn read_input(shell: &mut Shell, options: &Options) -> Stop {
let stdin = std::io::stdin();
let interactive = options.interactive.unwrap_or_else(|| stdin.is_terminal());
if !interactive {
let mut text = String::new();
if stdin.lock().read_to_string(&mut text).is_err() {
return Stop::Done;
}
return shell.run_input(&text);
}
shell.greet();
shell.prompt(&stdin)
}
fn print_config(out: &mut dyn Write) {
let _ = writeln!(out, "version: {VERSION}");
for (name, value) in Config::default().settings() {
let _ = writeln!(out, "{name}: {value}");
}
let _ = writeln!(out, "vector-size: 1024");
let _ = writeln!(out, "row-group-size: 122880");
let _ = writeln!(out, "storage-format: native (rudb v1), DuckDB import and export");
let _ = writeln!(out, "execution-tiers: interpreted");
let _ = writeln!(out, "duckdb-compat-level: 0 (nothing is implemented yet)");
let _ = writeln!(out, "target: {}", std::env::consts::ARCH);
let _ = writeln!(out, "os: {}", std::env::consts::OS);
}