chore: enums for statement and op types (#69)
* Experiment with statement & op enums * Clean-up & fixes * More clean-up * Add argument length checks * More clean-up * Place statement and operation logic in submodules
This commit is contained in:
parent
83a4f8969f
commit
c2d23b0b1b
11 changed files with 891 additions and 606 deletions
82
src/frontend/operation.rs
Normal file
82
src/frontend/operation.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
use std::fmt;
|
||||
|
||||
use crate::middleware::{hash_str, NativeOperation, NativeStatement};
|
||||
|
||||
use super::{AnchoredKey, SignedPod, Statement, StatementArg, Value};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum OperationArg {
|
||||
Statement(Statement),
|
||||
Literal(Value),
|
||||
Entry(String, Value),
|
||||
}
|
||||
|
||||
impl fmt::Display for OperationArg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
OperationArg::Statement(s) => write!(f, "{}", s),
|
||||
OperationArg::Literal(v) => write!(f, "{}", v),
|
||||
OperationArg::Entry(k, v) => write!(f, "({}, {})", k, v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Value> for OperationArg {
|
||||
fn from(v: Value) -> Self {
|
||||
Self::Literal(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Value> for OperationArg {
|
||||
fn from(v: &Value) -> Self {
|
||||
Self::Literal(v.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for OperationArg {
|
||||
fn from(s: &str) -> Self {
|
||||
Self::Literal(Value::from(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for OperationArg {
|
||||
fn from(v: i64) -> Self {
|
||||
Self::Literal(Value::from(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for OperationArg {
|
||||
fn from(b: bool) -> Self {
|
||||
Self::Literal(Value::from(b))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&SignedPod, &str)> for OperationArg {
|
||||
fn from((pod, key): (&SignedPod, &str)) -> Self {
|
||||
// TODO: Actual value, TryFrom.
|
||||
let value = pod.kvs().get(&hash_str(key)).unwrap().clone();
|
||||
Self::Statement(Statement(
|
||||
NativeStatement::ValueOf,
|
||||
vec![
|
||||
StatementArg::Key(AnchoredKey(pod.origin(), key.to_string())),
|
||||
StatementArg::Literal(Value::Raw(value)),
|
||||
],
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Operation(pub NativeOperation, pub Vec<OperationArg>);
|
||||
|
||||
impl fmt::Display for Operation {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?} ", self.0)?;
|
||||
for (i, arg) in self.1.iter().enumerate() {
|
||||
if i != 0 {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
write!(f, "{}", arg)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue