Skip to main content

dpp/data_contract/conversion/value/v0/
mod.rs

1use crate::data_contract::serialized_version::DataContractInSerializationFormat;
2use crate::version::PlatformVersion;
3use crate::ProtocolError;
4use platform_value::Value;
5use platform_version::{TryFromPlatformVersioned, TryIntoPlatformVersioned};
6
7/// `platform_value` deserialization for `DataContract` with an explicit
8/// validation flag.
9///
10/// `from_value(value, full_validation, pv)` is the single entry point: pass
11/// `true` on trust boundaries (SDK ingest, fixture loaders, contract
12/// registration) to run full schema validation, and `false` to reconstruct
13/// already-trusted data (e.g. storage reads) without re-validating it.
14///
15/// For *serialization*, `to_value(pv)` threads an explicit platform version.
16/// The serde path (`ValueConvertible::to_object` /
17/// `platform_value::to_value(&data_contract)`) instead reads the
18/// process-global current platform version, which another thread may change
19/// concurrently (e.g. parallel tests building platforms at older protocol
20/// versions) — prefer `to_value(pv)` wherever a `PlatformVersion` is in hand.
21pub trait DataContractValueConversionMethodsV0 {
22    /// Deserialize from a `platform_value::Value`, running full schema
23    /// validation when `full_validation` is `true`.
24    fn from_value(
25        raw_object: Value,
26        full_validation: bool,
27        platform_version: &PlatformVersion,
28    ) -> Result<Self, ProtocolError>
29    where
30        Self: Sized;
31
32    /// Serialize to a `platform_value::Value` at an explicit platform
33    /// version, independent of the process-global current version.
34    ///
35    /// Provided by default for any implementor convertible to
36    /// `DataContractInSerializationFormat` (`DataContract`, `DataContractV0`,
37    /// `DataContractV1`); other implementors of this trait need not provide
38    /// it, keeping the added method source-compatible.
39    fn to_value(&self, platform_version: &PlatformVersion) -> Result<Value, ProtocolError>
40    where
41        Self: Sized,
42        for<'a> DataContractInSerializationFormat:
43            TryFromPlatformVersioned<&'a Self, Error = ProtocolError>,
44    {
45        let format: DataContractInSerializationFormat =
46            self.try_into_platform_versioned(platform_version)?;
47        platform_value::to_value(&format).map_err(ProtocolError::ValueError)
48    }
49}