2025-05-03 21:20:54 +02:00
|
|
|
#[derive(Debug)]
|
2025-05-02 20:35:45 +02:00
|
|
|
pub enum Statement {
|
|
|
|
|
Insert,
|
|
|
|
|
Select,
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 21:20:54 +02:00
|
|
|
#[derive(Debug)]
|
2025-05-02 20:35:45 +02:00
|
|
|
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(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 19:21:05 +02:00
|
|
|
pub struct StatementExecuteResult {
|
|
|
|
|
pub msg: String,
|
|
|
|
|
}
|
2025-05-02 20:53:12 +02:00
|
|
|
|
2025-05-02 20:35:45 +02:00
|
|
|
impl Statement {
|
2025-05-02 20:53:12 +02:00
|
|
|
pub fn execute(&self) -> StatementExecuteResult {
|
2025-05-02 20:35:45 +02:00
|
|
|
match self {
|
2025-05-03 19:21:05 +02:00
|
|
|
Statement::Insert => StatementExecuteResult {
|
|
|
|
|
msg: String::from("insert"),
|
|
|
|
|
},
|
|
|
|
|
Statement::Select => StatementExecuteResult {
|
|
|
|
|
msg: String::from("select"),
|
|
|
|
|
},
|
2025-05-02 20:35:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|