1use serde::{Deserialize, Serialize};
18use std::collections::VecDeque;
19
20pub type Key = Vec<u8>;
21pub type Arguments = VecDeque<Value>;
22pub type ExecResult = Result<Value, String>;
23
24#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
25pub enum Value {
26 Nill,
27 Ok,
28 Bool(bool),
29 Integer(i64),
30 Float(u64),
31 Buffer(Vec<u8>),
32 Array(VecDeque<Value>),
33 Error(String),
34}
35
36
37#[derive(Serialize, Deserialize, Debug, PartialEq)]
38pub struct Command {
39 pub command: String,
40 pub arguments: Arguments,
41}
42
43#[derive(Serialize, Deserialize, Debug, PartialEq)]
44pub struct CommandResult {
45 pub results: Value,
46}
47
48impl std::fmt::Display for Value {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 match self {
51 Value::Nill => write!(f, "nill"),
52 Value::Ok => write!(f, "ok"),
53 Value::Bool(false) => write!(f, "false"),
54 Value::Bool(true) => write!(f, "true"),
55 Value::Integer(i) => write!(f, "{}", i),
56 Value::Float(i) => write!(f, "{}", f64::from_bits(*i)),
57 Value::Buffer(v) => write!(f, "{:?}", v),
58 Value::Array(v) => write!(f, "{:?}", v),
59 Value::Error(m) => write!(f, "{}", m),
60 }
61 }
62}
63
64impl std::fmt::Display for Command {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 let args =
67 self
68 .arguments
69 .iter()
70 .map(|arg|format!("{}", arg))
71 .collect::<Vec<String>>()
72 .join(",")
73 ;
74 write!(f, "{}: [{}]", self.command, args)
75 }
76}
77