55 lines
1.3 KiB
Rust
55 lines
1.3 KiB
Rust
#[derive(Debug)]
|
|
pub enum Statement {
|
|
Insert,
|
|
Select,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
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(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct StatementExecuteResult {
|
|
pub msg: String,
|
|
}
|
|
|
|
impl Statement {
|
|
pub fn execute(&self) -> StatementExecuteResult {
|
|
match self {
|
|
Statement::Insert => StatementExecuteResult {
|
|
msg: String::from("insert"),
|
|
},
|
|
Statement::Select => StatementExecuteResult {
|
|
msg: String::from("select"),
|
|
},
|
|
}
|
|
}
|
|
}
|