holochain_integrity_types/
dna_modifiers.rs1use crate::prelude::*;
4use holochain_serialized_bytes::prelude::*;
5
6#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
10#[cfg_attr(feature = "full-dna-def", derive(derive_builder::Builder))]
11pub struct DnaModifiers {
12    pub network_seed: NetworkSeed,
19
20    #[cfg_attr(feature = "full-dna-def", builder(default = "().try_into().unwrap()"))]
22    pub properties: SerializedBytes,
23}
24
25impl DnaModifiers {
26    pub fn update(mut self, modifiers: DnaModifiersOpt) -> DnaModifiers {
29        self.network_seed = modifiers.network_seed.unwrap_or(self.network_seed);
30        self.properties = modifiers.properties.unwrap_or(self.properties);
31        self
32    }
33}
34
35#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
37pub struct DnaModifiersOpt<P = SerializedBytes> {
38    pub network_seed: Option<NetworkSeed>,
40    pub properties: Option<P>,
42}
43
44impl<P: TryInto<SerializedBytes, Error = E>, E: Into<SerializedBytesError>> Default
45    for DnaModifiersOpt<P>
46{
47    fn default() -> Self {
48        Self::none()
49    }
50}
51
52impl<P: TryInto<SerializedBytes, Error = E>, E: Into<SerializedBytesError>> DnaModifiersOpt<P> {
53    pub fn none() -> Self {
55        Self {
56            network_seed: None,
57            properties: None,
58        }
59    }
60
61    pub fn serialized(self) -> Result<DnaModifiersOpt<SerializedBytes>, E> {
63        let Self {
64            network_seed,
65            properties,
66        } = self;
67        let properties = if let Some(p) = properties {
68            Some(p.try_into()?)
69        } else {
70            None
71        };
72        Ok(DnaModifiersOpt {
73            network_seed,
74            properties,
75        })
76    }
77
78    pub fn with_network_seed(mut self, network_seed: NetworkSeed) -> Self {
80        self.network_seed = Some(network_seed);
81        self
82    }
83
84    pub fn with_properties(mut self, properties: P) -> Self {
86        self.properties = Some(properties);
87        self
88    }
89
90    pub fn has_some_option_set(&self) -> bool {
92        self.network_seed.is_some() || self.properties.is_some()
93    }
94}
95
96pub trait TryFromDnaProperties {
98    type Error;
100
101    fn try_from_dna_properties() -> Result<Self, Self::Error>
103    where
104        Self: Sized;
105}