dpp/document/serialization_traits/platform_value_conversion/
mod.rs

1mod v0;
2
3pub use v0::*;
4
5use crate::document::{Document, DocumentV0};
6use crate::version::PlatformVersion;
7use crate::ProtocolError;
8use platform_value::Value;
9use std::collections::BTreeMap;
10
11impl DocumentPlatformValueMethodsV0<'_> for Document {
12    /// Convert the document to a map value.
13    fn to_map_value(&self) -> Result<BTreeMap<String, Value>, ProtocolError> {
14        match self {
15            Document::V0(v0) => v0.to_map_value(),
16        }
17    }
18
19    /// Convert the document to a map value consuming the document.
20    fn into_map_value(self) -> Result<BTreeMap<String, Value>, ProtocolError> {
21        match self {
22            Document::V0(v0) => v0.into_map_value(),
23        }
24    }
25
26    /// Convert the document to a value consuming the document.
27    fn into_value(self) -> Result<Value, ProtocolError> {
28        match self {
29            Document::V0(v0) => v0.into_value(),
30        }
31    }
32
33    /// Convert the document to an object.
34    fn to_object(&self) -> Result<Value, ProtocolError> {
35        match self {
36            Document::V0(v0) => v0.to_object(),
37        }
38    }
39
40    /// Create a document from a platform value.
41    fn from_platform_value(
42        document_value: Value,
43        platform_version: &PlatformVersion,
44    ) -> Result<Self, ProtocolError> {
45        match platform_version
46            .dpp
47            .document_versions
48            .document_structure_version
49        {
50            0 => Ok(Document::V0(DocumentV0::from_platform_value(
51                document_value,
52                platform_version,
53            )?)),
54            version => Err(ProtocolError::UnknownVersionError(format!(
55                "version {version} not known for document for call from_platform_value"
56            ))),
57        }
58    }
59}