dpp/state_transition/traits/
state_transition_value_convert.rs1use crate::state_transition::{state_transition_helpers, StateTransitionFieldTypes};
2use crate::ProtocolError;
3use platform_value::{Value, ValueMapHelper};
4use platform_version::version::PlatformVersion;
5use serde::{Deserialize, Serialize};
6use std::collections::BTreeMap;
7
8pub trait StateTransitionValueConvert<'a>:
10 Serialize + Deserialize<'a> + StateTransitionFieldTypes
11{
12 fn to_object(&self, skip_signature: bool) -> Result<Value, ProtocolError> {
15 let skip_signature_paths = if skip_signature {
16 Self::signature_property_paths()
17 } else {
18 vec![]
19 };
20 state_transition_helpers::to_object(self, skip_signature_paths)
21 }
22
23 fn to_canonical_object(&self, skip_signature: bool) -> Result<Value, ProtocolError> {
26 let skip_signature_paths = if skip_signature {
27 Self::signature_property_paths()
28 } else {
29 vec![]
30 };
31 let mut object = state_transition_helpers::to_object(self, skip_signature_paths)?;
32
33 object.as_map_mut_ref().unwrap().sort_by_keys();
34 Ok(object)
35 }
36
37 fn to_canonical_cleaned_object(&self, skip_signature: bool) -> Result<Value, ProtocolError> {
40 let skip_signature_paths = if skip_signature {
41 Self::signature_property_paths()
42 } else {
43 vec![]
44 };
45 let mut object = state_transition_helpers::to_cleaned_object(self, skip_signature_paths)?;
46
47 object.as_map_mut_ref().unwrap().sort_by_keys();
48 Ok(object)
49 }
50
51 fn to_cleaned_object(&self, skip_signature: bool) -> Result<Value, ProtocolError> {
52 self.to_object(skip_signature)
53 }
54 fn from_object(
55 raw_object: Value,
56 _platform_version: &PlatformVersion,
57 ) -> Result<Self, ProtocolError>
58 where
59 Self: Sized,
60 {
61 platform_value::from_value(raw_object).map_err(ProtocolError::ValueError)
62 }
63
64 fn from_value_map(
65 raw_value_map: BTreeMap<String, Value>,
66 _platform_version: &PlatformVersion,
67 ) -> Result<Self, ProtocolError>
68 where
69 Self: Sized,
70 {
71 platform_value::from_value(Value::Map(
72 raw_value_map
73 .into_iter()
74 .map(|(k, v)| (k.into(), v))
75 .collect(),
76 ))
77 .map_err(ProtocolError::ValueError)
78 }
79 fn clean_value(_value: &mut Value) -> Result<(), ProtocolError> {
80 Ok(())
81 }
82}