datacard-rs 1.0.0

Generic binary card format library with checksums and pluggable format traits
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
//! Generic card implementation
//!
//! Works with any CardFormat implementation.

use crate::checksum::calculate_crc32;
use crate::error::{CardError, Result};
use crate::format::CardFormat;
use crate::header::FLAG_HAS_CHECKSUM;
use std::fs;
use std::io::{Cursor, Read, Write};
use std::marker::PhantomData;
use std::path::{Path, PathBuf};

/// Generic card header (8 bytes)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GenericHeader {
    pub magic: [u8; 4],
    pub major: u8,
    pub minor: u8,
    pub flags: u16,
}

impl GenericHeader {
    /// Create header for a specific format
    pub fn for_format<F: CardFormat>() -> Self {
        Self {
            magic: F::MAGIC,
            major: F::VERSION_MAJOR,
            minor: F::VERSION_MINOR,
            flags: 0,
        }
    }

    /// Create header with checksum flag
    pub fn for_format_with_checksum<F: CardFormat>() -> Self {
        Self {
            magic: F::MAGIC,
            major: F::VERSION_MAJOR,
            minor: F::VERSION_MINOR,
            flags: FLAG_HAS_CHECKSUM,
        }
    }

    /// Check if checksum flag is set
    pub fn has_checksum(&self) -> bool {
        self.flags & FLAG_HAS_CHECKSUM != 0
    }

    /// Write header to writer
    pub fn write_to<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
        writer.write_all(&self.magic)?;
        writer.write_all(&[self.major])?;
        writer.write_all(&[self.minor])?;
        writer.write_all(&self.flags.to_le_bytes())?;
        Ok(())
    }

    /// Get header as bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(8);
        bytes.extend_from_slice(&self.magic);
        bytes.push(self.major);
        bytes.push(self.minor);
        bytes.extend_from_slice(&self.flags.to_le_bytes());
        bytes
    }

    /// Read header from reader
    pub fn read_from<R: Read>(reader: &mut R) -> Result<Self> {
        let mut magic = [0u8; 4];
        reader.read_exact(&mut magic)?;

        let mut version = [0u8; 2];
        reader.read_exact(&mut version)?;

        let mut flags_bytes = [0u8; 2];
        reader.read_exact(&mut flags_bytes)?;

        Ok(Self {
            magic,
            major: version[0],
            minor: version[1],
            flags: u16::from_le_bytes(flags_bytes),
        })
    }

    /// Validate header against a format
    pub fn validate<F: CardFormat>(&self) -> Result<()> {
        F::validate_magic(&self.magic)?;
        F::validate_version(self.major, self.minor)?;
        Ok(())
    }
}

/// Generic card metadata (JSON-serializable)
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct GenericMetadata {
    /// Payload identifier
    pub id: String,

    /// Payload size in bytes
    pub payload_size: u64,

    /// Optional: original size before any transformation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub original_size: Option<u64>,

    /// Format-specific extension data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ext: Option<serde_json::Value>,
}

impl GenericMetadata {
    /// Create minimal metadata
    pub fn new(id: impl Into<String>, payload_size: u64) -> Self {
        Self {
            id: id.into(),
            payload_size,
            original_size: None,
            ext: None,
        }
    }

    /// Builder: set original size
    pub fn with_original_size(mut self, size: u64) -> Self {
        self.original_size = Some(size);
        self
    }

    /// Builder: set extension data
    pub fn with_ext(mut self, ext: serde_json::Value) -> Self {
        self.ext = Some(ext);
        self
    }

    /// Serialize to JSON bytes
    pub fn to_json(&self) -> Result<Vec<u8>> {
        Ok(serde_json::to_vec(self)?)
    }

    /// Deserialize from JSON bytes
    pub fn from_json(bytes: &[u8]) -> Result<Self> {
        Ok(serde_json::from_slice(bytes)?)
    }
}

