tessellate-core 0.3.0

Compiler and deterministic runtime for the Tess rule language
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
use chrono::NaiveDate;
use num_bigint::BigInt;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::BTreeSet;
use std::error::Error;
use std::fmt;

/// A runtime value produced by a Tess expression.
///
/// Variant order is part of the stable ordering used in diagnostics and JSON
/// output. Decimal values are normalized whenever they enter through the
/// constructors in this module, so numerically equal values also render alike.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum Value {
    Bool(bool),
    Int(i64),
    Decimal(#[serde(with = "exact_decimal_serde")] Decimal),
    String(String),
    Date(NaiveDate),
    /// An exact, signed number of seconds.
    Duration(i64),
    Enum {
        type_name: String,
        variant: String,
    },
    EntityRef {
        type_name: String,
        id: String,
    },
}

impl Value {
    #[must_use]
    pub fn decimal(value: Decimal) -> Self {
        Self::Decimal(value.normalize())
    }

    #[must_use]
    pub const fn kind_name(&self) -> &'static str {
        match self {
            Self::Bool(_) => "Bool",
            Self::Int(_) => "Int",
            Self::Decimal(_) => "Decimal",
            Self::String(_) => "String",
            Self::Date(_) => "Date",
            Self::Duration(_) => "Duration",
            Self::Enum { .. } => "Enum",
            Self::EntityRef { .. } => "Entity",
        }
    }

    #[must_use]
    pub const fn as_bool(&self) -> Option<bool> {
        if let Self::Bool(value) = self {
            Some(*value)
        } else {
            None
        }
    }

    #[must_use]
    pub const fn as_int(&self) -> Option<i64> {
        if let Self::Int(value) = self {
            Some(*value)
        } else {
            None
        }
    }

    #[must_use]
    pub const fn as_decimal(&self) -> Option<Decimal> {
        if let Self::Decimal(value) = self {
            Some(*value)
        } else {
            None
        }
    }

    #[must_use]
    pub const fn as_date(&self) -> Option<NaiveDate> {
        if let Self::Date(value) = self {
            Some(*value)
        } else {
            None
        }
    }

    #[must_use]
    pub const fn as_duration_seconds(&self) -> Option<i64> {
        if let Self::Duration(value) = self {
            Some(*value)
        } else {
            None
        }
    }

    /// Lossless, self-describing JSON representation.
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the configured JSON serializer cannot
    /// represent a value.
    pub fn to_json(&self) -> Result<JsonValue, serde_json::Error> {
        serde_json::to_value(self)
    }

    /// Decodes the lossless, self-describing representation from [`to_json`](Self::to_json).
    ///
    /// # Errors
    ///
    /// Returns [`ValueConversionError::InvalidTaggedValue`] for malformed or
    /// unsupported tagged values.
    pub fn from_json(value: JsonValue) -> Result<Self, ValueConversionError> {
        serde_json::from_value(value)
            .map(Self::normalized)
            .map_err(|error| ValueConversionError::InvalidTaggedValue(error.to_string()))
    }

    /// JSON representation used when a surrounding schema already supplies the type.
    #[must_use]
    pub fn to_plain_json(&self) -> JsonValue {
        match self {
            Self::Bool(value) => JsonValue::Bool(*value),
            Self::Int(value) | Self::Duration(value) => JsonValue::Number((*value).into()),
            // A string keeps the exact schema independent of downstream JSON number parsers.
            Self::Decimal(value) => JsonValue::String(value.normalize().to_string()),
            Self::String(value) | Self::EntityRef { id: value, .. } => {
                JsonValue::String(value.clone())
            }
            Self::Date(value) => JsonValue::String(value.format("%Y-%m-%d").to_string()),
            Self::Enum { variant, .. } => JsonValue::String(variant.clone()),
        }
    }

    /// Decodes conventional JSON using an expected Tess value kind.
    ///
    /// # Errors
    ///
    /// Returns a type or literal conversion error when `value` does not match
    /// `expected`.
    pub fn from_plain_json(
        value: &JsonValue,
        expected: &ValueKind,
    ) -> Result<Self, ValueConversionError> {
        match expected {
            ValueKind::Bool => value
                .as_bool()
                .map(Self::Bool)
                .ok_or_else(|| type_mismatch(expected, value)),
            ValueKind::Int => value
                .as_i64()
                .map(Self::Int)
                .ok_or_else(|| type_mismatch(expected, value)),
            ValueKind::Decimal => {
                let text = match value {
                    JsonValue::Number(number) => number.to_string(),
                    JsonValue::String(text) => text.clone(),
                    _ => return Err(type_mismatch(expected, value)),
                };
                parse_decimal(&text)
                    .map(Self::Decimal)
                    .map_err(|error| ValueConversionError::InvalidDecimal(error.to_string()))
            }
            ValueKind::String => value
                .as_str()
                .map(|value| Self::String(value.to_owned()))
                .ok_or_else(|| type_mismatch(expected, value)),
            ValueKind::Date => {
                let text = value
                    .as_str()
                    .ok_or_else(|| type_mismatch(expected, value))?;
                NaiveDate::parse_from_str(text, "%Y-%m-%d")
                    .map(Self::Date)
                    .map_err(|_| ValueConversionError::InvalidDate(text.to_owned()))
            }
            ValueKind::Duration => value
                .as_i64()
                .map(Self::Duration)
                .ok_or_else(|| type_mismatch(expected, value)),
            ValueKind::Enum(type_name) => value
                .as_str()
                .map(|variant| Self::Enum {
                    type_name: type_name.clone(),
                    variant: variant.to_owned(),
                })
                .ok_or_else(|| type_mismatch(expected, value)),
            ValueKind::EntityRef(type_name) => value
                .as_str()
                .map(|id| Self::EntityRef {
                    type_name: type_name.clone(),
                    id: id.to_owned(),
                })
                .ok_or_else(|| type_mismatch(expected, value)),
        }
    }

    fn normalized(self) -> Self {
        match self {
            Self::Decimal(value) => Self::Decimal(value.normalize()),
            other => other,
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bool(value) => value.fmt(formatter),
            Self::Int(value) => formatter.write_str(&group_integer_digits(&value.to_string())),
            Self::Decimal(value) => {
                let normalized = value.normalize().to_string();
                let (integer, fraction) = normalized.split_once('.').unwrap_or((&normalized, ""));
                formatter.write_str(&group_integer_digits(integer))?;
                if !fraction.is_empty() {
                    write!(formatter, ".{fraction}")?;
                }
                Ok(())
            }
            Self::String(value) => write!(formatter, "{value:?}"),
            Self::Date(value) => write!(formatter, "{}", value.format("%Y-%m-%d")),
            Self::Duration(seconds) => {
                write!(formatter, "{}s", group_integer_digits(&seconds.to_string()))
            }
            Self::Enum { type_name, variant } => write!(formatter, "{type_name}::{variant}"),
            Self::EntityRef { type_name, id } => write!(formatter, "{type_name}#{id}"),
        }
    }
}

