serde_evaluate 0.2.2

Extract single scalar field values from Serializable structs without full deserialization.
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Tests for the FieldExtractor (originally in lib.rs)

// Update imports to use the crate name since this is an integration test
use serde::{Deserialize, Serialize};
use serde_evaluate::error::EvaluateError;
use serde_evaluate::extractor::FieldExtractor;
use serde_evaluate::value::FieldScalarValue;
use std::collections::BTreeMap;

// Define test-specific structs here (copied from lib.rs tests)
#[derive(Serialize, Deserialize, Debug, Clone)]
struct TestRecord {
    id: i32,
    name: String,
    active: bool,
    count: Option<i32>,
    missing_count: Option<i32>,
    temperature: f32,
    initial: char,
    #[serde(with = "serde_bytes")] // Add this attribute
    data_bytes: Vec<u8>,
    unit_val: (),
    // New fields for Option tests
    opt_bool_some: Option<bool>,
    opt_bool_none: Option<bool>,
    opt_char_some: Option<char>,
    opt_char_none: Option<char>,
    opt_string_some: Option<String>,
    opt_string_none: Option<String>,
    #[serde(with = "serde_bytes")] // Add this attribute
    opt_bytes_some: Option<Vec<u8>>,
    #[serde(with = "serde_bytes")] // Add this attribute
    opt_bytes_none: Option<Vec<u8>>,
    opt_unit_some: Option<()>, // Note: () is the unit type
    opt_unit_none: Option<()>, // Note: () is the unit type
    opt_vec: Option<Vec<i32>>, // Option containing non-scalar
}

// New struct for map test
#[derive(Serialize)]
struct MapTestStruct {
    id: i32,
    my_map: BTreeMap<String, i32>,
}

fn create_test_record() -> TestRecord {
    TestRecord {
        id: 101,
        name: "Test Record".to_string(),
        active: true,
        count: Some(42),
        missing_count: None,
        temperature: 98.6,
        initial: 'X',
        data_bytes: vec![1, 2, 3, 4],
        unit_val: (),
        // Initialize new fields
        opt_bool_some: Some(true),
        opt_bool_none: None,
        opt_char_some: Some('Z'),
        opt_char_none: None,
        opt_string_some: Some("Hello Option".to_string()),
        opt_string_none: None,
        opt_bytes_some: Some(vec![10, 20, 30]),
        opt_bytes_none: None,
        opt_unit_some: Some(()), // Some variant of Option<()>
        opt_unit_none: None,     // None variant of Option<()>
        opt_vec: Some(vec![11, 22, 33]),
    }
}

#[test]
fn initial_setup_compiles() {
    let record = create_test_record();
    assert_eq!(record.id, 101);
}

// --- FieldExtractor Tests ---

#[test]
fn test_extract_string_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("name".to_string());
    let result = extractor.evaluate(&record);
    assert_eq!(
        result,
        Ok(FieldScalarValue::String("Test Record".to_string()))
    );
}

#[test]
fn test_extract_uint_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("id".to_string());
    let result = extractor.evaluate(&record);
    // Note: id is i32, captured as Int
    assert_eq!(result, Ok(FieldScalarValue::I32(101)));
}

#[test]
fn test_extract_missing_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("non_existent_field".to_string());
    let result = extractor.evaluate(&record);
    assert!(
        matches!(result, Err(EvaluateError::FieldNotFound { field_name }) if field_name == "non_existent_field")
    );
}

#[test]
fn test_extract_f32_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("temperature".to_string());
    let result = extractor.evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::F32(98.6)));
}

#[test]
fn test_extract_char_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("initial".to_string());
    let result = extractor.evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::Char('X')));
}

#[test]
fn test_extract_bytes_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("data_bytes".to_string());
    let result = extractor.evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::Bytes(vec![1, 2, 3, 4])));
}

