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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Core types in the Leaf specification

use borsh::{BorshDeserialize, BorshSerialize};

mod borsh_schema;
mod digest;

pub use borsh_schema::*;
pub use digest::*;

pub type NamespaceId = [u8; 32];
pub type SubspaceId = [u8; 32];
pub type SubspaceSecretKey = [u8; 32];
pub type NamespaceSecretKey = [u8; 32];

/// An entity path.
#[derive(
    borsh::BorshDeserialize,
    borsh::BorshSerialize,
    Debug,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
)]
pub struct EntityPath(pub Vec<PathSegment>);
impl AsRef<[PathSegment]> for EntityPath {
    fn as_ref(&self) -> &[PathSegment] {
        &self.0
    }
}
impl From<()> for EntityPath {
    fn from(_: ()) -> Self {
        EntityPath(Vec::default())
    }
}
impl<T: Into<PathSegment>, const N: usize> From<[T; N]> for EntityPath {
    fn from(value: [T; N]) -> Self {
        EntityPath(value.into_iter().map(|x| x.into()).collect())
    }
}

#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone, Default)]
pub struct Entity {
    pub components: Vec<ComponentEntry>,
}

impl Entity {
    pub fn sort_components(&mut self) {
        self.components.sort();
    }

    pub fn compute_digest(&self) -> Digest {
        let is_sorted = self.components.windows(2).all(|w| w[0] <= w[1]);
        assert!(is_sorted, "Components must be sorted to compute digest");
        let mut buf = Vec::new();
        self.serialize(&mut buf).unwrap();
        Digest::new(&buf)
    }
}

#[derive(
    borsh::BorshDeserialize,
    borsh::BorshSerialize,
    Debug,
    Clone,
    Copy,
    PartialEq,
    PartialOrd,
    Ord,
    Eq,
)]
pub struct ComponentEntry {
    // The schema ID may not be set if the component is encrypted.
    pub schema_id: Option<Digest>,
    pub component_id: Digest,
}

/// A segment in an entity Path.
#[derive(
    borsh::BorshDeserialize, borsh::BorshSerialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub enum PathSegment {
    Null,
    Bool(bool),
    Uint(u64),
    Int(i64),
    String(String),
    Bytes(Vec<u8>),
}
impl std::fmt::Debug for PathSegment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Null => write!(f, "Null"),
            Self::Bool(arg0) => write!(f, "{arg0}"),
            Self::Uint(arg0) => write!(f, "{arg0}_u64"),
            Self::Int(arg0) => write!(f, "{arg0}_i64"),
            Self::String(arg0) => write!(f, "\"{arg0}\""),
            Self::Bytes(arg0) => write!(f, "base32:{}", &iroh_base::base32::fmt(arg0)),
        }
    }
}
impl From<()> for PathSegment {
    fn from(_: ()) -> Self {
        Self::Null
    }
}
impl From<bool> for PathSegment {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}
impl From<u64> for PathSegment {
    fn from(value: u64) -> Self {
        Self::Uint(value)
    }
}
impl From<i64> for PathSegment {
    fn from(value: i64) -> Self {
        Self::Int(value)
    }
}
impl<'a> From<&'a str> for PathSegment {
    fn from(value: &'a str) -> Self {
        Self::String(value.into())
    }
}
impl From<String> for PathSegment {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}
impl From<Vec<u8>> for PathSegment {
    fn from(value: Vec<u8>) -> Self {
        Self::Bytes(value)
    }
}

/// The kind of component data.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub enum ComponentKind {
    /// Unencrypted data
    Unencrypted(ComponentData),
    /// Encrypted data with an associated key ID and encryption algorithm.
    Encrypted {
        algorithm: EncryptionAlgorithm,
        key_id: [u8; 32],
        encrypted_data: Vec<u8>,
    },
}

impl ComponentKind {
    pub fn unencrypted(&self) -> Option<&ComponentData> {
        match self {
            ComponentKind::Unencrypted(u) => Some(u),
            _ => None,
        }
    }
}

/// The data that makes up a component.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub struct ComponentData {
    /// The Schema ID of the component data.
    pub schema: Digest,
    /// The data of the component, to be parsed according to it's [`Schema`]'s [`BorshSchema`].
    pub data: Vec<u8>,
}

/// A [`Component`][crate::Component] schema.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub struct Schema {
    /// The name of the schema.
    pub name: String,
    /// The [`BorshSchema`] describing the component's data.
    pub format: BorshSchema,
    /// The collection ID containing the components that document this schema.
    pub specification: Digest,
}

/// A [`borsh`] schema describing the data format of a [`Component`][crate::Component].
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub enum BorshSchema {
    Null,
    Bool,
    U8,
    U16,
    U32,
    U64,
    U128,
    I8,
    I16,
    I32,
    I64,
    I128,
    F32,
    F64,
    String,
    Option {
        schema: Box<BorshSchema>,
    },
    Array {
        schema: Box<BorshSchema>,
        len: u32,
    },
    Struct {
        fields: Vec<(String, BorshSchema)>,
    },
    Enum {
        variants: Vec<(String, BorshSchema)>,
    },
    Vector {
        schema: Box<BorshSchema>,
    },
    Map {
        key: Box<BorshSchema>,
        value: Box<BorshSchema>,
    },
    Set {
        schema: Box<BorshSchema>,
    },
    Blob,
    Snapshot,
    Link,
}

#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub struct Link {
    namespace: KeyResolverKind,
    subspace: KeyResolverKind,
    path: Vec<PathSegment>,
    snapshot: Option<Digest>,
}
impl HasBorshSchema for Link {
    fn borsh_schema() -> BorshSchema {
        BorshSchema::Link
    }
}

/// Similar to a [`types::Link`], but with a resolved namespace and subspace.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, BorshDeserialize, BorshSerialize)]
pub struct ExactLink {
    pub namespace: NamespaceId,
    pub subspace: SubspaceId,
    pub path: EntityPath,
}

impl<P: Into<EntityPath>> From<(NamespaceId, SubspaceId, P)> for ExactLink {
    fn from((n, s, p): (NamespaceId, SubspaceId, P)) -> Self {
        Self {
            namespace: n,
            subspace: s,
            path: p.into(),
        }
    }
}

#[derive(
    borsh::BorshDeserialize,
    borsh::BorshSerialize,
    Debug,
    Clone,
    Hash,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
)]
pub struct Blob(pub Digest);
impl HasBorshSchema for Blob {
    fn borsh_schema() -> BorshSchema {
        BorshSchema::Blob
    }
}

#[derive(
    borsh::BorshDeserialize,
    borsh::BorshSerialize,
    Debug,
    Clone,
    Hash,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
)]
pub struct Snapshot(pub Digest);
impl HasBorshSchema for Snapshot {
    fn borsh_schema() -> BorshSchema {
        BorshSchema::Snapshot
    }
}

/// A key-resolver algorithm.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub enum KeyResolverKind {
    /// The key is stored inline and may be used directly.
    Inline([u8; 32]),
    /// The key must be resolved using the key resolver with the given digest. The `data` is passed
    /// to the key resolver algorithm.
    Custom { id: Digest, data: Vec<u8> },
}

/// A key-resolver algorithm.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub struct KeyResolver {
    pub name: String,
    pub specification: Digest,
}

/// An encryption algorithm.
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, Clone)]
pub struct EncryptionAlgorithm {
    pub name: String,
    pub specification: Digest,
}