Server code
This commit is contained in:
1 parent
4c2cd7130b
commit
ec73c54276
6 files changed
+213
-47
No files matched your search
+105
-39
@@ -1,29 +1,73 @@
|
||||
use minicbor::decode::Error;
|
||||
mod handling;
|
||||
mod testing;
|
||||
|
||||
use litemap::LiteMap;
|
||||
use minicbor::Decoder;
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use minicbor::data::Type;
|
||||
use minicbor::decode::Error;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter };
|
||||
use std::io::Write;
|
||||
|
||||
enum Command {
|
||||
pub enum Command {
|
||||
Unknown,
|
||||
Set(String, Box<dyn Display>),
|
||||
Push(String, Box<dyn Display>),
|
||||
Set(String, StoredData),
|
||||
Define(String, LiteMap<String, StoredData>),
|
||||
Push(String, StoredData),
|
||||
Display(String),
|
||||
SetOutput(fn() -> Box<dyn Write>),
|
||||
}
|
||||
|
||||
struct Line{
|
||||
data: Vec<Box<dyn Display>>,
|
||||
#[derive(Clone)]
|
||||
pub enum StoredData {
|
||||
Integer(isize),
|
||||
Float(f64),
|
||||
String(String),
|
||||
Bool(bool),
|
||||
Line(Line),
|
||||
Vec(Vec<StoredData>),
|
||||
}
|
||||
|
||||
impl From<Vec<Box<dyn Display>>> for Line {
|
||||
fn from(value: Vec<Box<dyn Display>>) -> Self {
|
||||
Self{data: value}
|
||||
impl Display for StoredData {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
StoredData::Integer(v) => v.fmt(f),
|
||||
StoredData::Float(v) => v.fmt(f),
|
||||
StoredData::String(v) => v.fmt(f),
|
||||
StoredData::Bool(v) => v.fmt(f),
|
||||
StoredData::Line(v) => v.fmt(f),
|
||||
StoredData::Vec(vec) => {
|
||||
vec.iter().for_each(|d| {
|
||||
_ = write!(f, "{}\n", d);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Line {
|
||||
data: Vec<StoredData>,
|
||||
}
|
||||
|
||||
impl From<Vec<StoredData>> for Line {
|
||||
fn from(value: Vec<StoredData>) -> Self {
|
||||
Self { data: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Line {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "[{}]", self.data.iter().map(|i| i.to_string()).collect::<Vec<String>>().join(", "))
|
||||
write!(
|
||||
f,
|
||||
"[{}]",
|
||||
self.data
|
||||
.iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +84,7 @@ fn parse_command(data: &[u8]) -> Result<Command, Error> {
|
||||
decoder.array()?;
|
||||
let id = decoder.str()?.to_string();
|
||||
let value = decode_value(&mut decoder)?;
|
||||
return Ok(Command::Set(id, value))
|
||||
return Ok(Command::Set(id, value));
|
||||
}
|
||||
if data.starts_with(b"PUSH:") {
|
||||
let payload = &data[b"PUSH:".len()..];
|
||||
@@ -48,32 +92,41 @@ fn parse_command(data: &[u8]) -> Result<Command, Error> {
|
||||
decoder.array()?;
|
||||
let id = decoder.str()?.to_string();
|
||||
let value = decode_value(&mut decoder)?;
|
||||
return Ok(Command::Push(id, value))
|
||||
return Ok(Command::Push(id, value));
|
||||
}
|
||||
if data.starts_with(b"DISPLAY:") {
|
||||
let payload = &data[b"DISPLAY:".len()..];
|
||||
let mut decoder = Decoder::new(&payload);
|
||||
decoder.array()?;
|
||||
let id = decoder.str()?.to_string();
|
||||
return Ok(Command::Display(id));
|
||||
}
|
||||
Ok(Command::Unknown)
|
||||
}
|
||||
|
||||
fn decode_value(decoder: &mut Decoder) -> Result<Box<dyn Display>, Error> {
|
||||
fn decode_value(decoder: &mut Decoder) -> Result<StoredData, Error> {
|
||||
Ok(match decoder.datatype()? {
|
||||
Type::Bool => Box::new(decoder.bool()?),
|
||||
Type::U8 => Box::new(decoder.u8()? as isize),
|
||||
Type::U16 => Box::new(decoder.u16()? as isize),
|
||||
Type::U32 => Box::new(decoder.u32()? as isize),
|
||||
Type::U64 => Box::new(decoder.u64()? as isize),
|
||||
Type::I8 => Box::new(decoder.i8()? as isize),
|
||||
Type::I16 => Box::new(decoder.i16()? as isize),
|
||||
Type::I32 => Box::new(decoder.i32()? as isize),
|
||||
Type::I64 => Box::new(decoder.i64()? as isize),
|
||||
Type::Int => Box::new(decoder.i32()? as isize),
|
||||
Type::F16 => Box::new(decoder.f32()? as f64),
|
||||
Type::F32 => Box::new(decoder.f32()? as f64),
|
||||
Type::F64 => Box::new(decoder.f64()?),
|
||||
Type::String |
|
||||
Type::StringIndef => Box::new(decoder.str()?.to_string()),
|
||||
Type::Array |
|
||||
Type::ArrayIndef => {
|
||||
Type::Bool => StoredData::Bool(decoder.bool()?),
|
||||
Type::U8 => StoredData::Integer(decoder.u8()? as isize),
|
||||
Type::U16 => StoredData::Integer(decoder.u16()? as isize),
|
||||
Type::U32 => StoredData::Integer(decoder.u32()? as isize),
|
||||
Type::U64 => StoredData::Integer(decoder.u64()? as isize),
|
||||
Type::I8 => StoredData::Integer(decoder.i8()? as isize),
|
||||
Type::I16 => StoredData::Integer(decoder.i16()? as isize),
|
||||
Type::I32 => StoredData::Integer(decoder.i32()? as isize),
|
||||
Type::I64 => StoredData::Integer(decoder.i64()? as isize),
|
||||
Type::Int => StoredData::Integer(decoder.i32()? as isize),
|
||||
Type::F16 => StoredData::Float(decoder.f32()? as f64),
|
||||
Type::F32 => StoredData::Float(decoder.f32()? as f64),
|
||||
Type::F64 => StoredData::Float(decoder.f64()?),
|
||||
Type::String | Type::StringIndef => StoredData::String(decoder.str()?.to_string()),
|
||||
Type::Array | Type::ArrayIndef => {
|
||||
let len = decoder.array()?;
|
||||
let mut array = if let Some(len) = len { Vec::with_capacity(len as usize) } else { vec![] };
|
||||
let mut array = if let Some(len) = len {
|
||||
Vec::with_capacity(len as usize)
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
if array.is_empty() {
|
||||
loop {
|
||||
let dt = decoder.datatype()?;
|
||||
@@ -83,12 +136,25 @@ fn decode_value(decoder: &mut Decoder) -> Result<Box<dyn Display>, Error> {
|
||||
array.push(decode_value(decoder)?);
|
||||
}
|
||||
}
|
||||
Box::new(Line::from(array))
|
||||
StoredData::Line(Line::from(array))
|
||||
}
|
||||
_ => {
|
||||
decoder.skip()?;
|
||||
StoredData::Bool(false)
|
||||
}
|
||||
_ => { decoder.skip()?; Box::new(false)}
|
||||
})
|
||||
}
|
||||
|
||||
struct NoteContext {
|
||||
entries: HashMap<String, Box<dyn Any>>,
|
||||
}
|
||||
pub struct NoteContext {
|
||||
entries: HashMap<String, StoredData>,
|
||||
output: Box<dyn Write>,
|
||||
}
|
||||
|
||||
impl NoteContext {
|
||||
pub fn default() -> Self {
|
||||
Self{
|
||||
entries: Default::default(),
|
||||
output: Box::new(std::io::stdout()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user