osdb/src/main.rs

184 lines
4.4 KiB
Rust
Raw Normal View History

2025-05-01 21:00:28 +02:00
enum MetaCommand {
Exit,
}
enum MetaCommandParseError {
Unrecognized { cmd: String },
}
impl std::fmt::Display for MetaCommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MetaCommandParseError::Unrecognized { cmd } => {
write!(f, "unrecognized meta-command {cmd:?}")
}
}
}
}
impl std::str::FromStr for MetaCommand {
type Err = MetaCommandParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
".exit" => Ok(MetaCommand::Exit),
cmd => Err(MetaCommandParseError::Unrecognized {
cmd: cmd.to_string(),
}),
}
}
}
enum Statement {
Insert,
Select,
}
enum StatementParseError {
Unrecognized { stmt: String },
}
impl std::fmt::Display for StatementParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StatementParseError::Unrecognized { stmt } => {
write!(f, "unrecognized statement {stmt:?}")
}
}
}
}
impl std::str::FromStr for Statement {
type Err = StatementParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim();
let lower = s.to_lowercase();
if lower.starts_with("insert") {
Ok(Statement::Insert)
} else if lower.starts_with("select") {
Ok(Statement::Select)
} else {
Err(StatementParseError::Unrecognized {
stmt: s.to_string(),
})
}
}
}
enum Command {
MetaCommand(MetaCommand),
Statement(Statement),
}
enum CommandParseError {
MetaCommand(MetaCommandParseError),
Statement(StatementParseError),
}
impl std::fmt::Display for CommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CommandParseError::MetaCommand(meta_command_parse_error) => {
write!(f, "{meta_command_parse_error}")
}
CommandParseError::Statement(statement_parse_error) => {
write!(f, "{statement_parse_error}")
}
}
}
}
impl From<MetaCommand> for Command {
fn from(value: MetaCommand) -> Self {
Command::MetaCommand(value)
}
}
impl From<MetaCommandParseError> for CommandParseError {
fn from(value: MetaCommandParseError) -> Self {
CommandParseError::MetaCommand(value)
}
}
impl From<Statement> for Command {
fn from(value: Statement) -> Self {
Command::Statement(value)
}
}
impl From<StatementParseError> for CommandParseError {
fn from(value: StatementParseError) -> Self {
CommandParseError::Statement(value)
}
}
impl std::str::FromStr for Command {
type Err = CommandParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with(".") {
s.parse::<MetaCommand>()
.map(|x| x.into())
.map_err(|x| x.into())
} else {
s.parse::<Statement>()
.map(|x| x.into())
.map_err(|x| x.into())
}
}
}
2025-05-01 19:24:18 +02:00
fn main() {
2025-05-01 21:00:28 +02:00
startup_msg();
while let Some(input) = read_input() {
match input.parse() {
Ok(cmd) => match cmd {
Command::MetaCommand(cmd) => match cmd {
MetaCommand::Exit => {
println!("Good-bye");
break;
}
},
Command::Statement(stmt) => execute_statment(stmt),
2025-05-01 21:00:28 +02:00
},
Err(err) => eprintln!("{err}"),
}
}
}
fn execute_statment(stmt: Statement) {
match stmt {
Statement::Insert => println!("insert"),
Statement::Select => println!("select"),
}
}
2025-05-01 21:00:28 +02:00
fn startup_msg() {
let name = env!("CARGO_PKG_NAME");
let version = env!("CARGO_PKG_VERSION");
let authors = env!("CARGO_PKG_AUTHORS");
println!("{name} v{version} started.",);
println!("Copyright 2025 {authors}. All rights reserved.")
2025-05-01 21:00:28 +02:00
}
fn read_input() -> Option<String> {
use std::io::{BufRead, Write};
print!("osdb > ");
std::io::stdout().flush().expect("failed to flush stdout");
let mut input = String::new();
let len = std::io::stdin()
.lock()
.read_line(&mut input)
.expect("failed to read input from stdin");
if len == 0 {
None
} else {
Some(input)
}
2025-05-01 19:24:18 +02:00
}