#[test]
fn test_extract_unit_field() {
    let record = create_test_record();
    let extractor = FieldExtractor::new("unit_val".to_string());
    let result = extractor.evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::Unit));
}

// New test demonstrating Option handling
#[test]
fn test_extract_various_option_fields() {
    // Test case 1: Extracting an existing Option<i32> field with Some value
    let record = create_test_record();
    let extractor_some = FieldExtractor::new("count".to_string());
    let result_some = extractor_some.evaluate(&record);
    assert_eq!(
        result_some,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::I32(42)
        ))))
    );

    // Test case 2: Extracting an existing Option<i32> field with None value
    let extractor_none = FieldExtractor::new("missing_count".to_string());
    let result_none = extractor_none.evaluate(&record);
    assert_eq!(result_none, Ok(FieldScalarValue::Option(None)));

    // Test case 3: Extracting a non-existent field (should still be FieldNotFound error)
    let extractor_non_existent = FieldExtractor::new("non_existent_field".to_string());
    let result_non_existent = extractor_non_existent.evaluate(&record);
    assert!(matches!(
        result_non_existent,
        Err(EvaluateError::FieldNotFound { field_name }) if field_name == "non_existent_field"
    ));

    // Test case 4: Attempting to extract a field that exists but is not Option
    // (e.g., extracting 'name' which is String, not Option<String>)
    // The current implementation handles this correctly, extracting the scalar value directly.
    let extractor_string = FieldExtractor::new("name".to_string());
    let result_string = extractor_string.evaluate(&record);
    assert_eq!(
        result_string,
        Ok(FieldScalarValue::String("Test Record".to_string()))
    );
}

// Helper function to test Option extraction for both Some and None cases
fn assert_option_extraction(
    record: &TestRecord,
    some_field_name: &str,
    expected_some_value: FieldScalarValue,
    none_field_name: &str,
) {
    // Test Some variant
    let extractor_some = FieldExtractor::new(some_field_name.to_string());
    assert_eq!(
        extractor_some.evaluate(record),
        Ok(FieldScalarValue::Option(Some(Box::new(
            expected_some_value
        ))))
    );

    // Test None variant
    let extractor_none = FieldExtractor::new(none_field_name.to_string());
    assert_eq!(
        extractor_none.evaluate(record),
        Ok(FieldScalarValue::Option(None))
    );
}

#[test]
fn test_extract_option_bool() {
    let record = create_test_record();
    assert_option_extraction(
        &record,
        "opt_bool_some",
        FieldScalarValue::Bool(true),
        "opt_bool_none",
    );
}

#[test]
fn test_extract_option_char() {
    let record = create_test_record();
    assert_option_extraction(
        &record,
        "opt_char_some",
        FieldScalarValue::Char('Z'),
        "opt_char_none",
    );
}

#[test]
fn test_extract_option_string() {
    let record = create_test_record();
    assert_option_extraction(
        &record,
        "opt_string_some",
        FieldScalarValue::String("Hello Option".to_string()),
        "opt_string_none",
    );
}

#[test]
fn test_extract_option_bytes() {
    let record = create_test_record();
    assert_option_extraction(
        &record,
        "opt_bytes_some",
        FieldScalarValue::Bytes(vec![10, 20, 30]),
        "opt_bytes_none",
    );
}

#[test]
fn test_extract_option_unit() {
    let record = create_test_record();
    assert_option_extraction(
        &record,
        "opt_unit_some",
        FieldScalarValue::Unit,
        "opt_unit_none",
    );
}

// --- Tests for non-scalar Option contents (should fail) ---

#[test]
fn test_extract_option_non_scalar_vec() {
    // Attempting to extract an Option<Vec<i32>> should fail because Vec is not scalar
    let record = create_test_record();
    let extractor = FieldExtractor::new("opt_vec".to_string());
    let result = extractor.evaluate(&record);
    // Similar to the struct case, expect UnsupportedType for the sequence.
    assert!(
        matches!(result, Err(EvaluateError::UnsupportedType { type_name }) if type_name == "sequence"),
        "Expected UnsupportedType for Option<Vec>, got {:?}",
        result
    );
}

