commit 7d85c128f4357c973ff6981a69bb0d70c27ca593 Author: kjuulh Date: Sun Mar 27 17:33:24 2022 +0200 Simple input parser diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..649953f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqlite_clone.iml b/.idea/sqlite_clone.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/.idea/sqlite_clone.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..1821803 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "sqlite_clone" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3cbba4a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sqlite_clone" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..02bc1cb --- /dev/null +++ b/src/main.rs @@ -0,0 +1,65 @@ +use std::io; +use std::io::Write; +use std::process::exit; + +struct InputBuffer { + buffer: Option, +} + +impl InputBuffer { + pub fn new() -> Self { + return Self { + buffer: None + }; + } + + pub(crate) fn print_prompt(&mut self) -> &mut Self { + print!("db > "); + if let Err(_) = io::stdout().flush() { + panic!("could not print to stdout") + } + self + } + + pub(crate) fn read_input(&mut self) -> Result<&mut Self, String> { + self.buffer = Some(String::new()); + match &mut self.buffer { + Some(input_buffer) => { + if let Ok(_) = io::stdin().read_line(input_buffer) { + Ok(self) + } else { + Err(String::from("could not handle input")) + } + } + _ => { Err(String::from("Could not initialize buffer")) } + } + } + + pub(crate) fn parse(&self) { + match &self.buffer { + Some(command) => { + match command.as_str().replace("\n", "").trim() { + ".exit" => { + exit(0); + } + cmd => { + println!("Could not handle command: {cmd}") + } + } + } + None => {} + } + } +} + + +fn main() -> Result<(), String> { + let mut input_buffer = InputBuffer::new(); + + loop { + input_buffer + .print_prompt() + .read_input()? + .parse() + } +}