pub(crate) fn group_integer_digits(value: &str) -> String {
    let (sign, digits) = value
        .strip_prefix('-')
        .map_or(("", value), |digits| ("-", digits));
    let separator_count = digits.len().saturating_sub(1) / 3;
    let mut grouped = String::with_capacity(value.len() + separator_count);
    grouped.push_str(sign);
    for (index, digit) in digits.chars().enumerate() {
        if index != 0 && (digits.len() - index) % 3 == 0 {
            grouped.push('_');
        }
        grouped.push(digit);
    }
    grouped
}

/// Type information needed to decode untagged, schema-directed JSON values.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(tag = "kind", content = "name", rename_all = "snake_case")]
pub enum ValueKind {
    Bool,
    Int,
    Decimal,
    String,
    Date,
    Duration,
    Enum(String),
    EntityRef(String),
}

impl fmt::Display for ValueKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bool => formatter.write_str("Bool"),
            Self::Int => formatter.write_str("Int"),
            Self::Decimal => formatter.write_str("Decimal"),
            Self::String => formatter.write_str("String"),
            Self::Date => formatter.write_str("Date"),
            Self::Duration => formatter.write_str("Duration"),
            Self::Enum(name) | Self::EntityRef(name) => formatter.write_str(name),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ValueConversionError {
    TypeMismatch {
        expected: ValueKind,
        found: &'static str,
    },
    InvalidDecimal(String),
    InvalidDate(String),
    InvalidTaggedValue(String),
}

impl fmt::Display for ValueConversionError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TypeMismatch { expected, found } => {
                write!(formatter, "expected {expected}, found JSON {found}")
            }
            Self::InvalidDecimal(message) => write!(formatter, "invalid Decimal: {message}"),
            Self::InvalidDate(value) => {
                write!(formatter, "invalid Date `{value}`; expected YYYY-MM-DD")
            }
            Self::InvalidTaggedValue(message) => {
                write!(formatter, "invalid tagged Tess value: {message}")
            }
        }
    }
}

impl Error for ValueConversionError {}

fn type_mismatch(expected: &ValueKind, value: &JsonValue) -> ValueConversionError {
    let found = match value {
        JsonValue::Null => "null",
        JsonValue::Bool(_) => "boolean",
        JsonValue::Number(_) => "number",
        JsonValue::String(_) => "string",
        JsonValue::Array(_) => "array",
        JsonValue::Object(_) => "object",
    };
    ValueConversionError::TypeMismatch {
        expected: expected.clone(),
        found,
    }
}

/// Four-valued truth used by Tess's open-world, paraconsistent logic.
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum TruthValue {
    True,
    False,
    #[default]
    Unknown,
    Conflict,
}

impl TruthValue {
    #[must_use]
    pub const fn not(self) -> Self {
        match self {
            Self::True => Self::False,
            Self::False => Self::True,
            Self::Unknown => Self::Unknown,
            Self::Conflict => Self::Conflict,
        }
    }

    /// Conflict-preserving conjunction.
    ///
    /// Dominance order: `false > conflict > unknown > true`.
    #[must_use]
    pub const fn and(self, other: Self) -> Self {
        if matches!(self, Self::False) || matches!(other, Self::False) {
            Self::False
        } else if matches!(self, Self::Conflict) || matches!(other, Self::Conflict) {
            Self::Conflict
        } else if matches!(self, Self::Unknown) || matches!(other, Self::Unknown) {
            Self::Unknown
        } else {
            Self::True
        }
    }

    /// Conflict-preserving disjunction.
    ///
    /// Dominance order: `true > conflict > unknown > false`.
    #[must_use]
    pub const fn or(self, other: Self) -> Self {
        if matches!(self, Self::True) || matches!(other, Self::True) {
            Self::True
        } else if matches!(self, Self::Conflict) || matches!(other, Self::Conflict) {
            Self::Conflict
        } else if matches!(self, Self::Unknown) || matches!(other, Self::Unknown) {
            Self::Unknown
        } else {
            Self::False
        }
    }

    #[must_use]
    pub const fn is_true(self) -> bool {
        matches!(self, Self::True)
    }

    #[must_use]
    pub const fn is_false(self) -> bool {
        matches!(self, Self::False)
    }

    #[must_use]
    pub const fn is_known(self) -> bool {
        matches!(self, Self::True | Self::False)
    }
}

impl From<bool> for TruthValue {
    fn from(value: bool) -> Self {
        if value { Self::True } else { Self::False }
    }
}

impl std::ops::Not for TruthValue {
    type Output = Self;

    fn not(self) -> Self::Output {
        self.not()
    }
}

impl std::ops::BitAnd for TruthValue {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.and(rhs)
    }
}

impl std::ops::BitOr for TruthValue {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        self.or(rhs)
    }
}

impl fmt::Display for TruthValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::True => "true",
            Self::False => "false",
            Self::Unknown => "unknown",
            Self::Conflict => "conflict",
        })
    }
}

/// A value together with open-world evaluation state.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "status", content = "value", rename_all = "snake_case")]
pub enum EvalValue {
    Known(Value),
    Unknown {
        #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
        missing: BTreeSet<String>,
    },
    Conflict {
        reasons: Vec<String>,
    },
}

impl EvalValue {
    #[must_use]
    pub const fn known(value: Value) -> Self {
        Self::Known(value)
    }

    #[must_use]
    pub fn unknown(missing: impl IntoIterator<Item = String>) -> Self {
        Self::Unknown {
            missing: missing.into_iter().collect(),
        }
    }

    #[must_use]
    pub fn missing(name: impl Into<String>) -> Self {
        Self::Unknown {
            missing: BTreeSet::from([name.into()]),
        }
    }

    #[must_use]
    pub fn conflict(reasons: impl IntoIterator<Item = String>) -> Self {
        let mut reasons = reasons.into_iter().collect::<Vec<_>>();
        reasons.sort();
        reasons.dedup();
        Self::Conflict { reasons }
    }

    #[must_use]
    pub const fn as_known(&self) -> Option<&Value> {
        if let Self::Known(value) = self {
            Some(value)
        } else {
            None
        }
    }

    #[must_use]
    pub fn into_known(self) -> Option<Value> {
        if let Self::Known(value) = self {
            Some(value)
        } else {
            None
        }
    }

    #[must_use]
    pub fn truth_value(&self) -> Option<TruthValue> {
        match self {
            Self::Known(Value::Bool(value)) => Some((*value).into()),
            Self::Unknown { .. } => Some(TruthValue::Unknown),
            Self::Conflict { .. } => Some(TruthValue::Conflict),
            Self::Known(_) => None,
        }
    }

    #[must_use]
    pub fn map(self, map: impl FnOnce(Value) -> Value) -> Self {
        match self {
            Self::Known(value) => Self::known(map(value)),
            Self::Unknown { missing } => Self::Unknown { missing },
            Self::Conflict { reasons } => Self::Conflict { reasons },
        }
    }
}

impl From<Value> for EvalValue {
    fn from(value: Value) -> Self {
        Self::known(value)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecimalError {
    Invalid(String),
    Overflow { operation: &'static str },
    DivisionByZero,
    Inexact { operation: &'static str },
}

impl fmt::Display for DecimalError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid(value) => write!(formatter, "invalid decimal literal `{value}`"),
            Self::Overflow { operation } => {
                write!(formatter, "decimal overflow during {operation}")
            }
            Self::DivisionByZero => formatter.write_str("decimal division by zero"),
            Self::Inexact { operation } => write!(
                formatter,
                "decimal {operation} is not exactly representable; explicit rounding is required"
            ),
        }
    }
}

impl Error for DecimalError {}

mod exact_decimal_serde {
    use super::{Decimal, parse_decimal};
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(value: &Decimal, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&value.to_string())
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
    where
        D: Deserializer<'de>,
    {
        let text = String::deserialize(deserializer)?;
        parse_decimal(&text).map_err(serde::de::Error::custom)
    }
}

/// Parses and normalizes an exact base-10 decimal literal.
///
/// # Errors
///
/// Returns [`DecimalError::Invalid`] if `text` is not representable as a
/// `rust_decimal::Decimal`.
pub fn parse_decimal(text: &str) -> Result<Decimal, DecimalError> {
    let expanded = if text.contains(['e', 'E']) {
        expand_scientific_decimal(text).ok_or_else(|| DecimalError::Invalid(text.to_owned()))?
    } else {
        text.to_owned()
    };
    Decimal::from_str_exact(&expanded)
        .map(|value| value.normalize())
        .map_err(|_| DecimalError::Invalid(text.to_owned()))
}

fn expand_scientific_decimal(text: &str) -> Option<String> {
    let (negative, unsigned) = if let Some(value) = text.strip_prefix('-') {
        (true, value)
    } else if let Some(value) = text.strip_prefix('+') {
        (false, value)
    } else {
        (false, text)
    };
    let mut parts = unsigned.split(['e', 'E']);
    let significand = parts.next()?;
    let exponent = parts.next()?.parse::<i32>().ok()?;
    if parts.next().is_some() {
        return None;
    }

    let mut digits = String::new();
    let mut decimal_index = None;
    for character in significand.chars() {
        match character {
            '0'..='9' => digits.push(character),
            '_' => {}
            '.' if decimal_index.is_none() => decimal_index = Some(digits.len()),
            _ => return None,
        }
    }
    if digits.is_empty() {
        return None;
    }
    let decimal_index = decimal_index.unwrap_or(digits.len());
    let shifted = i64::try_from(decimal_index).ok()? + i64::from(exponent);
    let digit_count = i64::try_from(digits.len()).ok()?;
    let padding = if shifted < 0 {
        shifted.unsigned_abs()
    } else if shifted > digit_count {
        (shifted - digit_count).unsigned_abs()
    } else {
        0
    };
    if padding > 128 {
        return None;
    }

    let mut expanded = String::new();
    if negative {
        expanded.push('-');
    }
    if shifted <= 0 {
        expanded.push_str("0.");
        expanded.extend(std::iter::repeat_n('0', usize::try_from(-shifted).ok()?));
        expanded.push_str(&digits);
    } else if shifted >= digit_count {
        expanded.push_str(&digits);
        expanded.extend(std::iter::repeat_n(
            '0',
            usize::try_from(shifted - digit_count).ok()?,
        ));
    } else {
        let split = usize::try_from(shifted).ok()?;
        expanded.push_str(&digits[..split]);
        expanded.push('.');
        expanded.push_str(&digits[split..]);
    }
    Some(expanded)
}

fn aligned_decimal_mantissa(value: Decimal, scale: u32) -> BigInt {
    let factor = BigInt::from(10_u8).pow(scale - value.scale());
    BigInt::from(value.mantissa()) * factor
}

fn decimal_sum_is_exact(result: Decimal, left: Decimal, right: Decimal, subtract: bool) -> bool {
    let scale = result.scale().max(left.scale()).max(right.scale());
    let expected = if subtract {
        aligned_decimal_mantissa(left, scale) - aligned_decimal_mantissa(right, scale)
    } else {
        aligned_decimal_mantissa(left, scale) + aligned_decimal_mantissa(right, scale)
    };
    aligned_decimal_mantissa(result, scale) == expected
}

fn decimal_product_is_exact(result: Decimal, left: Decimal, right: Decimal) -> bool {
    let left_side =
        BigInt::from(result.mantissa()) * BigInt::from(10_u8).pow(left.scale() + right.scale());
    let right_side = BigInt::from(left.mantissa())
        * BigInt::from(right.mantissa())
        * BigInt::from(10_u8).pow(result.scale());
    left_side == right_side
}

fn decimal_quotient_is_exact(result: Decimal, left: Decimal, right: Decimal) -> bool {
    let left_side = BigInt::from(result.mantissa())
        * BigInt::from(right.mantissa())
        * BigInt::from(10_u8).pow(left.scale());
    let right_side =
        BigInt::from(left.mantissa()) * BigInt::from(10_u8).pow(right.scale() + result.scale());
    left_side == right_side
}

/// Adds two decimals without overflow.
///
/// # Errors
///
/// Returns [`DecimalError::Overflow`] when the exact result is out of range.
pub fn checked_decimal_add(left: Decimal, right: Decimal) -> Result<Decimal, DecimalError> {
    let result = left
        .checked_add(right)
        .ok_or(DecimalError::Overflow {
            operation: "addition",
        })?
        .normalize();
    if decimal_sum_is_exact(result, left, right, false) {
        Ok(result)
    } else {
        Err(DecimalError::Inexact {
            operation: "addition",
        })
    }
}

/// Subtracts two decimals without overflow.
///
/// # Errors
///
/// Returns [`DecimalError::Overflow`] when the exact result is out of range.
pub fn checked_decimal_sub(left: Decimal, right: Decimal) -> Result<Decimal, DecimalError> {
    let result = left
        .checked_sub(right)
        .ok_or(DecimalError::Overflow {
            operation: "subtraction",
        })?
        .normalize();
    if decimal_sum_is_exact(result, left, right, true) {
        Ok(result)
    } else {
        Err(DecimalError::Inexact {
            operation: "subtraction",
        })
    }
}

/// Multiplies two decimals without overflow.
///
/// # Errors
///
/// Returns [`DecimalError::Overflow`] when the exact result is out of range.
pub fn checked_decimal_mul(left: Decimal, right: Decimal) -> Result<Decimal, DecimalError> {
    let result = left
        .checked_mul(right)
        .ok_or(DecimalError::Overflow {
            operation: "multiplication",
        })?
        .normalize();
    if decimal_product_is_exact(result, left, right) {
        Ok(result)
    } else {
        Err(DecimalError::Inexact {
            operation: "multiplication",
        })
    }
}

/// Divides without silently accepting a rounded `rust_decimal` result.
///
/// # Errors
///
/// Returns an error for division by zero, overflow, or a quotient that cannot
/// be represented exactly.
pub fn checked_decimal_div(left: Decimal, right: Decimal) -> Result<Decimal, DecimalError> {
    if right.is_zero() {
        return Err(DecimalError::DivisionByZero);
    }
    let quotient = left
        .checked_div(right)
        .ok_or(DecimalError::Overflow {
            operation: "division",
        })?
        .normalize();
    if decimal_quotient_is_exact(quotient, left, right) {
        Ok(quotient)
    } else {
        Err(DecimalError::Inexact {
            operation: "division",
        })
    }
}

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

    #[test]
    fn truth_negation_preserves_unknown_and_conflict() {
        assert_eq!(TruthValue::Unknown.not(), TruthValue::Unknown);
        assert_eq!(TruthValue::Conflict.not(), TruthValue::Conflict);
        assert_eq!(TruthValue::True.not(), TruthValue::False);
    }

    #[test]
    fn truth_operators_preserve_conflict_over_unknown() {
        assert_eq!(
            TruthValue::False.and(TruthValue::Unknown),
            TruthValue::False
        );
        assert_eq!(TruthValue::True.or(TruthValue::Unknown), TruthValue::True);
        assert_eq!(
            TruthValue::Conflict.and(TruthValue::True),
            TruthValue::Conflict
        );
        assert_eq!(
            TruthValue::Conflict.and(TruthValue::Unknown),
            TruthValue::Conflict
        );
        assert_eq!(
            TruthValue::Conflict.or(TruthValue::Unknown),
            TruthValue::Conflict
        );
    }

    #[test]
    fn decimal_rendering_is_normalized() {
        let value = Value::decimal(parse_decimal("1.2300").unwrap());
        assert_eq!(value.to_string(), "1.23");
    }

    #[test]
    fn decimal_parsing_rejects_values_that_require_rounding() {
        assert_eq!(
            parse_decimal("0.0000000000000000000000000001").unwrap(),
            Decimal::new(1, 28)
        );
        assert_eq!(parse_decimal("1e-28").unwrap(), Decimal::new(1, 28));
        assert!(parse_decimal("0.00000000000000000000000000001").is_err());
        assert!(parse_decimal("1e-29").is_err());
    }

