radish_types/
lib.rs

1/* Copyright (c) 2020 Dmitry Shatilov <shatilov dot diman at gmail dot com>
2 * 
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Affero General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU Affero General Public License for more details.
12
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 */
16
17use 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