// New test for map field extraction attempt
#[test]
fn test_extract_map_field_unsupported() {
    let mut map_data = BTreeMap::new();
    map_data.insert("key1".to_string(), 1);
    map_data.insert("key2".to_string(), 2);

    let record = MapTestStruct {
        id: 1,
        my_map: map_data,
    };

    let extractor = FieldExtractor::new("my_map");
    let result = extractor.evaluate(&record);

    assert!(
        matches!(result, Err(EvaluateError::UnsupportedType { .. })),
        "Expected UnsupportedType error for map field, got {:?}",
        result
    );
}

#[test]
fn test_nested_option_u32_some_some() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_u32: Option<Option<u32>>,
    }

    let test_struct = TestStruct {
        nested_option_u32: Some(Some(123u32)),
    };

    let result = FieldExtractor::new("nested_option_u32").evaluate(&test_struct);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(Some(Box::new(FieldScalarValue::U32(123))))
        ))))
    );
}

#[test]
fn test_nested_option_u32_some_none() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_u32: Option<Option<u32>>,
    }

    let test_struct = TestStruct {
        nested_option_u32: Some(None),
    };

    let result = FieldExtractor::new("nested_option_u32").evaluate(&test_struct);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(None)
        ))))
    );
}

#[test]
fn test_nested_option_u32_none() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_u32: Option<Option<u32>>,
    }

    let test_struct = TestStruct {
        nested_option_u32: None,
    };

    let result = FieldExtractor::new("nested_option_u32").evaluate(&test_struct);
    assert_eq!(result, Ok(FieldScalarValue::Option(None)));
}

#[test]
fn test_nested_option_string_some_some() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_string: Option<Option<String>>,
    }

    let test_struct = TestStruct {
        nested_option_string: Some(Some("hello".to_string())),
    };

    let result = FieldExtractor::new("nested_option_string").evaluate(&test_struct);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(Some(Box::new(FieldScalarValue::String(
                "hello".to_string()
            ))))
        ))))
    );
}

#[test]
fn test_nested_option_string_some_none() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_string: Option<Option<String>>,
    }

    let test_struct = TestStruct {
        nested_option_string: Some(None),
    };

    let result = FieldExtractor::new("nested_option_string").evaluate(&test_struct);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(None)
        ))))
    );
}

#[test]
fn test_nested_option_string_none() {
    #[derive(Serialize)]
    struct TestStruct {
        nested_option_string: Option<Option<String>>,
    }

    let test_struct = TestStruct {
        nested_option_string: None,
    };

    let result = FieldExtractor::new("nested_option_string").evaluate(&test_struct);
    assert_eq!(result, Ok(FieldScalarValue::Option(None)));
}

// =============================================================================
// Enum variant handling
// =============================================================================

#[derive(Serialize)]
enum Status {
    Active,
    #[allow(dead_code)]
    Inactive,
}

#[derive(Serialize)]
enum Payload {
    Data(String),
}

#[derive(Serialize)]
enum Event {
    Click(i32, i32),
}

#[derive(Serialize)]
enum Task {
    Todo { id: u32, title: String },
}

#[test]
fn test_extract_unit_variant() {
    #[derive(Serialize)]
    struct Record {
        status: Status,
    }
    let record = Record {
        status: Status::Active,
    };
    let result = FieldExtractor::new("status").evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::Unit));
}

#[test]
fn test_extract_newtype_variant_rejected() {
    #[derive(Serialize)]
    struct Record {
        payload: Payload,
    }
    let record = Record {
        payload: Payload::Data("hello".to_string()),
    };
    let result = FieldExtractor::new("payload").evaluate(&record);
    assert!(
        matches!(
            result,
            Err(EvaluateError::UnsupportedVariant {
                variant_type: "newtype"
            })
        ),
        "Expected UnsupportedVariant newtype, got {:?}",
        result
    );
}

