cdx-core 0.7.1

Core library for reading, writing, and validating Codex Document Format (.cdx) files
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Asset index types for managing collections of assets.

use serde::{Deserialize, Serialize};

use super::{FontAsset, ImageAsset};
use crate::DocumentId;

/// An asset index file structure.
///
/// This represents files like `assets/images/index.json` or `assets/fonts/index.json`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetIndex<T> {
    /// Format version.
    pub version: String,

    /// Total count of assets.
    pub count: u32,

    /// Total size of all assets in bytes.
    pub total_size: u64,

    /// Array of asset entries.
    pub assets: Vec<T>,
}

impl<T> AssetIndex<T> {
    /// Create a new empty asset index.
    #[must_use]
    pub fn new() -> Self {
        Self {
            version: crate::SPEC_VERSION.to_string(),
            count: 0,
            total_size: 0,
            assets: Vec::new(),
        }
    }

    /// Add an asset to the index.
    pub fn add(&mut self, asset: T, size: u64) {
        self.assets.push(asset);
        self.count += 1;
        self.total_size += size;
    }

    /// Check if the index is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.assets.is_empty()
    }

    /// Get the number of assets.
    #[must_use]
    pub fn len(&self) -> usize {
        self.assets.len()
    }
}

impl<T> Default for AssetIndex<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Type alias for image asset index.
pub type ImageIndex = AssetIndex<ImageAsset>;

/// Type alias for font asset index.
pub type FontIndex = AssetIndex<FontAsset>;

/// Type alias for embed asset index.
pub type EmbedIndex = AssetIndex<EmbedAsset>;

/// An embedded file asset.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbedAsset {
    /// Unique identifier for the embed.
    pub id: String,

    /// Path within the archive (e.g., "assets/embeds/data.csv").
    pub path: String,

    /// Content hash for verification.
    pub hash: DocumentId,

    /// File size in bytes.
    pub size: u64,

    /// MIME type of the embedded file.
    pub mime_type: String,

    /// Original filename (if known).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,

    /// Description of the embedded file.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Whether the embed should be displayed inline.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub inline: bool,
}

impl EmbedAsset {
    /// Create a new embed asset.
    #[must_use]
    pub fn new(id: impl Into<String>, mime_type: impl Into<String>) -> Self {
        let id = id.into();
        let path = format!("assets/embeds/{id}");
        Self {
            id,
            path,
            hash: DocumentId::pending(),
            size: 0,
            mime_type: mime_type.into(),
            filename: None,
            description: None,
            inline: false,
        }
    }

    /// Set the content hash.
    #[must_use]
    pub fn with_hash(mut self, hash: DocumentId) -> Self {
        self.hash = hash;
        self
    }

    /// Set the file size.
    #[must_use]
    pub const fn with_size(mut self, size: u64) -> Self {
        self.size = size;
        self
    }

    /// Set the original filename.
    #[must_use]
    pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
        self.filename = Some(filename.into());
        self
    }

    /// Set the description.
    #[must_use]
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set whether the embed should be displayed inline.
    #[must_use]
    pub const fn with_inline(mut self, inline: bool) -> Self {
        self.inline = inline;
        self
    }

    /// Set a custom path.
    #[must_use]
    pub fn with_path(mut self, path: impl Into<String>) -> Self {
        self.path = path.into();
        self
    }
}

impl super::Asset for EmbedAsset {
    fn id(&self) -> &str {
        &self.id
    }

    fn path(&self) -> &str {
        &self.path
    }

    fn hash(&self) -> &DocumentId {
        &self.hash
    }

    fn size(&self) -> u64 {
        self.size
    }

    fn mime_type(&self) -> &str {
        &self.mime_type
    }
}

/// Generic asset entry that can represent any asset type.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum AssetEntry {
    /// Image asset.
    Image(ImageAsset),
    /// Font asset.
    Font(FontAsset),
    /// Embedded file asset.
    Embed(EmbedAsset),
    /// Alias to another asset (for deduplication).
    ///
    /// When two assets have identical content (same hash), one can be stored
    /// as an alias pointing to the canonical asset. This saves storage space
    /// while maintaining separate logical identities.
    Alias(AssetAlias),
}

/// An alias entry that references another asset.
///
/// Used for deduplication: when the same content is referenced by multiple
/// logical assets, only one copy is stored and others become aliases.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetAlias {
    /// Unique identifier for this alias.
    pub id: String,

    /// ID of the canonical asset this aliases to.
    pub alias_of: String,

    /// The hash of the content (same as the canonical asset).
    pub hash: DocumentId,
}

impl AssetEntry {
    /// Get the asset ID.
    #[must_use]
    pub fn id(&self) -> &str {
        match self {
            Self::Image(a) => &a.id,
            Self::Font(a) => &a.id,
            Self::Embed(a) => &a.id,
            Self::Alias(a) => &a.id,
        }
    }

    /// Get the asset path.
    ///
    /// For aliases, this returns an empty string as aliases don't have their own path.
    /// Use `resolve_path` with the asset index to get the canonical asset's path.
    #[must_use]
    pub fn path(&self) -> &str {
        match self {
            Self::Image(a) => &a.path,
            Self::Font(a) => &a.path,
            Self::Embed(a) => &a.path,
            Self::Alias(_) => "",
        }
    }

