tryparse 0.4.4

Multi-strategy parser for messy real-world data. Handles broken JSON, markdown wrappers, and type mismatches.
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
//! Flexible value types with metadata.

use std::hash::{Hash, Hasher};

use serde_json::Value;

/// Confidence reduction factor applied for each transformation.
/// Each transformation multiplies confidence by this value (0.95 = 5% reduction).
pub const CONFIDENCE_PENALTY_FACTOR: f32 = 0.95;

/// A flexible value that wraps a JSON value with parsing metadata.
///
/// This type tracks how the value was obtained and what transformations
/// were applied, which is useful for debugging and scoring candidates.
#[derive(Debug, Clone)]
pub struct FlexValue {
    /// The underlying JSON value.
    pub value: Value,
    /// Information about how this value was parsed.
    pub source: Source,
    /// List of transformations applied to get this value.
    transformations: Vec<Transformation>,
    /// Confidence score (0.0 - 1.0), higher is better.
    /// Starts at 1.0 and decreases with each transformation.
    confidence: f32,
    /// Maximum nesting depth where transformations occurred.
    ///
    /// This is used for recursive scoring - transformations at deeper
    /// levels are penalized more heavily (10x per level).
    max_transformation_depth: usize,
}

// Implement Hash and Eq based on the value only (for circular detection)
impl Hash for FlexValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Hash the JSON value structurally without string conversion
        hash_json_value(&self.value, state);
    }
}

/// Hash a JSON value recursively without string conversion.
///
/// This is more efficient than converting to string for large values.
fn hash_json_value<H: Hasher>(value: &Value, state: &mut H) {
    // Hash the discriminant first to distinguish different types
    std::mem::discriminant(value).hash(state);

    match value {
        Value::Null => {
            // Discriminant already hashed
        }
        Value::Bool(b) => {
            b.hash(state);
        }
        Value::Number(n) => {
            // Convert to canonical representation for hashing
            if let Some(i) = n.as_i64() {
                0u8.hash(state); // Mark as integer
                i.hash(state);
            } else if let Some(u) = n.as_u64() {
                1u8.hash(state); // Mark as unsigned
                u.hash(state);
            } else if let Some(f) = n.as_f64() {
                2u8.hash(state); // Mark as float
                                 // Convert float to bits for hashing
                f.to_bits().hash(state);
            }
        }
        Value::String(s) => {
            s.hash(state);
        }
        Value::Array(arr) => {
            arr.len().hash(state);
            for item in arr {
                hash_json_value(item, state);
            }
        }
        Value::Object(obj) => {
            obj.len().hash(state);
            // Hash entries in sorted order for consistency
            // (serde_json::Map preserves insertion order, but we want value equality)
            let mut keys: Vec<_> = obj.keys().collect();
            keys.sort();
            for key in keys {
                key.hash(state);
                hash_json_value(&obj[key], state);
            }
        }
    }
}

impl PartialEq for FlexValue {
    fn eq(&self, other: &Self) -> bool {
        // Compare based on JSON value only
        self.value == other.value
    }
}

impl Eq for FlexValue {}

impl FlexValue {
    /// Creates a new `FlexValue` with the given value and source.
    ///
    /// # Examples
    ///
    /// ```
    /// use tryparse::value::{FlexValue, Source};
    /// use serde_json::json;
    ///
    /// let value = FlexValue::new(json!({"name": "Alice"}), Source::Direct);
    /// assert_eq!(value.confidence(), 1.0);
    /// ```
    #[inline]
    pub fn new(value: Value, source: Source) -> Self {
        Self {
            value,
            source,
            transformations: Vec::new(),
            confidence: 1.0,
            max_transformation_depth: 0,
        }
    }

    /// Creates a new `FlexValue` from a fixed JSON string with repairs.
    #[inline]
    pub fn from_fixed_json(value: Value, fixes: Vec<JsonFix>) -> Self {
        Self {
            value,
            source: Source::Fixed { fixes },
            transformations: Vec::new(),
            confidence: 0.9, // Start lower for repaired JSON
            max_transformation_depth: 0,
        }
    }

    /// Adds a transformation to this value's history.
    ///
    /// This reduces the confidence score slightly.
    pub fn add_transformation(&mut self, trans: Transformation) {
        self.transformations.push(trans);
        self.confidence *= CONFIDENCE_PENALTY_FACTOR;
    }

    /// Adds a transformation with depth tracking for recursive scoring.
    ///
    /// The depth indicates the nesting level at which this transformation occurred.
    /// Deeper transformations are penalized more heavily in scoring (10x per level).
    pub fn add_transformation_at_depth(&mut self, trans: Transformation, depth: usize) {
        self.transformations.push(trans);
        self.confidence *= CONFIDENCE_PENALTY_FACTOR;
        self.max_transformation_depth = self.max_transformation_depth.max(depth);
    }

    /// Returns the maximum transformation depth.
    #[inline]
    pub const fn max_transformation_depth(&self) -> usize {
        self.max_transformation_depth
    }

    /// Returns the confidence score for this value.
    ///
    /// Higher values (closer to 1.0) indicate more confident parsing.
    #[inline]
    pub const fn confidence(&self) -> f32 {
        self.confidence
    }

    /// Returns a reference to the transformations applied.
    #[inline]
    pub fn transformations(&self) -> &[Transformation] {
        &self.transformations
    }

    /// Consumes self and returns the transformations.
    #[inline]
    pub fn into_transformations(self) -> Vec<Transformation> {
        self.transformations
    }

    /// Returns a JSON representation of the transformation history and metadata.
    ///
    /// This provides a human-readable explanation of how the value was parsed
    /// and what transformations were applied.
    ///
    /// # Examples
    ///
    /// ```
    /// use tryparse::value::{FlexValue, Source, Transformation};
    /// use serde_json::json;
    ///
    /// let mut value = FlexValue::new(json!(42), Source::Direct);
    /// value.add_transformation(Transformation::StringToNumber {
    ///     original: "42".to_string(),
    /// });
    ///
    /// let explanation = value.explanation_json();
    /// assert!(explanation["transformations"].is_array());
    /// assert!(explanation["score"].is_number());
    /// ```
    pub fn explanation_json(&self) -> Value {
        use serde_json::json;

        // Convert source to JSON
        let source_json = match &self.source {
            Source::Direct => json!({"type": "direct"}),
            Source::Markdown { lang } => json!({
                "type": "markdown",
                "language": lang,
            }),
            Source::Fixed { fixes } => json!({
                "type": "fixed",
                "fixes": fixes.iter().map(|f| f.description()).collect::<Vec<_>>(),
            }),
            Source::MultiJson { index } => json!({
                "type": "multi_json",
                "index": index,
            }),
            Source::MultiJsonArray => json!({"type": "multi_json_array"}),
            Source::Heuristic { pattern } => json!({
                "type": "heuristic",
                "pattern": pattern,
            }),
            Source::Yaml => json!({"type": "yaml"}),
        };

        // Convert transformations to JSON
        let transformations_json: Vec<Value> = self
            .transformations
            .iter()
            .map(transformation_to_json)
            .collect();

        // Calculate score
        let score = crate::scoring::score_candidate(self);

        json!({
            "source": source_json,
            "confidence": self.confidence,
            "score": score,
            "transformations": transformations_json,
            "transformation_count": self.transformations.len(),
            "max_transformation_depth": self.max_transformation_depth,
        })
    }
}

/// Helper function to convert a Transformation to JSON.
fn transformation_to_json(t: &Transformation) -> Value {
    use serde_json::json;

    match t {
        Transformation::ExtractedFromMarkdown => json!({
            "type": "extracted_from_markdown",
            "penalty": t.penalty(),
        }),
        Transformation::JsonRepaired { fixes } => json!({
            "type": "json_repaired",
            "fixes": fixes.iter().map(|f| f.description()).collect::<Vec<_>>(),
            "penalty": t.penalty(),
        }),
        Transformation::StringToNumber { original } => json!({
            "type": "string_to_number",
            "original": original,
            "penalty": t.penalty(),
        }),
        Transformation::FloatToInt { original } => json!({
            "type": "float_to_int",
            "original": original,
            "penalty": t.penalty(),
        }),
        Transformation::SingleToArray => json!({
            "type": "single_to_array",
            "penalty": t.penalty(),
        }),
        Transformation::FieldNameCaseChanged { from, to } => json!({
            "type": "field_name_case_changed",
            "from": from,
            "to": to,
            "penalty": t.penalty(),
        }),
        Transformation::DefaultValueInserted { field } => json!({
            "type": "default_value_inserted",
            "field": field,
            "penalty": t.penalty(),
        }),
        Transformation::ExtraKey { key } => json!({
            "type": "extra_key",
            "key": key,
            "penalty": t.penalty(),
        }),
        Transformation::ImpliedKey { field } => json!({
            "type": "implied_key",
            "field": field,
            "penalty": t.penalty(),
        }),
        Transformation::ObjectFromMarkdown { score } => json!({
            "type": "object_from_markdown",
            "score": score,
            "penalty": t.penalty(),
        }),
        Transformation::ArrayItemParseError { index, error } => json!({
            "type": "array_item_parse_error",
            "index": index,
            "error": error,
            "penalty": t.penalty(),
        }),
        Transformation::JsonToString { original } => json!({
            "type": "json_to_string",
            "original": original,
            "penalty": t.penalty(),
        }),
        Transformation::ConstraintChecked {
            name,
            passed,
            is_assert,
        } => json!({
            "type": "constraint_checked",
            "name": name,
            "passed": passed,
            "is_assert": is_assert,
            "penalty": t.penalty(),
        }),
        Transformation::DefaultButHadUnparseableValue {
            field,
            value,
            error,
        } => json!({
            "type": "default_but_had_unparseable_value",
            "field": field,
            "value": value,
            "error": error,
            "penalty": t.penalty(),
        }),
        Transformation::SubstringMatch { original, target } => json!({
            "type": "substring_match",
            "original": original,
            "target": target,
            "penalty": t.penalty(),
        }),
        Transformation::StrippedNonAlphaNumeric { original, stripped } => json!({
            "type": "stripped_non_alphanumeric",
            "original": original,
            "stripped": stripped,
            "penalty": t.penalty(),
        }),
        Transformation::UnionMatch { index, candidates } => json!({
            "type": "union_match",
            "index": index,
            "candidates": candidates,
            "penalty": t.penalty(),
        }),
        Transformation::FirstMatch { index, total } => json!({
            "type": "first_match",
            "index": index,
            "total": total,
            "penalty": t.penalty(),
        }),
    }
}

/// Information about how a value was parsed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
    /// Parsed directly as valid JSON.
    Direct,

    /// Extracted from a markdown code block.
    Markdown {
        /// The language tag (e.g., "json"), if any.
        lang: Option<String>,
    },

    /// Parsed after fixing/repairing the JSON.
    Fixed {
        /// List of fixes that were applied.
        fixes: Vec<JsonFix>,
    },

    /// One of multiple JSON objects found.
    MultiJson {
        /// Index of this object in the sequence.
        index: usize,
    },

    /// All JSON objects combined into an array.
    MultiJsonArray,

    /// Extracted using heuristic pattern matching.
    Heuristic {
        /// Description of the pattern used.
        pattern: String,
    },

    /// Parsed from YAML and converted to JSON.
    Yaml,
}

/// Types of JSON fixes that can be applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum JsonFix {
    /// Added quotes around unquoted object keys.
    UnquotedKeys,
    /// Removed trailing commas.
    TrailingCommas,
    /// Converted single quotes to double quotes.
    SingleQuotes,
    /// Added missing commas between items.
    MissingCommas,
    /// Closed unclosed braces or brackets.
    UnclosedBraces,
    /// Removed comments from JSON.
    Comments,
    /// Normalized Unicode smart/curly quotes.
    SmartQuotes,
    /// Normalized field names to snake_case.
    FieldNormalization,
    /// Unescaped double-escaped JSON string.
    DoubleEscaped,
    /// Converted template literals (backticks) to quotes.
    TemplateLiterals,
    /// Converted hex numbers to decimal.
    HexNumbers,
    /// Escaped unescaped newlines in strings.
    UnescapedNewlines,
    /// Removed JavaScript function definitions.
    JavaScriptFunctions,
    /// Converted Python triple-quoted strings to regular quoted strings.
    TripleQuotedStrings,
    /// Added quotes around unquoted values.
    UnquotedValues,
}

impl JsonFix {
    /// Returns the penalty score for this fix type.
    ///
    /// Lower penalties are better. Some fixes are more reliable than others.
    pub const fn penalty(&self) -> u32 {
        match self {
            // Low-risk fixes (definitely correct)
            Self::TrailingCommas => 1,
            Self::Comments => 1,
            Self::SmartQuotes => 1,
            Self::DoubleEscaped => 1,
            Self::TemplateLiterals => 1,
            Self::UnescapedNewlines => 1,
            Self::JavaScriptFunctions => 1,

            // Medium-risk fixes (usually correct)
            Self::SingleQuotes => 2,
            Self::UnquotedKeys => 2,
            Self::HexNumbers => 2,
            Self::TripleQuotedStrings => 2,
            Self::MissingCommas => 3,
            Self::UnclosedBraces => 3,

            // Higher-risk fixes (can cause issues)
            Self::UnquotedValues => 5, // Can wrap already-quoted strings incorrectly
            Self::FieldNormalization => 4,
        }
    }

    /// Returns a human-readable description of this fix.
    pub const fn description(self) -> &'static str {
        match self {
            Self::UnquotedKeys => "added quotes around object keys",
            Self::TrailingCommas => "removed trailing commas",
            Self::SingleQuotes => "converted single quotes to double quotes",
            Self::MissingCommas => "added missing commas",
            Self::UnclosedBraces => "closed unclosed braces/brackets",
            Self::Comments => "removed comments",
            Self::SmartQuotes => "normalized smart/curly quotes",
            Self::FieldNormalization => "normalized field names to snake_case",
            Self::DoubleEscaped => "unescaped double-escaped JSON",
            Self::TemplateLiterals => "converted template literals (backticks) to quotes",
            Self::HexNumbers => "converted hex numbers to decimal",
            Self::UnescapedNewlines => "escaped unescaped newlines in strings",
            Self::JavaScriptFunctions => "removed JavaScript function definitions",
            Self::TripleQuotedStrings => "converted triple-quoted strings to regular quotes",
            Self::UnquotedValues => "added quotes around unquoted values",
        }
    }
}

/// Transformations applied during parsing or deserialization.
#[derive(Debug, Clone, PartialEq)]
pub enum Transformation {
    /// Extracted value from markdown code block.
    ExtractedFromMarkdown,

    /// JSON was repaired before parsing.
    JsonRepaired {
        /// The fixes that were applied.
        fixes: Vec<JsonFix>,
    },

    /// String was converted to a number.
    StringToNumber {
        /// The original string value.
        original: String,
    },

    /// Float was rounded to an integer.
    FloatToInt {
        /// The original float value.
        original: f64,
    },

    /// Single value was wrapped in an array.
    SingleToArray,

    /// Field name case was changed to match struct field.
    FieldNameCaseChanged {
        /// Original field name from JSON.
        from: String,
        /// Target field name in struct.
        to: String,
    },

    /// Default value was inserted for missing field.
    DefaultValueInserted {
        /// Name of the field that got a default.
        field: String,
    },

    /// Extra key found in object that doesn't match any struct field.
    ExtraKey {
        /// The extra key name.
        key: String,
    },

    /// Entire object was coerced into a single struct field (implicit key).
    ImpliedKey {
        /// The field that received the object.
        field: String,
    },

    /// Object was extracted from markdown code block.
    ///
    /// This is tracked separately from ExtractedFromMarkdown to specifically
    /// identify objects that were parsed from markdown-wrapped JSON.
    ObjectFromMarkdown {
        /// Score penalty for this markdown extraction.
        score: i32,
    },

    /// Array item failed to parse.
    ///
    /// This transformation tracks array items that couldn't be deserialized
    /// and were skipped. The index indicates the position in the array.
    ArrayItemParseError {
        /// Index of the item that failed.
        index: usize,
        /// Error message describing the failure.
        error: String,
    },

    /// Object/Array was converted to a string representation.
    ///
    /// This transformation indicates that a composite type (object or array)
    /// was converted to its string representation, which is generally
    /// undesirable in union resolution.
    JsonToString {
        /// The original JSON value that was converted.
        original: String,
    },

    /// Constraint was validated during deserialization.
    ///
    /// This tracks both passing and failing constraints to provide
    /// visibility into validation that occurred.
    ConstraintChecked {
        /// Name of the constraint that was checked.
        name: String,
        /// Whether the constraint passed.
        passed: bool,
        /// Whether this was an assert (fails deserialization) or check (just tracked).
        is_assert: bool,
    },

    /// Default value used despite having an unparseable value.
    ///
    /// This indicates a field had a value that couldn't be parsed,
    /// so a default was used instead. More expensive than DefaultValueInserted
    /// because we had data but couldn't use it.
    DefaultButHadUnparseableValue {
        /// The field name.
        field: String,
        /// The unparseable value.
        value: String,
        /// Error message from parsing attempt.
        error: String,
    },

    /// Substring match was used for enum or string matching.
    ///
    /// Instead of exact match, a substring was found.
    SubstringMatch {
        /// The original string that was matched.
        original: String,
        /// The target that was matched against.
        target: String,
    },

    /// Non-alphanumeric characters were stripped for matching.
    ///
    /// Punctuation and special characters were removed to find a match.
    StrippedNonAlphaNumeric {
        /// The original string before stripping.
        original: String,
        /// The stripped string that matched.
        stripped: String,
    },

    /// Union variant was selected from multiple candidates.
    ///
    /// This tracks which variant won in a union type resolution.
    UnionMatch {
        /// Index of the winning variant.
        index: usize,
        /// Names of all candidate types.
        candidates: Vec<String>,
    },

    /// First match was selected from multiple options.
    ///
    /// When multiple candidates succeeded, the first one was chosen.
    /// This is used for array-to-struct coercion.
    FirstMatch {
        /// Index of the selected option.
        index: usize,
        /// Total number of candidates.
        total: usize,
    },
}

impl Transformation {
    /// Returns a penalty score for this transformation.
    ///
    /// Higher scores indicate less desirable transformations.
    #[inline]
    pub const fn penalty(&self) -> u32 {
        match self {
            Self::ExtractedFromMarkdown => 0, // Free, already in source
            Self::JsonRepaired { .. } => 0,   // Free, already in source
            Self::StringToNumber { .. } => 2,
            Self::FloatToInt { .. } => 3,
            Self::SingleToArray => 5,
            Self::FieldNameCaseChanged { .. } => 4,
            Self::DefaultValueInserted { .. } => 50, // Very expensive
            Self::ExtraKey { .. } => 10,             // Moderate penalty
            Self::ImpliedKey { .. } => 8,            // Moderate penalty
            Self::ObjectFromMarkdown { score } => {
                // Dynamic penalty based on score
                if *score >= 0 {
                    *score as u32
                } else {
                    0
                }
            }
            Self::ArrayItemParseError { index, .. } => {
                // Penalty increases with index (deeper errors are worse)
                1 + (*index as u32)
            }
            Self::JsonToString { .. } => 2, // Moderate penalty for type conversion
            Self::ConstraintChecked {
                passed, is_assert, ..
            } => {
                // Failed asserts are very expensive
                // Failed checks are moderate penalty
                // Passing constraints are free
                match (passed, is_assert) {
                    (false, true) => 100, // Failed assert - very expensive
                    (false, false) => 10, // Failed check - moderate penalty
                    (true, _) => 0,       // Passing constraint - free
                }
            }
            Self::DefaultButHadUnparseableValue { .. } => 2, // Had value but couldn't parse
            Self::SubstringMatch { .. } => 2,                // Fuzzy matching
            Self::StrippedNonAlphaNumeric { .. } => 3,       // More aggressive fuzzy matching
            Self::UnionMatch { .. } => 0,                    // Just tracking, not a penalty
            Self::FirstMatch { .. } => 1,                    // Slight penalty for array-to-struct
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use serde_json::json;

    use super::*;

    #[test]
    fn test_flex_value_new() {
        let value = FlexValue::new(json!({"test": 1}), Source::Direct);
        assert_eq!(value.confidence(), 1.0);
        assert!(value.transformations().is_empty());
    }

    #[test]
    fn test_flex_value_hash_consistency() {
        use std::hash::{DefaultHasher, Hasher};

        // Helper to compute hash
        fn compute_hash(value: &FlexValue) -> u64 {
            let mut hasher = DefaultHasher::new();
            value.hash(&mut hasher);
            hasher.finish()
        }

        // Equal values should have equal hashes
        let v1 = FlexValue::new(json!({"a": 1, "b": 2}), Source::Direct);
        let v2 = FlexValue::new(json!({"a": 1, "b": 2}), Source::Direct);
        assert_eq!(compute_hash(&v1), compute_hash(&v2));

        // Different values should have different hashes
        let v3 = FlexValue::new(json!({"a": 1, "b": 3}), Source::Direct);
        assert_ne!(compute_hash(&v1), compute_hash(&v3));

        // Different types should have different hashes
        let v4 = FlexValue::new(json!([1, 2]), Source::Direct);
        assert_ne!(compute_hash(&v1), compute_hash(&v4));
    }

    #[test]
    fn test_flex_value_hash_with_hashset() {
        let mut set = HashSet::new();

        let v1 = FlexValue::new(json!({"name": "test", "value": 42}), Source::Direct);
        let v2 = FlexValue::new(json!({"name": "test", "value": 42}), Source::Direct);
        let v3 = FlexValue::new(json!({"name": "other", "value": 42}), Source::Direct);

        // Insert first value
        assert!(set.insert(v1.clone()));

        // Inserting equal value should return false (already exists)
        assert!(!set.insert(v2));

        // Inserting different value should succeed
        assert!(set.insert(v3));

        assert_eq!(set.len(), 2);
    }

    #[test]
    fn test_flex_value_hash_nested_objects() {
        use std::hash::{DefaultHasher, Hasher};

        fn compute_hash(value: &FlexValue) -> u64 {
            let mut hasher = DefaultHasher::new();
            value.hash(&mut hasher);
            hasher.finish()
        }

        // Deeply nested objects should hash consistently
        let nested = json!({
            "level1": {
                "level2": {
                    "level3": [1, 2, 3]
                }
            }
        });
        let v1 = FlexValue::new(nested.clone(), Source::Direct);
        let v2 = FlexValue::new(nested, Source::Direct);
        assert_eq!(compute_hash(&v1), compute_hash(&v2));
    }

    #[test]
    fn test_flex_value_hash_floats() {
        use std::hash::{DefaultHasher, Hasher};

        fn compute_hash(value: &FlexValue) -> u64 {
            let mut hasher = DefaultHasher::new();
            value.hash(&mut hasher);
            hasher.finish()
        }

        // Floats should hash by their bit representation
        let v1 = FlexValue::new(json!(1.234), Source::Direct);
        let v2 = FlexValue::new(json!(1.234), Source::Direct);
        let v3 = FlexValue::new(json!(1.235), Source::Direct);

        assert_eq!(compute_hash(&v1), compute_hash(&v2));
        assert_ne!(compute_hash(&v1), compute_hash(&v3));
    }

    #[test]
    fn test_flex_value_hash_all_types() {
        use std::hash::{DefaultHasher, Hasher};

        fn compute_hash(value: &FlexValue) -> u64 {
            let mut hasher = DefaultHasher::new();
            value.hash(&mut hasher);
            hasher.finish()
        }

        // Test all JSON types produce different hashes
        let null = FlexValue::new(json!(null), Source::Direct);
        let bool_val = FlexValue::new(json!(true), Source::Direct);
        let num = FlexValue::new(json!(42), Source::Direct);
        let string = FlexValue::new(json!("test"), Source::Direct);
        let array = FlexValue::new(json!([1, 2]), Source::Direct);
        let object = FlexValue::new(json!({"a": 1}), Source::Direct);

        let hashes = [
            compute_hash(&null),
            compute_hash(&bool_val),
            compute_hash(&num),
            compute_hash(&string),
            compute_hash(&array),
            compute_hash(&object),
        ];

        // All hashes should be unique
        let unique: HashSet<_> = hashes.iter().collect();
        assert_eq!(
            unique.len(),
            hashes.len(),
            "All types should have unique hashes"
        );
    }

    #[test]
    fn test_flex_value_transformation() {
        let mut value = FlexValue::new(json!(42), Source::Direct);
        assert_eq!(value.confidence(), 1.0);

        value.add_transformation(Transformation::StringToNumber {
            original: "42".to_string(),
        });

        assert_eq!(value.confidence(), 0.95);
        assert_eq!(value.transformations().len(), 1);
    }

    #[test]
    fn test_transformation_penalty() {
        assert_eq!(
            Transformation::StringToNumber {
                original: "42".into()
            }
            .penalty(),
            2
        );
        assert_eq!(Transformation::FloatToInt { original: 42.5 }.penalty(), 3);
        assert_eq!(
            Transformation::DefaultValueInserted {
                field: "test".into()
            }
            .penalty(),
            50
        );
    }

    #[test]
    fn test_json_fix_description() {
        assert!(JsonFix::UnquotedKeys.description().contains("quotes"));
        assert!(JsonFix::TrailingCommas.description().contains("trailing"));
    }

    #[test]
    fn test_source_equality() {
        assert_eq!(Source::Direct, Source::Direct);
        assert_ne!(Source::Direct, Source::MultiJsonArray);
    }
}