pod2/src/frontend/operation.rs
2025-03-05 21:02:28 +10:00

73 lines
1.7 KiB
Rust

use std::fmt;
use super::{AnchoredKey, SignedPod, Statement, StatementArg, Value};
use crate::middleware::{hash_str, NativePredicate, OperationType, Predicate};
#[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 {
Self::Statement((pod, key).into())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Operation(pub OperationType, 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(())
}
}