refactor: pull things into own files, have a library
This commit is contained in:
parent
4848f2be2f
commit
ee23572983
7 changed files with 176 additions and 166 deletions
45
src/statements.rs
Normal file
45
src/statements.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue