daml_grpc/
serialize.rs

1use crate::data::value::DamlValue;
2use crate::data::DamlResult;
3use crate::nat::Nat;
4use crate::primitive_types::{
5    DamlBool, DamlContractId, DamlDate, DamlFixedNumeric, DamlGenMap, DamlInt64, DamlParty, DamlText, DamlTextMap,
6    DamlTimestamp, DamlUnit,
7};
8
9/// Marker trait for types which can be serialized to a [`DamlValue`].
10pub trait DamlSerializableType: Sized {}
11
12impl DamlSerializableType for DamlUnit {}
13impl DamlSerializableType for DamlBool {}
14impl DamlSerializableType for DamlInt64 {}
15impl DamlSerializableType for DamlText {}
16impl DamlSerializableType for DamlParty {}
17impl DamlSerializableType for DamlContractId {}
18impl DamlSerializableType for DamlTimestamp {}
19impl DamlSerializableType for DamlDate {}
20impl<T> DamlSerializableType for DamlFixedNumeric<T> where T: DamlSerializableType + Nat {}
21impl<T> DamlSerializableType for Box<T> where T: DamlSerializeInto<DamlValue> + DamlSerializableType {}
22impl<T> DamlSerializableType for Option<T> where T: DamlSerializeInto<DamlValue> + DamlSerializableType {}
23impl<T> DamlSerializableType for Vec<T> where T: DamlSerializeInto<DamlValue> + DamlSerializableType {}
24impl<K, V> DamlSerializableType for DamlGenMap<K, V>
25where
26    K: DamlSerializeInto<DamlValue> + DamlSerializableType,
27    V: DamlSerializeInto<DamlValue> + DamlSerializableType,
28{
29}
30impl<V> DamlSerializableType for DamlTextMap<V> where V: DamlSerializeInto<DamlValue> + DamlSerializableType {}
31
32/// Serialize from a concrete [`DamlSerializableType`] to a [`DamlValue`].
33pub trait DamlSerializeFrom<T>: Sized
34where
35    T: DamlSerializableType,
36{
37    fn serialize_from(_: T) -> Self;
38}
39
40/// Serialize a concrete [`DamlSerializableType`] type into a [`DamlValue`].
41pub trait DamlSerializeInto<T = DamlValue>: DamlSerializableType {
42    fn serialize_into(self) -> T;
43}
44
45/// Blanket impl for all concrete [`DamlSerializableType`] types which implement [`DamlSerializeFrom`].
46impl<T, U> DamlSerializeInto<U> for T
47where
48    T: DamlSerializableType,
49    U: DamlSerializeFrom<T>,
50{
51    fn serialize_into(self) -> U {
52        U::serialize_from(self)
53    }
54}
55
56/// Deserialize from a [`DamlValue`] to a concrete [`DamlDeserializableType`] type.
57pub trait DamlDeserializeFrom: DamlDeserializableType {
58    fn deserialize_from(value: DamlValue) -> DamlResult<Self>;
59}
60
61/// Deserialize a [`DamlValue`] into a concrete [`DamlDeserializableType`] type.
62pub trait DamlDeserializeInto<T: DamlDeserializableType> {
63    fn deserialize_into(self) -> DamlResult<T>;
64}
65
66/// Blanket [`DamlDeserializeInto`] impl for all types which implement [`DamlDeserializeFrom`].
67impl<T> DamlDeserializeInto<T> for DamlValue
68where
69    T: DamlDeserializeFrom,
70{
71    fn deserialize_into(self) -> DamlResult<T> {
72        T::deserialize_from(self)
73    }
74}
75
76/// Marker trait for types which can be converted from a [`DamlValue`].
77pub trait DamlDeserializableType: Sized {}
78
79impl DamlDeserializableType for DamlUnit {}
80impl DamlDeserializableType for DamlBool {}
81impl DamlDeserializableType for DamlInt64 {}
82impl DamlDeserializableType for DamlText {}
83impl DamlDeserializableType for DamlParty {}
84impl DamlDeserializableType for DamlContractId {}
85impl DamlDeserializableType for DamlTimestamp {}
86impl DamlDeserializableType for DamlDate {}
87impl<T> DamlDeserializableType for DamlFixedNumeric<T> where T: DamlDeserializableType + Nat {}
88impl<T> DamlDeserializableType for Box<T> where T: DamlDeserializeFrom + DamlDeserializableType {}
89impl<T> DamlDeserializableType for Option<T> where T: DamlDeserializeFrom + DamlDeserializableType {}
90impl<T> DamlDeserializableType for Vec<T> where T: DamlDeserializeFrom + DamlDeserializableType {}
91impl<K, V> DamlDeserializableType for DamlGenMap<K, V>
92where
93    K: DamlDeserializeFrom + DamlDeserializableType,
94    V: DamlDeserializeFrom + DamlDeserializableType,
95{
96}
97impl<V> DamlDeserializableType for DamlTextMap<V> where V: DamlDeserializeFrom + DamlDeserializableType {}