    #[test]
    fn tagged_decimal_json_rejects_values_that_require_rounding() {
        let value = json!({
            "type": "decimal",
            "value": "0.00000000000000000000000000001",
        });
        assert!(matches!(
            Value::from_json(value),
            Err(ValueConversionError::InvalidTaggedValue(_))
        ));
    }

    #[test]
    fn typed_json_preserves_full_decimal_number_precision() {
        let json: JsonValue = serde_json::from_str("0.1234567890123456789012345679").unwrap();
        assert_eq!(
            Value::from_plain_json(&json, &ValueKind::Decimal).unwrap(),
            Value::decimal(parse_decimal("0.1234567890123456789012345679").unwrap())
        );
    }

    #[test]
    fn human_readable_numbers_use_digit_separators() {
        assert_eq!(Value::Int(1_234_567).to_string(), "1_234_567");
        assert_eq!(Value::Int(-1_234_567).to_string(), "-1_234_567");
        assert_eq!(
            Value::decimal(parse_decimal("1234567.8900").unwrap()).to_string(),
            "1_234_567.89"
        );
        assert_eq!(Value::Duration(86_400).to_string(), "86_400s");

        assert_eq!(Value::Int(1_234_567).to_plain_json(), json!(1_234_567));
        assert_eq!(
            Value::decimal(parse_decimal("1234567.8900").unwrap()).to_plain_json(),
            json!("1234567.89")
        );
    }

    #[test]
    fn decimal_division_rejects_rounded_result() {
        assert_eq!(
            checked_decimal_div(parse_decimal("1").unwrap(), parse_decimal("3").unwrap()),
            Err(DecimalError::Inexact {
                operation: "division"
            })
        );
    }

    #[test]
    fn decimal_multiplication_rejects_underflow_rounding() {
        assert_eq!(
            checked_decimal_mul(
                parse_decimal("0.0000000000000000000000000001").unwrap(),
                parse_decimal("0.1").unwrap()
            ),
            Err(DecimalError::Inexact {
                operation: "multiplication"
            })
        );
    }

    #[test]
    fn decimal_addition_and_subtraction_reject_precision_rounding() {
        let tenth = Decimal::new(1, 1);
        assert_eq!(
            checked_decimal_add(Decimal::MAX, tenth),
            Err(DecimalError::Inexact {
                operation: "addition"
            })
        );
        assert_eq!(
            checked_decimal_sub(Decimal::MAX, tenth),
            Err(DecimalError::Inexact {
                operation: "subtraction"
            })
        );
    }

    #[test]
    fn decimal_division_accepts_exact_result() {
        assert_eq!(
            checked_decimal_div(parse_decimal("1").unwrap(), parse_decimal("4").unwrap()).unwrap(),
            parse_decimal("0.25").unwrap()
        );
    }

    #[test]
    fn tagged_json_round_trips_enum() {
        let value = Value::Enum {
            type_name: "등급".to_owned(),
            variant: "A".to_owned(),
        };
        assert_eq!(Value::from_json(value.to_json().unwrap()).unwrap(), value);
    }

    #[test]
    fn typed_json_parses_date_and_duration() {
        assert_eq!(
            Value::from_plain_json(&json!("2026-07-11"), &ValueKind::Date).unwrap(),
            Value::Date(NaiveDate::from_ymd_opt(2026, 7, 11).unwrap())
        );
        assert_eq!(
            Value::from_plain_json(&json!(3_600), &ValueKind::Duration).unwrap(),
            Value::Duration(3_600)
        );
    }

    #[test]
    fn conflict_reasons_are_stable() {
        assert_eq!(
            EvalValue::conflict(["z".to_owned(), "a".to_owned(), "a".to_owned()]),
            EvalValue::Conflict {
                reasons: vec!["a".to_owned(), "z".to_owned()]
            }
        );
    }
}