#[test]
fn test_extract_tuple_variant_rejected() {
    #[derive(Serialize)]
    struct Record {
        event: Event,
    }
    let record = Record {
        event: Event::Click(10, 20),
    };
    let result = FieldExtractor::new("event").evaluate(&record);
    assert!(
        matches!(
            result,
            Err(EvaluateError::UnsupportedVariant {
                variant_type: "tuple"
            })
        ),
        "Expected UnsupportedVariant tuple, got {:?}",
        result
    );
}

#[test]
fn test_extract_struct_variant_rejected() {
    #[derive(Serialize)]
    struct Record {
        task: Task,
    }
    let record = Record {
        task: Task::Todo {
            id: 1,
            title: "test".to_string(),
        },
    };
    let result = FieldExtractor::new("task").evaluate(&record);
    assert!(
        matches!(
            result,
            Err(EvaluateError::UnsupportedVariant {
                variant_type: "struct"
            })
        ),
        "Expected UnsupportedVariant struct, got {:?}",
        result
    );
}

// =============================================================================
// Newtype struct transparency
// =============================================================================

#[test]
fn test_extract_newtype_struct_field() {
    #[derive(Serialize)]
    struct UserId(u64);

    #[derive(Serialize)]
    struct User {
        id: UserId,
        name: String,
    }

    let user = User {
        id: UserId(12345),
        name: "Alice".to_string(),
    };
    let result = FieldExtractor::new("id").evaluate(&user);
    assert_eq!(result, Ok(FieldScalarValue::U64(12345)));
}

#[test]
fn test_nested_extract_through_newtype_struct() {
    #[derive(Serialize)]
    struct Inner {
        value: i32,
    }

    #[derive(Serialize)]
    struct Wrapper(Inner);

    #[derive(Serialize)]
    struct Outer {
        wrapped: Wrapper,
    }

    let data = Outer {
        wrapped: Wrapper(Inner { value: 99 }),
    };
    let extractor =
        serde_evaluate::extractor::NestedFieldExtractor::new_from_path(&["wrapped", "value"])
            .unwrap();
    let result = extractor.evaluate(&data);
    assert_eq!(result, Ok(FieldScalarValue::I32(99)));
}

// =============================================================================
// Triple-nested Options
// =============================================================================

#[test]
fn test_triple_nested_option_all_some() {
    #[derive(Serialize)]
    struct Record {
        val: Option<Option<Option<u32>>>,
    }
    let record = Record {
        val: Some(Some(Some(42))),
    };
    let result = FieldExtractor::new("val").evaluate(&record);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(Some(Box::new(FieldScalarValue::Option(Some(Box::new(
                FieldScalarValue::U32(42)
            ))))))
        ))))
    );
}

#[test]
fn test_triple_nested_option_some_some_none() {
    #[derive(Serialize)]
    struct Record {
        val: Option<Option<Option<u32>>>,
    }
    let record = Record {
        val: Some(Some(None)),
    };
    let result = FieldExtractor::new("val").evaluate(&record);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(Some(Box::new(FieldScalarValue::Option(None))))
        ))))
    );
}

#[test]
fn test_triple_nested_option_some_none() {
    #[derive(Serialize)]
    struct Record {
        val: Option<Option<Option<u32>>>,
    }
    let record = Record { val: Some(None) };
    let result = FieldExtractor::new("val").evaluate(&record);
    assert_eq!(
        result,
        Ok(FieldScalarValue::Option(Some(Box::new(
            FieldScalarValue::Option(None)
        ))))
    );
}

#[test]
fn test_triple_nested_option_none() {
    #[derive(Serialize)]
    struct Record {
        val: Option<Option<Option<u32>>>,
    }
    let record = Record { val: None };
    let result = FieldExtractor::new("val").evaluate(&record);
    assert_eq!(result, Ok(FieldScalarValue::Option(None)));
}