/// Generic card that works with any CardFormat
#[derive(Debug, Clone)]
pub struct GenericCard<F: CardFormat> {
    /// Card header
    pub header: GenericHeader,

    /// Card metadata
    pub metadata: GenericMetadata,

    /// Raw payload bytes
    pub payload: Vec<u8>,

    /// Format marker
    _format: PhantomData<F>,
}

impl<F: CardFormat> GenericCard<F> {
    /// Create a new card with payload
    pub fn new(id: impl Into<String>, payload: Vec<u8>) -> Self {
        let metadata = GenericMetadata::new(id, payload.len() as u64);
        Self {
            header: GenericHeader::for_format::<F>(),
            metadata,
            payload,
            _format: PhantomData,
        }
    }

    /// Create a new card with checksum
    pub fn new_with_checksum(id: impl Into<String>, payload: Vec<u8>) -> Self {
        let metadata = GenericMetadata::new(id, payload.len() as u64);
        Self {
            header: GenericHeader::for_format_with_checksum::<F>(),
            metadata,
            payload,
            _format: PhantomData,
        }
    }

    /// Create from metadata and payload
    pub fn from_parts(metadata: GenericMetadata, payload: Vec<u8>) -> Result<Self> {
        if payload.len() as u64 != metadata.payload_size {
            return Err(CardError::PayloadSizeMismatch {
                expected: metadata.payload_size,
                actual: payload.len(),
            });
        }

        Ok(Self {
            header: GenericHeader::for_format::<F>(),
            metadata,
            payload,
            _format: PhantomData,
        })
    }

    /// Load card from file
    ///
    /// The `.card` extension is added automatically - do NOT include it in the path.
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = Self::enforce_card_extension(path.as_ref())?;
        let data = fs::read(&path)?;
        Self::from_bytes(&data)
    }

    /// Save card to file
    ///
    /// The `.card` extension is added automatically - do NOT include it in the path.
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let path = Self::enforce_card_extension(path.as_ref())?;
        let bytes = self.to_bytes()?;
        fs::write(&path, bytes)?;
        Ok(())
    }

    /// Enforce .card extension - reject paths that already have it, add it automatically
    fn enforce_card_extension(path: &Path) -> Result<PathBuf> {
        // Reject if path already ends with .card
        if let Some(ext) = path.extension() {
            if ext == "card" {
                return Err(CardError::InvalidFormat(
                    "Do not include .card extension in path - it is added automatically".to_string()
                ));
            }
        }

        // Add .card extension
        let mut path_buf = path.to_path_buf();
        let mut new_name = path_buf
            .file_name()
            .map(|s| s.to_os_string())
            .unwrap_or_default();
        new_name.push(".card");
        path_buf.set_file_name(new_name);

        Ok(path_buf)
    }

    /// Calculate CRC32 checksum
    pub fn calculate_checksum(&self) -> u32 {
        let header_bytes = self.header.to_bytes();
        let meta_json = self.metadata.to_json().unwrap();
        let meta_len_bytes = (meta_json.len() as u32).to_le_bytes();

        calculate_crc32(&[&header_bytes, &meta_len_bytes, &meta_json, &self.payload])
    }

    /// Serialize to bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        let mut buffer = Vec::new();

        // Header
        self.header.write_to(&mut buffer)?;

        // Metadata
        let meta_json = self.metadata.to_json()?;
        if meta_json.len() > 65536 {
            return Err(CardError::MetadataTooLarge(meta_json.len()));
        }
        buffer.write_all(&(meta_json.len() as u32).to_le_bytes())?;
        buffer.write_all(&meta_json)?;

        // Payload
        buffer.write_all(&self.payload)?;

        // Checksum if enabled
        if self.header.has_checksum() {
            let checksum = self.calculate_checksum();
            buffer.write_all(&checksum.to_le_bytes())?;
        }

        Ok(buffer)
    }

    /// Deserialize from bytes
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);

        // Read and validate header
        let header = GenericHeader::read_from(&mut cursor)?;
        header.validate::<F>()?;

        // Read metadata
        let mut meta_len_bytes = [0u8; 4];
        cursor.read_exact(&mut meta_len_bytes)?;
        let meta_len = u32::from_le_bytes(meta_len_bytes) as usize;

        let mut meta_json = vec![0u8; meta_len];
        cursor.read_exact(&mut meta_json)?;
        let metadata = GenericMetadata::from_json(&meta_json)?;

        // Read payload
        let payload_len = metadata.payload_size as usize;
        let mut payload = vec![0u8; payload_len];
        cursor.read_exact(&mut payload)?;

        // Format-specific payload validation
        F::validate_payload(&payload)?;

        // Validate checksum if present
        if header.has_checksum() {
            let mut checksum_bytes = [0u8; 4];
            cursor.read_exact(&mut checksum_bytes)?;
            let stored_checksum = u32::from_le_bytes(checksum_bytes);

            let card = Self {
                header,
                metadata,
                payload,
                _format: PhantomData,
            };

            let calculated = card.calculate_checksum();
            if calculated != stored_checksum {
                return Err(CardError::ChecksumMismatch {
                    expected: stored_checksum,
                    actual: calculated,
                });
            }

            Ok(card)
        } else {
            Ok(Self {
                header,
                metadata,
                payload,
                _format: PhantomData,
            })
        }
    }

    /// Get payload reference
    pub fn payload(&self) -> &[u8] {
        &self.payload
    }

    /// Get ID
    pub fn id(&self) -> &str {
        &self.metadata.id
    }

    /// Check if card has checksum
    pub fn has_checksum(&self) -> bool {
        self.header.has_checksum()
    }
}

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

    /// Test format
    struct TestFormat;

    impl CardFormat for TestFormat {
        const MAGIC: [u8; 4] = *b"TEST";
        const VERSION_MAJOR: u8 = 1;
        const VERSION_MINOR: u8 = 0;

        fn format_name() -> &'static str {
            "Test"
        }
    }

    #[test]
    fn test_generic_card_roundtrip() {
        let payload = vec![1, 2, 3, 4, 5];
        let card: GenericCard<TestFormat> = GenericCard::new("test::roundtrip", payload.clone());

        let bytes = card.to_bytes().unwrap();
        let loaded: GenericCard<TestFormat> = GenericCard::from_bytes(&bytes).unwrap();

        assert_eq!(loaded.id(), "test::roundtrip");
        assert_eq!(loaded.payload(), &payload);
    }

    #[test]
    fn test_generic_card_with_checksum() {
        let payload = vec![1, 2, 3, 4, 5];
        let card: GenericCard<TestFormat> =
            GenericCard::new_with_checksum("test::checksum", payload.clone());

        assert!(card.has_checksum());

        let bytes = card.to_bytes().unwrap();
        let loaded: GenericCard<TestFormat> = GenericCard::from_bytes(&bytes).unwrap();

        assert!(loaded.has_checksum());
        assert_eq!(loaded.payload(), &payload);
    }

    #[test]
    fn test_wrong_magic_fails() {
        struct OtherFormat;
        impl CardFormat for OtherFormat {
            const MAGIC: [u8; 4] = *b"OTHE";
            const VERSION_MAJOR: u8 = 1;
            const VERSION_MINOR: u8 = 0;
            fn format_name() -> &'static str {
                "Other"
            }
        }

        let card: GenericCard<TestFormat> = GenericCard::new("test", vec![1, 2, 3]);
        let bytes = card.to_bytes().unwrap();

        // Try to load as OtherFormat - should fail
        let result: Result<GenericCard<OtherFormat>> = GenericCard::from_bytes(&bytes);
        assert!(matches!(result, Err(CardError::InvalidMagic(_))));
    }
}