56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use crate::server::StoredData::*;
|
|
use crate::server::{Command, NoteContext, StoredData};
|
|
use litemap::LiteMap;
|
|
use std::io::Write;
|
|
use std::string::String;
|
|
|
|
impl NoteContext {
|
|
pub fn execute_command(&mut self, command: Command) {
|
|
match command {
|
|
Command::Unknown => {}
|
|
Command::Set(key, value) => self.set(key, value),
|
|
Command::Define(key, schema) => self.define(key, schema),
|
|
Command::Push(key, value) => self.push(key, value),
|
|
Command::Display(key) => self.display(key),
|
|
Command::SetOutput(generator) => self.output = generator(),
|
|
}
|
|
}
|
|
|
|
fn set(&mut self, key: String, value: StoredData) {
|
|
if key.is_empty() {
|
|
return;
|
|
}
|
|
self.entries.insert(key, value);
|
|
}
|
|
|
|
fn push(&mut self, key: String, value: StoredData) {
|
|
let mut vectorize: Option<StoredData> = None;
|
|
let current = self.entries.get_mut(&key);
|
|
if let Some(current) = current {
|
|
if let Vec(vec) = current {
|
|
vec.push(value);
|
|
} else {
|
|
vectorize = Some(Vec(vec![current.clone()]));
|
|
}
|
|
} else {
|
|
self.set(key, value);
|
|
return;
|
|
}
|
|
|
|
if let Some(vec) = vectorize {
|
|
self.set(key, vec);
|
|
}
|
|
}
|
|
|
|
fn define(&mut self, _key: String, _value: LiteMap<String, StoredData>) {
|
|
todo!()
|
|
}
|
|
|
|
fn display(&mut self, key: String) {
|
|
if let Some(data) = self.entries.get(&key) {
|
|
writeln!(self.output, "{}", key).unwrap();
|
|
_ = self.output.write_all(data.to_string().as_bytes());
|
|
}
|
|
}
|
|
}
|