Refactor frontend/middleware types (#194)

* unify fe/be NativeOp and NativePred

* remove Origin in favour of PodId

* Combine string and hash in Key

* use middleware::AnchoredKey in frontend

* merge frontend/middleware types

* refactor custom predicates

* clean up a bit

* fix middleware custom tests

* clean up

* clean up 2

* add acronyms in typos list
This commit is contained in:
Eduard S. 2025-04-16 11:59:30 +02:00 committed by GitHub
parent 9e860ef262
commit c232c8dae5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 1985 additions and 2800 deletions

View file

@ -1,13 +1,12 @@
use std::fmt;
use serde::{Deserialize, Serialize};
// use serde::{Deserialize, Serialize};
use crate::{
frontend::{CustomPredicateRef, NativePredicate, Predicate, SignedPod, Statement, Value},
middleware::{self, OperationAux},
frontend::SignedPod,
middleware::{AnchoredKey, OperationAux, OperationType, Statement, Value},
};
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub enum OperationArg {
Statement(Statement),
Literal(Value),
@ -56,7 +55,16 @@ impl From<bool> for OperationArg {
impl From<(&SignedPod, &str)> for OperationArg {
fn from((pod, key): (&SignedPod, &str)) -> Self {
Self::Statement((pod, key).into())
// TODO: TryFrom.
let value = pod
.kvs()
.get(&key.into())
.cloned()
.unwrap_or_else(|| panic!("Key {} is not present in POD: {}", key, pod));
Self::Statement(Statement::ValueOf(
AnchoredKey::from((pod.id(), key)),
value,
))
}
}
@ -72,120 +80,7 @@ impl<V: Into<Value>> From<(&str, V)> for OperationArg {
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum OperationType {
Native(NativeOperation),
Custom(CustomPredicateRef),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum NativeOperation {
None = 0,
NewEntry = 1,
CopyStatement = 2,
EqualFromEntries = 3,
NotEqualFromEntries = 4,
GtFromEntries = 5,
LtFromEntries = 6,
TransitiveEqualFromStatements = 7,
GtToNotEqual = 8,
LtToNotEqual = 9,
SumOf = 13,
ProductOf = 14,
MaxOf = 15,
DictContainsFromEntries = 16,
DictNotContainsFromEntries = 17,
SetContainsFromEntries = 18,
SetNotContainsFromEntries = 19,
ArrayContainsFromEntries = 20,
}
impl TryFrom<OperationType> for middleware::OperationType {
type Error = anyhow::Error;
fn try_from(fe_ot: OperationType) -> Result<Self, Self::Error> {
type FeOT = OperationType;
type FeNO = NativeOperation;
type MwOT = middleware::OperationType;
type MwNO = middleware::NativeOperation;
let mw_ot = match fe_ot {
FeOT::Native(FeNO::None) => MwOT::Native(MwNO::None),
FeOT::Native(FeNO::NewEntry) => MwOT::Native(MwNO::NewEntry),
FeOT::Native(FeNO::CopyStatement) => MwOT::Native(MwNO::CopyStatement),
FeOT::Native(FeNO::EqualFromEntries) => MwOT::Native(MwNO::EqualFromEntries),
FeOT::Native(FeNO::NotEqualFromEntries) => MwOT::Native(MwNO::NotEqualFromEntries),
FeOT::Native(FeNO::GtFromEntries) => MwOT::Native(MwNO::GtFromEntries),
FeOT::Native(FeNO::LtFromEntries) => MwOT::Native(MwNO::LtFromEntries),
FeOT::Native(FeNO::TransitiveEqualFromStatements) => {
MwOT::Native(MwNO::TransitiveEqualFromStatements)
}
FeOT::Native(FeNO::GtToNotEqual) => MwOT::Native(MwNO::GtToNotEqual),
FeOT::Native(FeNO::LtToNotEqual) => MwOT::Native(MwNO::LtToNotEqual),
FeOT::Native(FeNO::SumOf) => MwOT::Native(MwNO::SumOf),
FeOT::Native(FeNO::ProductOf) => MwOT::Native(MwNO::ProductOf),
FeOT::Native(FeNO::MaxOf) => MwOT::Native(MwNO::MaxOf),
FeOT::Native(FeNO::DictContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Native(FeNO::DictNotContainsFromEntries) => {
MwOT::Native(MwNO::NotContainsFromEntries)
}
FeOT::Native(FeNO::SetContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Native(FeNO::SetNotContainsFromEntries) => {
MwOT::Native(MwNO::NotContainsFromEntries)
}
FeOT::Native(FeNO::ArrayContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Custom(mw_cpr) => MwOT::Custom(mw_cpr.into()),
};
Ok(mw_ot)
}
}
impl OperationType {
/// Gives the type of predicate that the operation will output, if known.
/// CopyStatement may output any predicate (it will match the statement copied),
/// so output_predicate returns None on CopyStatement.
pub fn output_predicate(&self) -> Option<Predicate> {
match self {
OperationType::Native(native_op) => match native_op {
NativeOperation::None => Some(Predicate::Native(NativePredicate::None)),
NativeOperation::NewEntry => Some(Predicate::Native(NativePredicate::ValueOf)),
NativeOperation::CopyStatement => None,
NativeOperation::EqualFromEntries => {
Some(Predicate::Native(NativePredicate::Equal))
}
NativeOperation::NotEqualFromEntries => {
Some(Predicate::Native(NativePredicate::NotEqual))
}
NativeOperation::GtFromEntries => Some(Predicate::Native(NativePredicate::Gt)),
NativeOperation::LtFromEntries => Some(Predicate::Native(NativePredicate::Lt)),
NativeOperation::TransitiveEqualFromStatements => {
Some(Predicate::Native(NativePredicate::Equal))
}
NativeOperation::GtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
NativeOperation::LtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
NativeOperation::SumOf => Some(Predicate::Native(NativePredicate::SumOf)),
NativeOperation::ProductOf => Some(Predicate::Native(NativePredicate::ProductOf)),
NativeOperation::MaxOf => Some(Predicate::Native(NativePredicate::MaxOf)),
NativeOperation::DictContainsFromEntries => {
Some(Predicate::Native(NativePredicate::DictContains))
}
NativeOperation::DictNotContainsFromEntries => {
Some(Predicate::Native(NativePredicate::DictNotContains))
}
NativeOperation::SetContainsFromEntries => {
Some(Predicate::Native(NativePredicate::SetContains))
}
NativeOperation::SetNotContainsFromEntries => {
Some(Predicate::Native(NativePredicate::SetNotContains))
}
NativeOperation::ArrayContainsFromEntries => {
Some(Predicate::Native(NativePredicate::ArrayContains))
}
},
OperationType::Custom(cpr) => Some(Predicate::Custom(cpr.clone())),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>, pub OperationAux);
impl fmt::Display for Operation {