dpp/state_transition/
abstract_state_transition.rs

1use serde::Serialize;
2#[cfg(feature = "json-conversion")]
3use serde_json::Value as JsonValue;
4
5pub mod state_transition_helpers {
6    use super::*;
7    use crate::ProtocolError;
8    use platform_value::Value;
9    #[cfg(feature = "json-conversion")]
10    use std::convert::TryInto;
11
12    #[cfg(feature = "json-conversion")]
13    pub fn to_json<'a, I: IntoIterator<Item = &'a str>>(
14        serializable: impl Serialize,
15        skip_signature_paths: I,
16    ) -> Result<JsonValue, ProtocolError> {
17        to_object(serializable, skip_signature_paths)
18            .and_then(|v| v.try_into().map_err(ProtocolError::ValueError))
19    }
20
21    pub fn to_object<'a, I: IntoIterator<Item = &'a str>>(
22        serializable: impl Serialize,
23        skip_signature_paths: I,
24    ) -> Result<Value, ProtocolError> {
25        let mut value: Value = platform_value::to_value(serializable)?;
26        skip_signature_paths.into_iter().try_for_each(|path| {
27            value
28                .remove_values_matching_path(path)
29                .map_err(ProtocolError::ValueError)
30                .map(|_| ())
31        })?;
32        Ok(value)
33    }
34
35    pub fn to_cleaned_object<'a, I: IntoIterator<Item = &'a str>>(
36        serializable: impl Serialize,
37        skip_signature_paths: I,
38    ) -> Result<Value, ProtocolError> {
39        let mut value: Value = platform_value::to_value(serializable)?;
40
41        value = value.clean_recursive()?;
42
43        skip_signature_paths.into_iter().try_for_each(|path| {
44            value
45                .remove_values_matching_path(path)
46                .map_err(ProtocolError::ValueError)
47                .map(|_| ())
48        })?;
49        Ok(value)
50    }
51}