1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
pub use holochain_integrity_types::zome::*;
use holochain_serialized_bytes::prelude::*;
pub mod error;
#[cfg(feature = "full-dna-def")]
pub mod inline_zome;
use error::ZomeResult;
#[cfg(feature = "full-dna-def")]
use self::inline_zome::InlineZome;
#[cfg(feature = "full-dna-def")]
use error::ZomeError;
#[cfg(feature = "full-dna-def")]
use std::sync::Arc;
#[derive(Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "full-dna-def", derive(shrinkwraprs::Shrinkwrap))]
#[cfg_attr(feature = "test_utils", derive(arbitrary::Arbitrary))]
pub struct Zome {
name: ZomeName,
#[cfg_attr(feature = "full-dna-def", shrinkwrap(main_field))]
def: ZomeDef,
}
impl Zome {
pub fn new(name: ZomeName, def: ZomeDef) -> Self {
Self { name, def }
}
pub fn zome_name(&self) -> &ZomeName {
&self.name
}
pub fn zome_def(&self) -> &ZomeDef {
&self.def
}
pub fn into_inner(self) -> (ZomeName, ZomeDef) {
(self.name, self.def)
}
}
impl From<(ZomeName, ZomeDef)> for Zome {
fn from(pair: (ZomeName, ZomeDef)) -> Self {
Self::new(pair.0, pair.1)
}
}
impl From<Zome> for (ZomeName, ZomeDef) {
fn from(zome: Zome) -> Self {
zome.into_inner()
}
}
impl From<Zome> for ZomeName {
fn from(zome: Zome) -> Self {
zome.name
}
}
impl From<Zome> for ZomeDef {
fn from(zome: Zome) -> Self {
zome.def
}
}
#[derive(Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[serde(untagged, into = "ZomeDefSerialized")]
pub enum ZomeDef {
Wasm(WasmZome),
#[serde(skip_deserializing)]
#[cfg(feature = "full-dna-def")]
Inline(Arc<InlineZome>),
}
#[derive(Serialize)]
#[serde(untagged)]
enum ZomeDefSerialized {
Wasm(WasmZome),
#[cfg(feature = "full-dna-def")]
InlineUid(String),
}
impl From<ZomeDef> for ZomeDefSerialized {
fn from(d: ZomeDef) -> Self {
match d {
ZomeDef::Wasm(zome) => Self::Wasm(zome),
#[cfg(feature = "full-dna-def")]
ZomeDef::Inline(zome) => Self::InlineUid(zome.uuid.clone()),
}
}
}
#[cfg(feature = "full-dna-def")]
impl From<InlineZome> for ZomeDef {
fn from(iz: InlineZome) -> Self {
Self::Inline(Arc::new(iz))
}
}
impl ZomeDef {
pub fn wasm_hash(&self, _zome_name: &ZomeName) -> ZomeResult<holo_hash::WasmHash> {
match self {
ZomeDef::Wasm(WasmZome { wasm_hash }) => Ok(wasm_hash.clone()),
#[cfg(feature = "full-dna-def")]
ZomeDef::Inline(_) => Err(ZomeError::NonWasmZome(_zome_name.clone())),
}
}
}
#[cfg(feature = "test_utils")]
impl<'a> arbitrary::Arbitrary<'a> for ZomeDef {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self::Wasm(WasmZome::arbitrary(u)?))
}
}
#[derive(
Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, SerializedBytes,
)]
#[cfg_attr(feature = "test_utils", derive(arbitrary::Arbitrary))]
pub struct WasmZome {
pub wasm_hash: holo_hash::WasmHash,
}
impl WasmZome {
pub fn new(wasm_hash: holo_hash::WasmHash) -> Self {
Self { wasm_hash }
}
}
impl ZomeDef {
pub fn from_hash(wasm_hash: holo_hash::WasmHash) -> Self {
Self::Wasm(WasmZome { wasm_hash })
}
}