45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
pub enum Statement {
|
|
Insert,
|
|
Select,
|
|
}
|
|
|
|
pub 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(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Statement {
|
|
pub fn execute(&self) {
|
|
match self {
|
|
Statement::Insert => println!("insert"),
|
|
Statement::Select => println!("select"),
|
|
}
|
|
}
|
|
}
|