Skip to content

Commit 4e72e69

Browse files
arnaucubeed255
andcommitted
add From<MainPod> for OperationArg, add copy op!
Co-authored-by: Eduard S. <eduardsanou@posteo.net>
1 parent a633ef0 commit 4e72e69

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/frontend/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,22 @@ impl MainPod {
645645
pub fn id(&self) -> PodId {
646646
self.pod.id()
647647
}
648+
649+
/// Returns the value of a ValueOf statement with self id that defines key if it exists.
650+
pub fn get(&self, key: impl Into<Key>) -> Option<Value> {
651+
let key: Key = key.into();
652+
self.public_statements
653+
.iter()
654+
.find_map(|st| match st {
655+
Statement::ValueOf(ak, value)
656+
if ak.pod_id == self.id() && ak.key.hash() == key.hash() =>
657+
{
658+
Some(value)
659+
}
660+
_ => None,
661+
})
662+
.cloned()
663+
}
648664
}
649665

650666
struct MainPodCompilerInputs<'a> {
@@ -759,6 +775,9 @@ pub mod build_utils {
759775
(new_entry, ($key:expr, $value:expr)) => { $crate::frontend::Operation(
760776
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::NewEntry),
761777
$crate::op_args!(($key, $value)), $crate::middleware::OperationAux::None) };
778+
(copy, $($arg:expr),+) => { $crate::frontend::Operation(
779+
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::CopyStatement),
780+
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };
762781
(eq, $($arg:expr),+) => { $crate::frontend::Operation(
763782
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::EqualFromEntries),
764783
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };

src/frontend/operation.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use crate::{
4-
frontend::SignedPod,
4+
frontend::{MainPod, SignedPod},
55
middleware::{AnchoredKey, OperationAux, OperationType, Statement, Value},
66
};
77

@@ -78,6 +78,18 @@ impl From<(&SignedPod, &str)> for OperationArg {
7878
))
7979
}
8080
}
81+
impl From<(&MainPod, &str)> for OperationArg {
82+
fn from((pod, key): (&MainPod, &str)) -> Self {
83+
// TODO: TryFrom.
84+
let value = pod
85+
.get(key)
86+
.unwrap_or_else(|| panic!("Key {} is not present in POD: {}", key, pod));
87+
Self::Statement(Statement::ValueOf(
88+
AnchoredKey::from((pod.id(), key)),
89+
value,
90+
))
91+
}
92+
}
8193

8294
impl From<Statement> for OperationArg {
8395
fn from(s: Statement) -> Self {

0 commit comments

Comments
 (0)