move pod data's deserialization to RecursivePod trait (#294), expose get_common_data (#300)

This commit is contained in:
arnaucube 2025-06-18 11:13:29 +02:00 committed by GitHub
parent 6ab0bc52fc
commit 0541817116
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 87 additions and 81 deletions

View file

@ -796,6 +796,7 @@ pub trait Pod: fmt::Debug + DynClone + Any {
/// Return this Pods data serialized into a json value. This serialization can skip `params,
/// id, vds_root`
fn serialize_data(&self) -> serde_json::Value;
/// Extract key-values from ValueOf public statements
fn kvs(&self) -> HashMap<AnchoredKey, Value> {
self.pub_statements()
@ -830,6 +831,16 @@ pub trait RecursivePod: Pod {
fn verifier_data(&self) -> VerifierOnlyCircuitData;
fn proof(&self) -> Proof;
fn vds_root(&self) -> Hash;
/// Returns the deserialized RecursivePod.
fn deserialize_data(
params: Params,
data: serde_json::Value,
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>>
where
Self: Sized;
}
// impl Clone for Box<dyn RecursivePod>

View file

@ -3,16 +3,14 @@ use std::{
sync::{LazyLock, Mutex},
};
use crate::middleware::{
BackendResult, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result,
};
use crate::middleware::{DynError, Error, Hash, Params, Pod, PodId, PodType, RecursivePod, Result};
type DeserializeFn = fn(
params: Params,
id: PodId,
vds_root: Hash,
data: serde_json::Value,
) -> BackendResult<Box<dyn RecursivePod>>;
vds_root: Hash,
id: PodId,
) -> Result<Box<dyn RecursivePod>, Box<DynError>>;
static DESERIALIZERS: LazyLock<Mutex<HashMap<usize, DeserializeFn>>> =
LazyLock::new(backend::deserializers_default);
@ -41,7 +39,7 @@ pub fn deserialize_pod(
pod_type
)))?;
deserialize_fn(params, id, vds_root, data)
deserialize_fn(params, data, vds_root, id)
.map_err(|e| Error::custom(format!("deserialize error: {:?}", e)))
}
@ -65,10 +63,10 @@ mod backend {
pub(super) fn deserializers_default() -> Mutex<HashMap<usize, DeserializeFn>> {
let mut map: HashMap<usize, DeserializeFn> = HashMap::new();
map.insert(PodType::Empty as usize, EmptyPod::deserialize);
map.insert(PodType::Main as usize, MainPod::deserialize);
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize);
map.insert(PodType::MockMain as usize, MockMainPod::deserialize);
map.insert(PodType::Empty as usize, EmptyPod::deserialize_data);
map.insert(PodType::Main as usize, MainPod::deserialize_data);
map.insert(PodType::MockEmpty as usize, MockEmptyPod::deserialize_data);
map.insert(PodType::MockMain as usize, MockMainPod::deserialize_data);
Mutex::new(map)
}