Simple input parser

This commit is contained in:
Kasper Juul Hermansen 2022-03-27 17:33:24 +02:00
commit 7d85c128f4
8 changed files with 114 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View File

@ -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

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/sqlite_clone.iml" filepath="$PROJECT_DIR$/.idea/sqlite_clone.iml" />
</modules>
</component>
</project>

11
.idea/sqlite_clone.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

7
Cargo.lock generated Normal file
View File

@ -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"

8
Cargo.toml Normal file
View File

@ -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]

65
src/main.rs Normal file
View File

@ -0,0 +1,65 @@
use std::io;
use std::io::Write;
use std::process::exit;
struct InputBuffer {
buffer: Option<String>,
}
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()
}
}