    /// Get the asset hash.
    #[must_use]
    pub fn hash(&self) -> &DocumentId {
        match self {
            Self::Image(a) => &a.hash,
            Self::Font(a) => &a.hash,
            Self::Embed(a) => &a.hash,
            Self::Alias(a) => &a.hash,
        }
    }

    /// Get the asset size.
    ///
    /// For aliases, this returns 0 as no additional storage is used.
    #[must_use]
    pub fn size(&self) -> u64 {
        match self {
            Self::Image(a) => a.size,
            Self::Font(a) => a.size,
            Self::Embed(a) => a.size,
            Self::Alias(_) => 0,
        }
    }

    /// Check if this entry is an alias.
    #[must_use]
    pub fn is_alias(&self) -> bool {
        matches!(self, Self::Alias(_))
    }

    /// Get the canonical asset ID if this is an alias.
    #[must_use]
    pub fn alias_of(&self) -> Option<&str> {
        match self {
            Self::Alias(a) => Some(&a.alias_of),
            _ => None,
        }
    }
}

impl AssetAlias {
    /// Create a new asset alias.
    #[must_use]
    pub fn new(id: impl Into<String>, alias_of: impl Into<String>, hash: DocumentId) -> Self {
        Self {
            id: id.into(),
            alias_of: alias_of.into(),
            hash,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::asset::ImageFormat;

    #[test]
    fn test_asset_index_new() {
        let index: ImageIndex = AssetIndex::new();
        assert!(index.is_empty());
        assert_eq!(index.count, 0);
        assert_eq!(index.total_size, 0);
    }

    #[test]
    fn test_asset_index_add() {
        let mut index: ImageIndex = AssetIndex::new();
        let image = ImageAsset::new("test", ImageFormat::Png).with_size(1024);
        index.add(image, 1024);

        assert_eq!(index.len(), 1);
        assert_eq!(index.count, 1);
        assert_eq!(index.total_size, 1024);
    }

    #[test]
    fn test_embed_asset_new() {
        let embed = EmbedAsset::new("data", "text/csv");
        assert_eq!(embed.id, "data");
        assert_eq!(embed.mime_type, "text/csv");
        assert_eq!(embed.path, "assets/embeds/data");
    }

    #[test]
    fn test_embed_asset_builder() {
        let embed = EmbedAsset::new("spreadsheet", "application/vnd.ms-excel")
            .with_filename("sales.xlsx")
            .with_description("Quarterly sales data")
            .with_size(65536)
            .with_inline(false);

        assert_eq!(embed.filename, Some("sales.xlsx".to_string()));
        assert_eq!(embed.description, Some("Quarterly sales data".to_string()));
        assert_eq!(embed.size, 65536);
        assert!(!embed.inline);
    }

    #[test]
    fn test_asset_entry_variants() {
        let image = AssetEntry::Image(ImageAsset::new("img", ImageFormat::Png));
        assert_eq!(image.id(), "img");

        let embed = AssetEntry::Embed(EmbedAsset::new("file", "text/plain"));
        assert_eq!(embed.id(), "file");
    }

    #[test]
    fn test_asset_index_serialization() {
        let mut index: ImageIndex = AssetIndex::new();
        let image = ImageAsset::new("test", ImageFormat::Png).with_size(1024);
        index.add(image, 1024);

        let json = serde_json::to_string_pretty(&index).unwrap();
        assert!(json.contains(r#""count": 1"#));
        assert!(json.contains(r#""totalSize": 1024"#));

        let deserialized: ImageIndex = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.count, 1);
        assert_eq!(deserialized.total_size, 1024);
    }

    #[test]
    fn test_asset_alias_creation() {
        let hash: DocumentId =
            "sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
                .parse()
                .unwrap();
        let alias = AssetAlias::new("duplicate-logo", "original-logo", hash.clone());

        assert_eq!(alias.id, "duplicate-logo");
        assert_eq!(alias.alias_of, "original-logo");
        assert_eq!(alias.hash, hash);
    }

    #[test]
    fn test_asset_entry_alias() {
        let hash: DocumentId =
            "sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
                .parse()
                .unwrap();
        let alias = AssetEntry::Alias(AssetAlias::new("dup", "orig", hash));

        assert!(alias.is_alias());
        assert_eq!(alias.alias_of(), Some("orig"));
        assert_eq!(alias.id(), "dup");
        assert_eq!(alias.size(), 0); // Aliases don't add storage
        assert_eq!(alias.path(), ""); // Aliases don't have their own path
    }

    #[test]
    fn test_asset_entry_not_alias() {
        let image = AssetEntry::Image(ImageAsset::new("img", ImageFormat::Png));

        assert!(!image.is_alias());
        assert_eq!(image.alias_of(), None);
    }

    #[test]
    fn test_asset_alias_serialization() {
        let hash: DocumentId =
            "sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
                .parse()
                .unwrap();
        let alias = AssetEntry::Alias(AssetAlias::new("dup", "orig", hash));

        let json = serde_json::to_string_pretty(&alias).unwrap();
        assert!(json.contains(r#""type": "alias""#));
        assert!(json.contains(r#""aliasOf": "orig""#));

        let deserialized: AssetEntry = serde_json::from_str(&json).unwrap();
        assert!(deserialized.is_alias());
        assert_eq!(deserialized.alias_of(), Some("orig"));
    }
}