expose some interfaces for external usage (from introduction-pods) (#256)

* expose some interfaces for external usage (from introduction-pods)

* add From<MainPod> for OperationArg, add copy op!

Co-authored-by: Eduard S. <eduardsanou@posteo.net>

---------

Co-authored-by: Eduard S. <eduardsanou@posteo.net>
This commit is contained in:
arnaucube 2025-06-05 11:37:43 +02:00 committed by GitHub
parent 5d13ac32ce
commit 7bc0bd08d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 54 additions and 22 deletions

View file

@ -645,6 +645,22 @@ impl MainPod {
pub fn id(&self) -> PodId {
self.pod.id()
}
/// Returns the value of a ValueOf statement with self id that defines key if it exists.
pub fn get(&self, key: impl Into<Key>) -> Option<Value> {
let key: Key = key.into();
self.public_statements
.iter()
.find_map(|st| match st {
Statement::ValueOf(ak, value)
if ak.pod_id == self.id() && ak.key.hash() == key.hash() =>
{
Some(value)
}
_ => None,
})
.cloned()
}
}
struct MainPodCompilerInputs<'a> {
@ -759,6 +775,9 @@ pub mod build_utils {
(new_entry, ($key:expr, $value:expr)) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::NewEntry),
$crate::op_args!(($key, $value)), $crate::middleware::OperationAux::None) };
(copy, $($arg:expr),+) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::CopyStatement),
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };
(eq, $($arg:expr),+) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::EqualFromEntries),
$crate::op_args!($($arg),*), $crate::middleware::OperationAux::None) };

View file

@ -1,7 +1,7 @@
use std::fmt;
use crate::{
frontend::SignedPod,
frontend::{MainPod, SignedPod},
middleware::{AnchoredKey, OperationAux, OperationType, Statement, Value},
};
@ -78,6 +78,18 @@ impl From<(&SignedPod, &str)> for OperationArg {
))
}
}
impl From<(&MainPod, &str)> for OperationArg {
fn from((pod, key): (&MainPod, &str)) -> Self {
// TODO: TryFrom.
let value = pod
.get(key)
.unwrap_or_else(|| panic!("Key {} is not present in POD: {}", key, pod));
Self::Statement(Statement::ValueOf(
AnchoredKey::from((pod.id(), key)),
value,
))
}
}
impl From<Statement> for OperationArg {
fn from(s: Statement) -> Self {