feat(osdb): parse insert and select statements
This commit is contained in:
parent
a9ab0fff01
commit
a7876f76bf
1 changed files with 115 additions and 5 deletions
120
src/main.rs
120
src/main.rs
|
|
@ -29,28 +29,138 @@ impl std::str::FromStr for MetaCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
startup_msg();
|
startup_msg();
|
||||||
while let Some(input) = read_input() {
|
while let Some(input) = read_input() {
|
||||||
match input.parse() {
|
match input.parse() {
|
||||||
Ok(cmd) => match cmd {
|
Ok(cmd) => match cmd {
|
||||||
MetaCommand::Exit => {
|
Command::MetaCommand(cmd) => match cmd {
|
||||||
println!("Good-bye");
|
MetaCommand::Exit => {
|
||||||
break;
|
println!("Good-bye");
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Command::Statement(stmt) => execute_statment(stmt),
|
||||||
},
|
},
|
||||||
Err(err) => eprintln!("{err}"),
|
Err(err) => eprintln!("{err}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn execute_statment(stmt: Statement) {
|
||||||
|
match stmt {
|
||||||
|
Statement::Insert => println!("insert"),
|
||||||
|
Statement::Select => println!("select"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn startup_msg() {
|
fn startup_msg() {
|
||||||
let name = env!("CARGO_PKG_NAME");
|
let name = env!("CARGO_PKG_NAME");
|
||||||
let version = env!("CARGO_PKG_VERSION");
|
let version = env!("CARGO_PKG_VERSION");
|
||||||
let authors = env!("CARGO_PKG_AUTHORS");
|
let authors = env!("CARGO_PKG_AUTHORS");
|
||||||
|
|
||||||
println!("{name} v{version} started.",);
|
println!("{name} v{version} started.",);
|
||||||
println!("Copyright 2024 {authors}. All rights reserved.")
|
println!("Copyright 2025 {authors}. All rights reserved.")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_input() -> Option<String> {
|
fn read_input() -> Option<String> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue