structfs-core-store 0.2.0

Core StructFS store traits - Record, Value, Path, Format
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! The Record type - maybe-parsed data with format hint.

use bytes::Bytes;

use crate::{Codec, Error, Format, Value};

/// A record that can be forwarded without parsing or parsed for inspection.
///
/// This is the core abstraction for zero-copy forwarding. A Record is either:
/// - `Raw`: Unparsed bytes with a format hint. Can be forwarded without parsing.
/// - `Parsed`: A parsed `Value` tree. Efficient for inspection and modification.
///
/// # Zero-Copy Forwarding
///
/// ```rust
/// use structfs_core_store::{Record, Format};
/// use bytes::Bytes;
///
/// // Data comes in as raw bytes
/// let record = Record::raw(Bytes::from_static(b"{\"name\":\"Alice\"}"), Format::JSON);
///
/// // Forward without parsing - just pass the Record through
/// // No JSON parsing happens!
/// ```
///
/// # Lazy Parsing
///
/// ```rust
/// use structfs_core_store::{Record, Format, Value};
/// use bytes::Bytes;
///
/// let record = Record::raw(Bytes::from_static(b"..."), Format::JSON);
///
/// // Only parse when you need to inspect
/// // let value = record.into_value(&codec)?;
/// ```
#[derive(Clone)]
#[non_exhaustive]
pub enum Record {
    /// Unparsed bytes with format hint.
    ///
    /// The bytes can be forwarded without parsing. Use `into_value()` to
    /// parse when you need to inspect or modify the data.
    #[non_exhaustive]
    Raw {
        /// The raw bytes.
        bytes: Bytes,
        /// Hint about the format (JSON, protobuf, etc.)
        format: Format,
    },

    /// Parsed tree structure.
    ///
    /// Efficient for inspection and modification. Use `into_bytes()` to
    /// serialize when you need to send over the wire.
    Parsed(Value),
}

impl Record {
    // === Construction ===

    /// Create a record from raw bytes.
    pub fn raw(bytes: impl Into<Bytes>, format: Format) -> Self {
        Record::Raw {
            bytes: bytes.into(),
            format,
        }
    }

    /// Create a record from a parsed value.
    pub fn parsed(value: Value) -> Self {
        Record::Parsed(value)
    }

    // === Inspection (cheap) ===

    /// Check if this record is in raw (unparsed) form.
    pub fn is_raw(&self) -> bool {
        matches!(self, Record::Raw { .. })
    }

    /// Check if this record is parsed.
    pub fn is_parsed(&self) -> bool {
        matches!(self, Record::Parsed(_))
    }

    /// Get the format hint.
    ///
    /// For `Parsed` records, returns `Format::VALUE`.
    pub fn format(&self) -> Format {
        match self {
            Record::Raw { format, .. } => format.clone(),
            Record::Parsed(_) => Format::VALUE,
        }
    }

    /// Get raw bytes if available without serialization.
    ///
    /// Returns `None` for `Parsed` records (would require serialization).
    pub fn as_bytes(&self) -> Option<&Bytes> {
        match self {
            Record::Raw { bytes, .. } => Some(bytes),
            Record::Parsed(_) => None,
        }
    }

    /// Get parsed value if available without parsing.
    ///
    /// Returns `None` for `Raw` records (would require parsing).
    pub fn as_value(&self) -> Option<&Value> {
        match self {
            Record::Raw { .. } => None,
            Record::Parsed(v) => Some(v),
        }
    }

    // === Conversion (potentially costly) ===

    /// Parse into a Value.
    ///
    /// - For `Parsed` records: returns the value (no cost).
    /// - For `Raw` records: parses the bytes using the codec.
    ///
    /// This is where you pay the parsing cost.
    pub fn into_value(self, codec: &dyn Codec) -> Result<Value, Error> {
        match self {
            Record::Parsed(v) => Ok(v),
            Record::Raw { bytes, format } => codec.decode(&bytes, &format),
        }
    }

    /// Serialize into bytes.
    ///
    /// - For `Raw` records with matching format: returns the bytes (no cost).
    /// - For `Raw` records with different format: transcodes via Value.
    /// - For `Parsed` records: serializes using the codec.
    ///
    /// This is where you pay the serialization cost.
    pub fn into_bytes(self, codec: &dyn Codec, target_format: &Format) -> Result<Bytes, Error> {
        match self {
            Record::Raw { bytes, format } if &format == target_format => Ok(bytes),
            Record::Raw { bytes, format } => {
                // Transcode: parse then re-serialize
                let value = codec.decode(&bytes, &format)?;
                codec.encode(&value, target_format)
            }
            Record::Parsed(v) => codec.encode(&v, target_format),
        }
    }

    /// Try to get bytes without serialization, returning the record if not possible.
    ///
    /// Useful when you want bytes if available, but don't want to pay serialization cost.
    pub fn try_into_bytes(self, target_format: &Format) -> Result<Bytes, Self> {
        match self {
            Record::Raw { bytes, format } if &format == target_format => Ok(bytes),
            other => Err(other),
        }
    }
}

impl std::fmt::Debug for Record {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Record::Raw { bytes, format } => f
                .debug_struct("Record::Raw")
                .field("bytes_len", &bytes.len())
                .field("format", format)
                .finish(),
            Record::Parsed(v) => f.debug_tuple("Record::Parsed").field(v).finish(),
        }
    }
}

impl From<Value> for Record {
    fn from(v: Value) -> Self {
        Record::Parsed(v)
    }
}

impl From<Bytes> for Record {
    fn from(bytes: Bytes) -> Self {
        Record::Raw {
            bytes,
            format: Format::OCTET_STREAM,
        }
    }
}

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

    /// Simple test codec that handles JSON
    struct TestJsonCodec;

    impl Codec for TestJsonCodec {
        fn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error> {
            if format != &Format::JSON {
                return Err(Error::UnsupportedFormat(format.clone()));
            }
            let json: serde_json::Value = serde_json::from_slice(bytes)
                .map_err(|e| Error::decode(format.clone(), e.to_string()))?;
            Ok(json_to_value(json))
        }

        fn encode(&self, value: &Value, format: &Format) -> Result<Bytes, Error> {
            if format != &Format::JSON {
                return Err(Error::UnsupportedFormat(format.clone()));
            }
            let json = value_to_json(value);
            let bytes = serde_json::to_vec(&json)
                .map_err(|e| Error::encode(format.clone(), e.to_string()))?;
            Ok(Bytes::from(bytes))
        }

        fn supports(&self, format: &Format) -> bool {
            format == &Format::JSON
        }
    }

    fn json_to_value(json: serde_json::Value) -> Value {
        match json {
            serde_json::Value::Null => Value::Null,
            serde_json::Value::Bool(b) => Value::Bool(b),
            serde_json::Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Value::Integer(i)
                } else {
                    Value::Float(n.as_f64().unwrap_or(0.0))
                }
            }
            serde_json::Value::String(s) => Value::String(s),
            serde_json::Value::Array(arr) => {
                Value::Array(arr.into_iter().map(json_to_value).collect())
            }
            serde_json::Value::Object(obj) => {
                let map: BTreeMap<String, Value> = obj
                    .into_iter()
                    .map(|(k, v)| (k, json_to_value(v)))
                    .collect();
                Value::Map(map)
            }
        }
    }

    fn value_to_json(value: &Value) -> serde_json::Value {
        match value {
            Value::Null => serde_json::Value::Null,
            Value::Bool(b) => serde_json::Value::Bool(*b),
            Value::Integer(i) => serde_json::Value::Number((*i).into()),
            Value::Unsigned(i) => serde_json::Value::Number((*i).into()),
            Value::Float(f) => serde_json::Number::from_f64(*f)
                .map(serde_json::Value::Number)
                .unwrap_or(serde_json::Value::Null),
            Value::String(s) => serde_json::Value::String(s.clone()),
            Value::Bytes(b) => serde_json::Value::String(format!("bytes:{}", b.len())),
            Value::Array(arr) => serde_json::Value::Array(arr.iter().map(value_to_json).collect()),
            Value::Map(map) => {
                let obj: serde_json::Map<String, serde_json::Value> = map
                    .iter()
                    .map(|(k, v)| (k.clone(), value_to_json(v)))
                    .collect();
                serde_json::Value::Object(obj)
            }
        }
    }

    #[test]
    fn raw_record_inspection() {
        let record = Record::raw(Bytes::from_static(b"hello"), Format::JSON);

        assert!(record.is_raw());
        assert!(!record.is_parsed());
        assert_eq!(record.format(), Format::JSON);
        assert_eq!(record.as_bytes(), Some(&Bytes::from_static(b"hello")));
        assert_eq!(record.as_value(), None);
    }

    #[test]
    fn parsed_record_inspection() {
        let record = Record::parsed(Value::from("hello"));

        assert!(!record.is_raw());
        assert!(record.is_parsed());
        assert_eq!(record.format(), Format::VALUE);
        assert_eq!(record.as_bytes(), None);
        assert_eq!(record.as_value(), Some(&Value::from("hello")));
    }

    #[test]
    fn try_into_bytes_matching_format() {
        let bytes = Bytes::from_static(b"hello");
        let record = Record::raw(bytes.clone(), Format::JSON);

        let result = record.try_into_bytes(&Format::JSON);
        assert_eq!(result.unwrap(), bytes);
    }

    #[test]
    fn try_into_bytes_mismatched_format() {
        let record = Record::raw(Bytes::from_static(b"hello"), Format::JSON);

        let result = record.try_into_bytes(&Format::PROTOBUF);
        assert!(result.is_err()); // Returns the record back
    }

    #[test]
    fn into_value_parsed() {
        let codec = TestJsonCodec;
        let record = Record::parsed(Value::from("hello"));
        let value = record.into_value(&codec).unwrap();
        assert_eq!(value, Value::String("hello".to_string()));
    }

    #[test]
    fn into_value_raw() {
        let codec = TestJsonCodec;
        let record = Record::raw(Bytes::from_static(b"{\"name\":\"Alice\"}"), Format::JSON);
        let value = record.into_value(&codec).unwrap();
        match value {
            Value::Map(map) => {
                assert_eq!(map.get("name"), Some(&Value::String("Alice".to_string())));
            }
            _ => panic!("expected map"),
        }
    }

    #[test]
    fn into_bytes_raw_matching_format() {
        let codec = TestJsonCodec;
        let bytes = Bytes::from_static(b"{\"a\":1}");
        let record = Record::raw(bytes.clone(), Format::JSON);
        let result = record.into_bytes(&codec, &Format::JSON).unwrap();
        assert_eq!(result, bytes);
    }

    #[test]
    fn into_bytes_parsed() {
        let codec = TestJsonCodec;
        let record = Record::parsed(Value::from("hello"));
        let result = record.into_bytes(&codec, &Format::JSON).unwrap();
        assert_eq!(result, Bytes::from_static(b"\"hello\""));
    }

    #[test]
    fn into_bytes_raw_different_format_error() {
        let codec = TestJsonCodec;
        let record = Record::raw(Bytes::from_static(b"data"), Format::JSON);
        // Trying to transcode to PROTOBUF, which our codec doesn't support
        let result = record.into_bytes(&codec, &Format::PROTOBUF);
        assert!(result.is_err());
    }

    #[test]
    fn try_into_bytes_parsed_returns_err() {
        let record = Record::parsed(Value::Null);
        let result = record.try_into_bytes(&Format::JSON);
        assert!(result.is_err());
    }

    #[test]
    fn debug_raw_record() {
        let record = Record::raw(Bytes::from_static(b"hello"), Format::JSON);
        let debug = format!("{:?}", record);
        assert!(debug.contains("Record::Raw"));
        assert!(debug.contains("bytes_len"));
        assert!(debug.contains("format"));
    }

    #[test]
    fn debug_parsed_record() {
        let record = Record::parsed(Value::from(42));
        let debug = format!("{:?}", record);
        assert!(debug.contains("Record::Parsed"));
    }

    #[test]
    fn from_value_impl() {
        let value = Value::from("test");
        let record: Record = value.into();
        assert!(record.is_parsed());
        assert_eq!(record.as_value(), Some(&Value::String("test".to_string())));
    }

    #[test]
    fn from_bytes_impl() {
        let bytes = Bytes::from_static(b"test data");
        let record: Record = bytes.clone().into();
        assert!(record.is_raw());
        assert_eq!(record.format(), Format::OCTET_STREAM);
        assert_eq!(record.as_bytes(), Some(&bytes));
    }

    #[test]
    fn clone_raw_record() {
        let record = Record::raw(Bytes::from_static(b"data"), Format::JSON);
        let cloned = record.clone();
        assert!(cloned.is_raw());
        assert_eq!(cloned.format(), Format::JSON);
    }

    #[test]
    fn clone_parsed_record() {
        let record = Record::parsed(Value::from(123));
        let cloned = record.clone();
        assert!(cloned.is_parsed());
        assert_eq!(cloned.as_value(), Some(&Value::Integer(123)));
    }

    #[test]
    fn raw_with_vec_bytes() {
        let vec = vec![1u8, 2, 3, 4];
        let record = Record::raw(vec, Format::OCTET_STREAM);
        assert!(record.is_raw());
        assert_eq!(record.as_bytes().map(|b| b.len()), Some(4));
    }

    /// Codec that supports both JSON and a custom format
    struct MultiFormatCodec;

    impl Codec for MultiFormatCodec {
        fn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error> {
            if format == &Format::JSON {
                let json: serde_json::Value = serde_json::from_slice(bytes)
                    .map_err(|e| Error::decode(format.clone(), e.to_string()))?;
                Ok(json_to_value(json))
            } else if format.as_str() == "text/plain" {
                let s = String::from_utf8_lossy(bytes);
                Ok(Value::String(s.to_string()))
            } else {
                Err(Error::UnsupportedFormat(format.clone()))
            }
        }

        fn encode(&self, value: &Value, format: &Format) -> Result<Bytes, Error> {
            if format == &Format::JSON {
                let json = value_to_json(value);
                let bytes = serde_json::to_vec(&json)
                    .map_err(|e| Error::encode(format.clone(), e.to_string()))?;
                Ok(Bytes::from(bytes))
            } else if format.as_str() == "text/plain" {
                match value {
                    Value::String(s) => Ok(Bytes::from(s.clone())),
                    _ => Ok(Bytes::from(format!("{:?}", value))),
                }
            } else {
                Err(Error::UnsupportedFormat(format.clone()))
            }
        }

        fn supports(&self, format: &Format) -> bool {
            format == &Format::JSON || format.as_str() == "text/plain"
        }
    }

    #[test]
    fn into_bytes_transcode() {
        // Test transcoding: Raw JSON -> text/plain
        let codec = MultiFormatCodec;
        let text_format = Format::new("text/plain");

        // Create a raw JSON record
        let record = Record::raw(Bytes::from_static(b"\"hello world\""), Format::JSON);

        // Transcode to text/plain
        let result = record.into_bytes(&codec, &text_format).unwrap();

        // The JSON string "hello world" should be decoded then re-encoded as text/plain
        assert_eq!(result.as_ref(), b"hello world");
    }

    #[test]
    fn into_bytes_transcode_decode_error() {
        // Test transcode error when decode fails
        let codec = MultiFormatCodec;
        let text_format = Format::new("text/plain");

        // Create a raw record with invalid JSON
        let record = Record::raw(Bytes::from_static(b"not valid json {{{"), Format::JSON);

        // Transcode should fail during decode phase
        let result = record.into_bytes(&codec, &text_format);
        assert!(result.is_err());
    }

    #[test]
    fn into_value_decode_error() {
        let codec = TestJsonCodec;
        let record = Record::raw(Bytes::from_static(b"not valid json"), Format::JSON);
        let result = record.into_value(&codec);
        assert!(result.is_err());
    }

    #[test]
    fn into_bytes_parsed_encode_error() {
        let codec = TestJsonCodec;
        let record = Record::parsed(Value::from("test"));
        // Try to encode as PROTOBUF which TestJsonCodec doesn't support
        let result = record.into_bytes(&codec, &Format::PROTOBUF);
        assert!(result.is_err());
    }

    #[test]
    fn format_raw_format_clone() {
        // Verify that format() returns a cloned format, not a reference
        let record = Record::raw(Bytes::from_static(b"data"), Format::new("custom/format"));
        let format = record.format();
        assert_eq!(format.as_str(), "custom/format");
    }

    #[test]
    fn into_value_unsupported_format() {
        let codec = TestJsonCodec;
        let record = Record::raw(Bytes::from_static(b"data"), Format::PROTOBUF);
        let result = record.into_value(&codec);
        assert!(result.is_err());
    }
}