This commit is contained in:
33
sqlite_core/src/interaction/buffer.rs
Normal file
33
sqlite_core/src/interaction/buffer.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::interaction::Interaction;
|
||||
|
||||
pub struct Buffer {
|
||||
int_buffer: Arc<Mutex<Vec<String>>>,
|
||||
out_buffer: Arc<Mutex<Vec<String>>>,
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
pub fn new(int_buffer: Arc<Mutex<Vec<String>>>, out_buffer: Arc<Mutex<Vec<String>>>) -> Self {
|
||||
Self {
|
||||
int_buffer,
|
||||
out_buffer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Interaction for Buffer {
|
||||
fn input(&self) -> Result<String, String> {
|
||||
let mut int_buffer = self.int_buffer.lock().unwrap();
|
||||
if let Some(input_string) = int_buffer.pop() {
|
||||
Ok(input_string)
|
||||
} else {
|
||||
Err(String::from("internal_buffer is empty"))
|
||||
}
|
||||
}
|
||||
|
||||
fn output(&self, output: String) {
|
||||
let mut out_buffer = self.out_buffer.lock().unwrap();
|
||||
out_buffer.push(output)
|
||||
}
|
||||
}
|
26
sqlite_core/src/interaction/command_line.rs
Normal file
26
sqlite_core/src/interaction/command_line.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::interaction::Interaction;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct CommandLine {}
|
||||
|
||||
impl CommandLine {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Interaction for CommandLine {
|
||||
fn input(&self) -> Result<String, String> {
|
||||
let buffer: &mut String = &mut String::new();
|
||||
if let Ok(_) = std::io::stdin().read_line(buffer) {
|
||||
return Ok(buffer.to_string());
|
||||
} else {
|
||||
return Err(String::from("could not read input buffer"));
|
||||
}
|
||||
}
|
||||
|
||||
fn output(&self, output: String) {
|
||||
let _ = std::io::stdout().write(output.as_bytes());
|
||||
let _ = std::io::stdout().flush();
|
||||
}
|
||||
}
|
7
sqlite_core/src/interaction/mod.rs
Normal file
7
sqlite_core/src/interaction/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod buffer;
|
||||
pub mod command_line;
|
||||
|
||||
pub trait Interaction {
|
||||
fn input(&self) -> Result<String, String>;
|
||||
fn output(&self, output: String);
|
||||
}
|
Reference in New Issue
Block a user