decimal-bytes 0.4.2

Arbitrary precision decimals with lexicographically sortable byte encoding
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
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
//! # decimal-bytes
//!
//! Arbitrary precision decimals with lexicographically sortable byte encoding.
//!
//! This crate provides three decimal types optimized for database storage:
//!
//! - **[`Decimal`]**: Variable-length arbitrary precision (up to 131,072 digits)
//! - **[`Decimal64`]**: Fixed 8-byte representation with embedded scale (precision ≤ 16 digits)
//! - **[`Decimal64NoScale`]**: Fixed 8-byte representation with external scale (precision ≤ 18 digits)
//!
//! All types support PostgreSQL special values (NaN, ±Infinity) with correct sort ordering.
//!
//! ## When to Use Which
//!
//! | Type | Precision | Scale | Storage | Best For |
//! |------|-----------|-------|---------|----------|
//! | `Decimal64NoScale` | ≤ 18 digits | External | 8 bytes | **Columnar storage**, aggregates |
//! | `Decimal64` | ≤ 16 digits | Embedded | 8 bytes | Self-contained values |
//! | `Decimal` | Unlimited | Unlimited | Variable | Scientific, very large numbers |
//!
//! ## Decimal64NoScale (Recommended for Columnar Storage)
//!
//! ```
//! use decimal_bytes::Decimal64NoScale;
//!
//! // Scale is provided externally (e.g., from schema metadata)
//! let scale = 2;
//! let a = Decimal64NoScale::new("100.50", scale).unwrap();
//! let b = Decimal64NoScale::new("200.25", scale).unwrap();
//!
//! // Aggregates work correctly - just sum the raw i64 values!
//! let sum = a.value() + b.value();  // 30075
//! let result = Decimal64NoScale::from_raw(sum);
//! assert_eq!(result.to_string_with_scale(scale), "300.75");
//! ```
//!
//! ## Decimal64 Example
//!
//! ```
//! use decimal_bytes::Decimal64;
//!
//! // Create with embedded scale
//! let price = Decimal64::new("99.99", 2).unwrap();
//! assert_eq!(price.to_string(), "99.99");
//! assert_eq!(price.scale(), 2);  // Scale is embedded!
//!
//! // With precision and scale (PostgreSQL NUMERIC semantics)
//! let d = Decimal64::with_precision_scale("123.456", Some(5), Some(2)).unwrap();
//! assert_eq!(d.to_string(), "123.46"); // Rounded
//!
//! // Parse with automatic scale detection
//! let d: Decimal64 = "123.456".parse().unwrap();
//! assert_eq!(d.scale(), 3);
//!
//! // Special values
//! let inf = Decimal64::infinity();
//! let nan = Decimal64::nan();
//! assert!(price < inf);
//! assert!(inf < nan);
//! ```
//!
//! ## Decimal Example (Arbitrary Precision)
//!
//! ```
//! use decimal_bytes::Decimal;
//! use std::str::FromStr;
//!
//! // Create decimals
//! let a = Decimal::from_str("123.456").unwrap();
//! let b = Decimal::from_str("123.457").unwrap();
//!
//! // Byte comparison matches numerical comparison
//! assert!(a.as_bytes() < b.as_bytes());
//! assert!(a < b);
//!
//! // Special values (PostgreSQL compatible)
//! let inf = Decimal::infinity();
//! let nan = Decimal::nan();
//! assert!(a < inf);
//! assert!(inf < nan);
//! ```
//!
//! ## Sort Order
//!
//! The lexicographic byte order matches PostgreSQL NUMERIC:
//!
//! ```text
//! -Infinity < negative numbers < zero < positive numbers < +Infinity < NaN
//! ```
//!
//! Both `Decimal` and `Decimal64` support this sort order including special values.
//!
//! ## Special Value Semantics (PostgreSQL vs IEEE 754)
//!
//! The `Decimal` type follows **PostgreSQL semantics** for special values:
//!
//! | Behavior | PostgreSQL / decimal-bytes | IEEE 754 float |
//! |----------|---------------------------|----------------|
//! | `NaN == NaN` | `true` | `false` |
//! | `NaN` ordering | Greatest value (> Infinity) | Unordered |
//! | `Infinity == Infinity` | `true` | `true` |
//!
//! ```
//! use decimal_bytes::Decimal;
//!
//! let nan1 = Decimal::nan();
//! let nan2 = Decimal::nan();
//! let inf = Decimal::infinity();
//!
//! // NaN equals itself (PostgreSQL behavior, unlike IEEE 754)
//! assert_eq!(nan1, nan2);
//!
//! // NaN is greater than everything, including Infinity
//! assert!(nan1 > inf);
//! ```
//!
//! This makes `Decimal` suitable for use in indexes, sorting, and deduplication
//! where consistent ordering and equality semantics are required.

mod decimal64;
mod decimal64_no_scale;
mod encoding;

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub use decimal64::{Decimal64, MAX_DECIMAL64_PRECISION, MAX_DECIMAL64_SCALE};
pub use decimal64_no_scale::{
    Decimal64NoScale, MAX_DECIMAL64_NO_SCALE_PRECISION, MAX_DECIMAL64_NO_SCALE_SCALE,
};
pub use encoding::DecimalError;
pub use encoding::SpecialValue;
use encoding::{
    decode_special_value, decode_to_string, decode_to_string_with_scale, encode_decimal,
    encode_decimal_with_constraints, encode_special_value, ENCODING_NAN, ENCODING_NEG_INFINITY,
    ENCODING_POS_INFINITY,
};

/// An arbitrary precision decimal number stored as sortable bytes.
///
/// The internal byte representation is designed to be lexicographically sortable,
/// meaning that comparing the bytes directly yields the same result as comparing
/// the numerical values. This enables efficient range queries in databases.
///
/// # Storage Efficiency
///
/// The encoding uses:
/// - 1 byte for the sign
/// - Variable bytes for the exponent (typically 1-3 bytes)
/// - 4 bits per decimal digit (BCD encoding, 2 digits per byte)
#[derive(Clone)]
pub struct Decimal {
    bytes: Vec<u8>,
}

impl Decimal {
    /// Creates a new Decimal with precision and scale constraints.
    ///
    /// Values that exceed the constraints are truncated/rounded to fit.
    /// This is compatible with SQL NUMERIC(precision, scale) semantics.
    ///
    /// - `precision`: Maximum total number of significant digits (None = unlimited)
    /// - `scale`: Digits after decimal point; negative values round to left of decimal
    ///
    /// # PostgreSQL Compatibility
    ///
    /// Supports negative scale (rounds to powers of 10):
    /// - `scale = -3` rounds to nearest 1000
    /// - `NUMERIC(2, -3)` allows values like -99000 to 99000
    ///
    /// # Examples
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    ///
    /// // NUMERIC(5, 2) - up to 5 digits total, 2 after decimal
    /// let d = Decimal::with_precision_scale("123.456", Some(5), Some(2)).unwrap();
    /// assert_eq!(d.to_string(), "123.46"); // Rounded to 2 decimal places
    ///
    /// // NUMERIC(2, -3) - rounds to nearest 1000, max 2 significant digits
    /// let d = Decimal::with_precision_scale("12345", Some(2), Some(-3)).unwrap();
    /// assert_eq!(d.to_string(), "12000"); // Rounded to nearest 1000
    /// ```
    pub fn with_precision_scale(
        s: &str,
        precision: Option<u32>,
        scale: Option<i32>,
    ) -> Result<Self, DecimalError> {
        let bytes = encode_decimal_with_constraints(s, precision, scale)?;
        Ok(Self { bytes })
    }

    /// Creates a Decimal from raw bytes.
    ///
    /// The bytes must be a valid encoding produced by `as_bytes()`.
    /// Returns an error if the bytes are invalid.
    ///
    /// # Examples
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// let original = Decimal::from_str("123.456").unwrap();
    /// let bytes = original.as_bytes();
    /// let restored = Decimal::from_bytes(bytes).unwrap();
    /// assert_eq!(original, restored);
    /// ```
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecimalError> {
        // Validate by attempting to decode
        let _ = decode_to_string(bytes)?;
        Ok(Self {
            bytes: bytes.to_vec(),
        })
    }

    /// Creates a Decimal from raw bytes without validation.
    ///
    /// # Safety
    ///
    /// The caller must ensure the bytes are a valid encoding.
    /// Using invalid bytes may cause panics or incorrect results.
    #[inline]
    pub fn from_bytes_unchecked(bytes: Vec<u8>) -> Self {
        Self { bytes }
    }

    /// Returns the raw byte representation.
    ///
    /// These bytes are lexicographically sortable - comparing them directly
    /// yields the same result as comparing the numerical values.
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Consumes the Decimal and returns the underlying bytes.
    #[inline]
    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    /// Returns true if this decimal represents zero.
    pub fn is_zero(&self) -> bool {
        self.bytes.len() == 1 && self.bytes[0] == encoding::SIGN_ZERO
    }

    /// Returns true if this decimal is negative.
    pub fn is_negative(&self) -> bool {
        !self.bytes.is_empty() && self.bytes[0] == encoding::SIGN_NEGATIVE
    }

    /// Returns true if this decimal is positive (and not zero).
    pub fn is_positive(&self) -> bool {
        !self.bytes.is_empty() && self.bytes[0] == encoding::SIGN_POSITIVE
    }

    /// Returns true if this decimal represents positive infinity.
    pub fn is_pos_infinity(&self) -> bool {
        self.bytes.as_slice() == ENCODING_POS_INFINITY
    }

    /// Returns true if this decimal represents negative infinity.
    pub fn is_neg_infinity(&self) -> bool {
        self.bytes.as_slice() == ENCODING_NEG_INFINITY
    }

    /// Returns true if this decimal represents positive or negative infinity.
    pub fn is_infinity(&self) -> bool {
        self.is_pos_infinity() || self.is_neg_infinity()
    }

    /// Returns true if this decimal represents NaN (Not a Number).
    pub fn is_nan(&self) -> bool {
        self.bytes.as_slice() == ENCODING_NAN
    }

    /// Returns true if this decimal is a special value (Infinity or NaN).
    pub fn is_special(&self) -> bool {
        decode_special_value(&self.bytes).is_some()
    }

    /// Returns true if this decimal is a finite number (not Infinity or NaN).
    pub fn is_finite(&self) -> bool {
        !self.is_special()
    }

    /// Returns the number of bytes used to store this decimal.
    #[inline]
    pub fn byte_len(&self) -> usize {
        self.bytes.len()
    }

    /// Creates positive infinity.
    ///
    /// Infinity is greater than all finite numbers but less than NaN.
    /// Two positive infinities are equal to each other.
    ///
    /// # Example
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// let inf = Decimal::infinity();
    /// let big = Decimal::from_str("999999999999").unwrap();
    /// assert!(inf > big);
    /// assert_eq!(inf, Decimal::infinity());
    /// ```
    pub fn infinity() -> Self {
        Self {
            bytes: encode_special_value(SpecialValue::Infinity),
        }
    }

    /// Creates negative infinity.
    ///
    /// Negative infinity is less than all finite numbers and positive infinity.
    /// Two negative infinities are equal to each other.
    ///
    /// # Example
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// let neg_inf = Decimal::neg_infinity();
    /// let small = Decimal::from_str("-999999999999").unwrap();
    /// assert!(neg_inf < small);
    /// assert_eq!(neg_inf, Decimal::neg_infinity());
    /// ```
    pub fn neg_infinity() -> Self {
        Self {
            bytes: encode_special_value(SpecialValue::NegInfinity),
        }
    }

    /// Creates NaN (Not a Number).
    ///
    /// # PostgreSQL Semantics
    ///
    /// Unlike IEEE 754 floating-point where `NaN != NaN`, this follows PostgreSQL
    /// semantics where:
    /// - `NaN == NaN` is `true`
    /// - `NaN` is the greatest value (greater than positive infinity)
    /// - All NaN values are equal regardless of how they were created
    ///
    /// This makes NaN usable in sorting, indexing, and deduplication.
    ///
    /// # Example
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// let nan1 = Decimal::nan();
    /// let nan2 = Decimal::from_str("NaN").unwrap();
    /// let inf = Decimal::infinity();
    ///
    /// // NaN equals itself (PostgreSQL behavior)
    /// assert_eq!(nan1, nan2);
    ///
    /// // NaN is greater than everything
    /// assert!(nan1 > inf);
    /// ```
    pub fn nan() -> Self {
        Self {
            bytes: encode_special_value(SpecialValue::NaN),
        }
    }

    /// Converts this decimal to a string with a specific scale (number of decimal places).
    ///
    /// This ensures the output has exactly `scale` decimal places, adding trailing
    /// zeros if needed. Useful for PostgreSQL NUMERIC display formatting where
    /// the scale defines the display format.
    ///
    /// - Positive scale: adds decimal point with trailing zeros as needed
    /// - Zero scale: no decimal point added
    /// - Negative scale: no decimal point (value is already an integer)
    ///
    /// Special values (NaN, Infinity, -Infinity) are returned as-is without scale formatting.
    ///
    /// # Arguments
    /// * `scale` - Number of decimal places to ensure in the output
    ///
    /// # Examples
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// // Positive scale: includes decimal point and trailing zeros
    /// let d = Decimal::from_str("1").unwrap();
    /// assert_eq!(d.to_string_with_scale(18), "1.000000000000000000");
    ///
    /// let d = Decimal::from_str("1.5").unwrap();
    /// assert_eq!(d.to_string_with_scale(3), "1.500");
    ///
    /// // Zero scale: no decimal point
    /// let d = Decimal::from_str("123").unwrap();
    /// assert_eq!(d.to_string_with_scale(0), "123");
    ///
    /// // Negative scale: no decimal point
    /// let d = Decimal::from_str("100").unwrap();
    /// assert_eq!(d.to_string_with_scale(-2), "100");
    ///
    /// // Special values are unchanged
    /// let nan = Decimal::nan();
    /// assert_eq!(nan.to_string_with_scale(10), "NaN");
    /// ```
    pub fn to_string_with_scale(&self, scale: i32) -> String {
        decode_to_string_with_scale(&self.bytes, scale).expect("Decimal contains valid bytes")
    }
}

impl FromStr for Decimal {
    type Err = DecimalError;

    /// Creates a new Decimal from a string representation.
    ///
    /// # Examples
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    /// use std::str::FromStr;
    ///
    /// let d = Decimal::from_str("123.456").unwrap();
    /// let d = Decimal::from_str("-0.001").unwrap();
    /// let d = Decimal::from_str("1e10").unwrap();
    /// // Or use parse:
    /// let d: Decimal = "42.5".parse().unwrap();
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = encode_decimal(s)?;
        Ok(Self { bytes })
    }
}

impl fmt::Display for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = decode_to_string(&self.bytes).expect("Decimal contains valid bytes");
        write!(f, "{}", s)
    }
}

impl fmt::Debug for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let value = decode_to_string(&self.bytes).expect("Decimal contains valid bytes");
        f.debug_struct("Decimal")
            .field("value", &value)
            .field("bytes", &self.bytes)
            .finish()
    }
}

impl PartialEq for Decimal {
    fn eq(&self, other: &Self) -> bool {
        self.bytes == other.bytes
    }
}

impl Eq for Decimal {}

impl PartialOrd for Decimal {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Decimal {
    fn cmp(&self, other: &Self) -> Ordering {
        // Byte comparison is equivalent to numerical comparison
        self.bytes.cmp(&other.bytes)
    }
}

impl Hash for Decimal {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.bytes.hash(state);
    }
}

impl Serialize for Decimal {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Serialize as string for human readability in JSON
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for Decimal {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Decimal::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// Conversion from integer types
macro_rules! impl_from_int {
    ($($t:ty),*) => {
        $(
            impl From<$t> for Decimal {
                fn from(val: $t) -> Self {
                    // Integer conversion is infallible - always produces valid decimal bytes.
                    // Uses string conversion internally since the byte encoding is designed
                    // around decimal string parsing (BCD encoding of digit pairs).
                    Decimal::from_str(&val.to_string()).expect("Integer is always valid")
                }
            }
        )*
    };
}

impl_from_int!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);

impl TryFrom<f64> for Decimal {
    type Error = DecimalError;

    /// Converts an f64 to a Decimal.
    ///
    /// Special values are handled:
    /// - `f64::NAN` → `Decimal::nan()`
    /// - `f64::INFINITY` → `Decimal::infinity()`
    /// - `f64::NEG_INFINITY` → `Decimal::neg_infinity()`
    ///
    /// Note: Due to f64's limited precision (~15-17 significant digits),
    /// very precise decimal values may lose precision when converted from f64.
    /// For exact decimal representation, use `Decimal::from_str()` instead.
    ///
    /// # Example
    ///
    /// ```
    /// use decimal_bytes::Decimal;
    ///
    /// let d: Decimal = 123.456f64.try_into().unwrap();
    /// assert_eq!(d.to_string(), "123.456");
    ///
    /// let inf: Decimal = f64::INFINITY.try_into().unwrap();
    /// assert!(inf.is_pos_infinity());
    ///
    /// let nan: Decimal = f64::NAN.try_into().unwrap();
    /// assert!(nan.is_nan());
    /// ```
    fn try_from(val: f64) -> Result<Self, Self::Error> {
        if val.is_nan() {
            return Ok(Decimal::nan());
        }
        if val.is_infinite() {
            return Ok(if val.is_sign_positive() {
                Decimal::infinity()
            } else {
                Decimal::neg_infinity()
            });
        }
        Decimal::from_str(&val.to_string())
    }
}

impl Default for Decimal {
    fn default() -> Self {
        Decimal {
            bytes: vec![encoding::SIGN_ZERO],
        }
    }
}

// ============================================================================
// Optional: rust_decimal interop (enabled with "rust_decimal" feature)
// ============================================================================

#[cfg(feature = "rust_decimal")]
mod rust_decimal_interop {
    use super::{Decimal, DecimalError};
    use std::str::FromStr;

    impl TryFrom<rust_decimal::Decimal> for Decimal {
        type Error = DecimalError;

        /// Converts a `rust_decimal::Decimal` to a `decimal_bytes::Decimal`.
        ///
        /// # Example
        ///
        /// ```ignore
        /// use rust_decimal::Decimal as RustDecimal;
        /// use decimal_bytes::Decimal;
        ///
        /// let rd = RustDecimal::new(12345, 2); // 123.45
        /// let d: Decimal = rd.try_into().unwrap();
        /// assert_eq!(d.to_string(), "123.45");
        /// ```
        fn try_from(value: rust_decimal::Decimal) -> Result<Self, Self::Error> {
            Decimal::from_str(&value.to_string())
        }
    }

    impl TryFrom<&Decimal> for rust_decimal::Decimal {
        type Error = rust_decimal::Error;

        /// Converts a `decimal_bytes::Decimal` to a `rust_decimal::Decimal`.
        ///
        /// Note: This may fail if the decimal exceeds rust_decimal's precision limits
        /// (28-29 significant digits) or if it's a special value (Infinity, NaN).
        ///
        /// # Example
        ///
        /// ```ignore
        /// use rust_decimal::Decimal as RustDecimal;
        /// use decimal_bytes::Decimal;
        ///
        /// let d = Decimal::from_str("123.45").unwrap();
        /// let rd: RustDecimal = (&d).try_into().unwrap();
        /// assert_eq!(rd.to_string(), "123.45");
        /// ```
        fn try_from(value: &Decimal) -> Result<Self, Self::Error> {
            use std::str::FromStr;
            rust_decimal::Decimal::from_str(&value.to_string())
        }
    }

    impl TryFrom<Decimal> for rust_decimal::Decimal {
        type Error = rust_decimal::Error;

        fn try_from(value: Decimal) -> Result<Self, Self::Error> {
            rust_decimal::Decimal::try_from(&value)
        }
    }
}

// The rust_decimal_interop module only contains trait implementations,
// so there's nothing to re-export. The TryFrom impls are automatically available.

// ============================================================================
// Optional: bigdecimal interop (enabled with "bigdecimal" feature)
// ============================================================================

#[cfg(feature = "bigdecimal")]
mod bigdecimal_interop {
    use super::{Decimal, DecimalError};
    use std::str::FromStr;

    impl TryFrom<bigdecimal::BigDecimal> for Decimal {
        type Error = DecimalError;

        /// Converts a `bigdecimal::BigDecimal` to a `decimal_bytes::Decimal`.
        fn try_from(value: bigdecimal::BigDecimal) -> Result<Self, Self::Error> {
            Decimal::from_str(&value.to_string())
        }
    }

    impl TryFrom<&bigdecimal::BigDecimal> for Decimal {
        type Error = DecimalError;

        fn try_from(value: &bigdecimal::BigDecimal) -> Result<Self, Self::Error> {
            Decimal::from_str(&value.to_string())
        }
    }

    impl TryFrom<&Decimal> for bigdecimal::BigDecimal {
        type Error = bigdecimal::ParseBigDecimalError;

        /// Converts a `decimal_bytes::Decimal` to a `bigdecimal::BigDecimal`.
        ///
        /// Note: This may fail for special values (Infinity, NaN) which
        /// bigdecimal doesn't support.
        fn try_from(value: &Decimal) -> Result<Self, Self::Error> {
            use std::str::FromStr;
            bigdecimal::BigDecimal::from_str(&value.to_string())
        }
    }

    impl TryFrom<Decimal> for bigdecimal::BigDecimal {
        type Error = bigdecimal::ParseBigDecimalError;

        fn try_from(value: Decimal) -> Result<Self, Self::Error> {
            bigdecimal::BigDecimal::try_from(&value)
        }
    }
}

// The bigdecimal_interop module only contains trait implementations,
// so there's nothing to re-export. The TryFrom impls are automatically available.

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

    #[test]
    fn test_from_str() {
        let d = Decimal::from_str("123.456").unwrap();
        assert_eq!(d.to_string(), "123.456");
    }

    #[test]
    fn test_zero() {
        let d = Decimal::from_str("0").unwrap();
        assert!(d.is_zero());
        assert!(!d.is_negative());
        assert!(!d.is_positive());
        assert!(d.is_finite());
        assert!(!d.is_special());
    }

    #[test]
    fn test_negative() {
        let d = Decimal::from_str("-123.456").unwrap();
        assert!(d.is_negative());
        assert!(!d.is_zero());
        assert!(!d.is_positive());
        assert!(d.is_finite());
    }

    #[test]
    fn test_positive() {
        let d = Decimal::from_str("123.456").unwrap();
        assert!(d.is_positive());
        assert!(!d.is_zero());
        assert!(!d.is_negative());
        assert!(d.is_finite());
    }

    #[test]
    fn test_ordering() {
        let values = vec!["-100", "-10", "-1", "-0.1", "0", "0.1", "1", "10", "100"];
        let decimals: Vec<Decimal> = values
            .iter()
            .map(|s| Decimal::from_str(s).unwrap())
            .collect();

        // Check that ordering is correct
        for i in 0..decimals.len() - 1 {
            assert!(
                decimals[i] < decimals[i + 1],
                "{} should be < {}",
                values[i],
                values[i + 1]
            );
        }

        // Check that byte ordering matches
        for i in 0..decimals.len() - 1 {
            assert!(
                decimals[i].as_bytes() < decimals[i + 1].as_bytes(),
                "bytes of {} should be < bytes of {}",
                values[i],
                values[i + 1]
            );
        }
    }

    #[test]
    fn test_roundtrip() {
        let values = vec![
            "0", "1", "-1", "123.456", "-123.456", "0.001", "0.1", "10", "100", "1000000",
            "-1000000",
        ];

        for s in values {
            let d = Decimal::from_str(s).unwrap();
            let bytes = d.as_bytes();
            let restored = Decimal::from_bytes(bytes).unwrap();
            assert_eq!(d, restored, "Roundtrip failed for {}", s);
        }
    }

    #[test]
    fn test_precision_scale() {
        // Round to 2 decimal places
        let d = Decimal::with_precision_scale("123.456", Some(10), Some(2)).unwrap();
        assert_eq!(d.to_string(), "123.46");

        // When precision is exceeded, least significant integer digits are kept
        let d = Decimal::with_precision_scale("12345.67", Some(5), Some(2)).unwrap();
        assert_eq!(d.to_string(), "345.67"); // 5 digits total, 2 after decimal = 3 integer digits max

        // Rounding within precision limits
        let d = Decimal::with_precision_scale("99.999", Some(5), Some(2)).unwrap();
        assert_eq!(d.to_string(), "100"); // Rounds up, fits in precision
    }

    #[test]
    fn test_from_integer() {
        let d = Decimal::from(42i64);
        assert_eq!(d.to_string(), "42");

        let d = Decimal::from(-100i32);
        assert_eq!(d.to_string(), "-100");
    }

    #[test]
    fn test_from_f64() {
        // Normal f64 values
        let d: Decimal = 123.456f64.try_into().unwrap();
        assert_eq!(d.to_string(), "123.456");

        let d: Decimal = (-99.5f64).try_into().unwrap();
        assert_eq!(d.to_string(), "-99.5");

        let d: Decimal = 0.0f64.try_into().unwrap();
        assert!(d.is_zero());

        // Special values
        let inf: Decimal = f64::INFINITY.try_into().unwrap();
        assert!(inf.is_pos_infinity());

        let neg_inf: Decimal = f64::NEG_INFINITY.try_into().unwrap();
        assert!(neg_inf.is_neg_infinity());

        let nan: Decimal = f64::NAN.try_into().unwrap();
        assert!(nan.is_nan());
    }

    #[test]
    fn test_serialization() {
        let d = Decimal::from_str("123.456").unwrap();
        let json = serde_json::to_string(&d).unwrap();
        assert_eq!(json, "\"123.456\"");

        let restored: Decimal = serde_json::from_str(&json).unwrap();
        assert_eq!(d, restored);
    }

    #[test]
    fn test_byte_efficiency() {
        // Check that storage is reasonably efficient
        let d = Decimal::from_str("123456789").unwrap();
        // 1 byte sign + ~2 bytes exponent + ~5 bytes mantissa (9 digits / 2)
        assert!(
            d.byte_len() <= 10,
            "Expected <= 10 bytes, got {}",
            d.byte_len()
        );

        let d = Decimal::from_str("0.000001").unwrap();
        // Should be compact for small numbers too
        assert!(
            d.byte_len() <= 6,
            "Expected <= 6 bytes, got {}",
            d.byte_len()
        );
    }

    // ==================== Special Values Tests ====================

    #[test]
    fn test_infinity_creation() {
        let pos_inf = Decimal::infinity();
        assert!(pos_inf.is_pos_infinity());
        assert!(pos_inf.is_infinity());
        assert!(!pos_inf.is_neg_infinity());
        assert!(!pos_inf.is_nan());
        assert!(pos_inf.is_special());
        assert!(!pos_inf.is_finite());
        assert_eq!(pos_inf.to_string(), "Infinity");

        let neg_inf = Decimal::neg_infinity();
        assert!(neg_inf.is_neg_infinity());
        assert!(neg_inf.is_infinity());
        assert!(!neg_inf.is_pos_infinity());
        assert!(!neg_inf.is_nan());
        assert!(neg_inf.is_special());
        assert!(!neg_inf.is_finite());
        assert_eq!(neg_inf.to_string(), "-Infinity");
    }

    #[test]
    fn test_nan_creation() {
        let nan = Decimal::nan();
        assert!(nan.is_nan());
        assert!(nan.is_special());
        assert!(!nan.is_finite());
        assert!(!nan.is_infinity());
        assert!(!nan.is_zero());
        assert_eq!(nan.to_string(), "NaN");
    }

    #[test]
    fn test_special_value_from_str() {
        let pos_inf = Decimal::from_str("Infinity").unwrap();
        assert!(pos_inf.is_pos_infinity());

        let neg_inf = Decimal::from_str("-Infinity").unwrap();
        assert!(neg_inf.is_neg_infinity());

        let nan = Decimal::from_str("NaN").unwrap();
        assert!(nan.is_nan());

        // Case-insensitive
        let inf = Decimal::from_str("infinity").unwrap();
        assert!(inf.is_pos_infinity());

        let inf = Decimal::from_str("INF").unwrap();
        assert!(inf.is_pos_infinity());
    }

    #[test]
    fn test_special_value_ordering() {
        // PostgreSQL order: -Infinity < negatives < zero < positives < Infinity < NaN
        let neg_inf = Decimal::neg_infinity();
        let neg_num = Decimal::from_str("-1000").unwrap();
        let zero = Decimal::from_str("0").unwrap();
        let pos_num = Decimal::from_str("1000").unwrap();
        let pos_inf = Decimal::infinity();
        let nan = Decimal::nan();

        assert!(neg_inf < neg_num);
        assert!(neg_num < zero);
        assert!(zero < pos_num);
        assert!(pos_num < pos_inf);
        assert!(pos_inf < nan);

        // Verify byte ordering matches
        assert!(neg_inf.as_bytes() < neg_num.as_bytes());
        assert!(neg_num.as_bytes() < zero.as_bytes());
        assert!(zero.as_bytes() < pos_num.as_bytes());
        assert!(pos_num.as_bytes() < pos_inf.as_bytes());
        assert!(pos_inf.as_bytes() < nan.as_bytes());
    }

    #[test]
    fn test_special_value_equality() {
        // All NaNs are equal (PostgreSQL semantics)
        let nan1 = Decimal::from_str("NaN").unwrap();
        let nan2 = Decimal::from_str("nan").unwrap();
        let nan3 = Decimal::nan();
        assert_eq!(nan1, nan2);
        assert_eq!(nan2, nan3);

        // Infinities are equal to themselves
        let inf1 = Decimal::infinity();
        let inf2 = Decimal::from_str("Infinity").unwrap();
        assert_eq!(inf1, inf2);

        let neg_inf1 = Decimal::neg_infinity();
        let neg_inf2 = Decimal::from_str("-Infinity").unwrap();
        assert_eq!(neg_inf1, neg_inf2);
    }

    #[test]
    fn test_special_value_serialization() {
        let inf = Decimal::infinity();
        let json = serde_json::to_string(&inf).unwrap();
        assert_eq!(json, "\"Infinity\"");
        let restored: Decimal = serde_json::from_str(&json).unwrap();
        assert_eq!(inf, restored);

        let nan = Decimal::nan();
        let json = serde_json::to_string(&nan).unwrap();
        assert_eq!(json, "\"NaN\"");
        let restored: Decimal = serde_json::from_str(&json).unwrap();
        assert_eq!(nan, restored);
    }

    #[test]
    fn test_special_value_byte_efficiency() {
        // Special values should be compact (3 bytes each)
        assert_eq!(Decimal::infinity().byte_len(), 3);
        assert_eq!(Decimal::neg_infinity().byte_len(), 3);
        assert_eq!(Decimal::nan().byte_len(), 3);
    }

    // ==================== Negative Scale Tests ====================

    #[test]
    fn test_negative_scale() {
        // Round to nearest 1000
        let d = Decimal::with_precision_scale("12345", Some(10), Some(-3)).unwrap();
        assert_eq!(d.to_string(), "12000");

        // Round up
        let d = Decimal::with_precision_scale("12500", Some(10), Some(-3)).unwrap();
        assert_eq!(d.to_string(), "13000");

        // Round to nearest 100
        let d = Decimal::with_precision_scale("1234", Some(10), Some(-2)).unwrap();
        assert_eq!(d.to_string(), "1200");
    }

    #[test]
    fn test_negative_scale_with_precision() {
        // NUMERIC(2, -3): 2 significant digits, round to nearest 1000
        let d = Decimal::with_precision_scale("12345", Some(2), Some(-3)).unwrap();
        assert_eq!(d.to_string(), "12000");
    }

    // ==================== Error Handling Tests ====================

    #[test]
    fn test_invalid_format_errors() {
        // Multiple decimal points
        let result = Decimal::from_str("1.2.3");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DecimalError::InvalidFormat(_)
        ));

        // Invalid characters
        let result = Decimal::from_str("12abc");
        assert!(result.is_err());

        // Missing exponent after 'e'
        let result = Decimal::from_str("1e");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DecimalError::InvalidFormat(_)
        ));

        // Invalid exponent (non-numeric after 'e')
        let result = Decimal::from_str("1eabc");
        assert!(result.is_err());

        // Empty string gives zero
        let d = Decimal::from_str("").unwrap();
        assert!(d.is_zero());

        // Just a sign with no digits
        let result = Decimal::from_str("-");
        assert!(result.is_ok()); // Parses as zero
    }

    #[test]
    fn test_leading_plus_sign() {
        let d = Decimal::from_str("+123.456").unwrap();
        assert_eq!(d.to_string(), "123.456");
        assert!(d.is_positive());
    }

    #[test]
    fn test_scientific_notation() {
        let d = Decimal::from_str("1.5e10").unwrap();
        assert_eq!(d.to_string(), "15000000000");

        let d = Decimal::from_str("1.5E-3").unwrap();
        assert_eq!(d.to_string(), "0.0015");

        let d = Decimal::from_str("1e+5").unwrap();
        assert_eq!(d.to_string(), "100000");
    }

    #[test]
    fn test_leading_decimal_point() {
        // ".5" should be parsed as "0.5"
        let d = Decimal::from_str(".5").unwrap();
        assert_eq!(d.to_string(), "0.5");

        let d = Decimal::from_str("-.25").unwrap();
        assert_eq!(d.to_string(), "-0.25");
    }

    #[test]
    fn test_trailing_zeros() {
        let d = Decimal::from_str("100").unwrap();
        assert_eq!(d.to_string(), "100");

        let d = Decimal::from_str("1.500").unwrap();
        assert_eq!(d.to_string(), "1.5");
    }

    #[test]
    fn test_leading_zeros() {
        let d = Decimal::from_str("007").unwrap();
        assert_eq!(d.to_string(), "7");

        let d = Decimal::from_str("00.123").unwrap();
        assert_eq!(d.to_string(), "0.123");
    }

    // ==================== Additional Trait Tests ====================

    #[test]
    fn test_into_bytes() {
        let d = Decimal::from_str("123.456").unwrap();
        let bytes_ref = d.as_bytes().to_vec();
        let bytes_owned = d.into_bytes();
        assert_eq!(bytes_ref, bytes_owned);
    }

    #[test]
    fn test_clone() {
        let d1 = Decimal::from_str("123.456").unwrap();
        let d2 = d1.clone();
        assert_eq!(d1, d2);
        assert_eq!(d1.as_bytes(), d2.as_bytes());
    }

    #[test]
    fn test_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(Decimal::from_str("123.456").unwrap());
        set.insert(Decimal::from_str("123.456").unwrap()); // Duplicate
        set.insert(Decimal::from_str("789.012").unwrap());

        assert_eq!(set.len(), 2);
        assert!(set.contains(&Decimal::from_str("123.456").unwrap()));
    }

    #[test]
    fn test_debug_format() {
        let d = Decimal::from_str("123.456").unwrap();
        let debug_str = format!("{:?}", d);
        assert!(debug_str.contains("Decimal"));
        assert!(debug_str.contains("123.456"));
    }

    #[test]
    fn test_ord_trait() {
        use std::cmp::Ordering;

        let a = Decimal::from_str("1").unwrap();
        let b = Decimal::from_str("2").unwrap();
        let c = Decimal::from_str("1").unwrap();

        assert_eq!(a.cmp(&b), Ordering::Less);
        assert_eq!(b.cmp(&a), Ordering::Greater);
        assert_eq!(a.cmp(&c), Ordering::Equal);
    }

    #[test]
    fn test_from_bytes_invalid() {
        // Empty bytes should fail
        let result = Decimal::from_bytes(&[]);
        assert!(result.is_err());

        // Single invalid sign byte
        let result = Decimal::from_bytes(&[0x00]);
        assert!(result.is_err());
    }

    #[test]
    fn test_deserialize_from_string_number() {
        // JSON string numbers deserialize correctly
        let d: Decimal = serde_json::from_str("\"42\"").unwrap();
        assert_eq!(d.to_string(), "42");

        let d: Decimal = serde_json::from_str("\"-100\"").unwrap();
        assert_eq!(d.to_string(), "-100");

        let d: Decimal = serde_json::from_str("\"1.5e10\"").unwrap();
        assert_eq!(d.to_string(), "15000000000");
    }

    #[test]
    fn test_from_various_integer_types() {
        assert_eq!(Decimal::from(0i32).to_string(), "0");
        assert_eq!(Decimal::from(i32::MAX).to_string(), "2147483647");
        assert_eq!(Decimal::from(i32::MIN).to_string(), "-2147483648");
        assert_eq!(Decimal::from(i64::MAX).to_string(), "9223372036854775807");
        assert_eq!(Decimal::from(i64::MIN).to_string(), "-9223372036854775808");
    }

    #[test]
    fn test_precision_overflow() {
        // Exponent too large
        let result = Decimal::from_str("1e20000");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DecimalError::PrecisionOverflow
        ));

        // Exponent too small (negative)
        let result = Decimal::from_str("1e-20000");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DecimalError::PrecisionOverflow
        ));
    }

    #[test]
    fn test_all_zeros_variations() {
        let d = Decimal::from_str("0").unwrap();
        assert!(d.is_zero());

        let d = Decimal::from_str("0.0").unwrap();
        assert!(d.is_zero());

        let d = Decimal::from_str("00.00").unwrap();
        assert!(d.is_zero());

        let d = Decimal::from_str("-0").unwrap();
        assert!(d.is_zero());
        // Note: -0 normalizes to 0
    }

    // ==================== Additional Edge Case Tests ====================

    #[test]
    fn test_rounding_all_nines() {
        // Rounding 99.999 with scale 2 should become 100.00 -> 100
        let d = Decimal::with_precision_scale("99.999", Some(10), Some(2)).unwrap();
        assert_eq!(d.to_string(), "100");

        // Rounding 9.99 with scale 1 should become 10.0 -> 10
        let d = Decimal::with_precision_scale("9.99", Some(10), Some(1)).unwrap();
        assert_eq!(d.to_string(), "10");

        // 999 rounding to nearest 10 (scale -1) should become 1000
        let d = Decimal::with_precision_scale("999", Some(10), Some(-1)).unwrap();
        assert_eq!(d.to_string(), "1000");
    }

    #[test]
    fn test_negative_scale_small_number() {
        // Number smaller than rounding unit, rounds to 0
        let d = Decimal::with_precision_scale("4", Some(10), Some(-1)).unwrap();
        assert_eq!(d.to_string(), "0");

        // Number >= half the rounding unit, rounds up
        let d = Decimal::with_precision_scale("5", Some(10), Some(-1)).unwrap();
        assert_eq!(d.to_string(), "10");

        // Negative number smaller than rounding unit
        let d = Decimal::with_precision_scale("-4", Some(10), Some(-1)).unwrap();
        assert_eq!(d.to_string(), "0");

        // Negative number >= half unit
        let d = Decimal::with_precision_scale("-5", Some(10), Some(-1)).unwrap();
        assert_eq!(d.to_string(), "-10");
    }

    #[test]
    fn test_precision_truncation() {
        // When precision is exceeded, truncate from left
        let d = Decimal::with_precision_scale("123456", Some(3), Some(0)).unwrap();
        assert_eq!(d.to_string(), "456");

        // With decimal places
        let d = Decimal::with_precision_scale("12345.67", Some(4), Some(2)).unwrap();
        assert_eq!(d.to_string(), "45.67");
    }

    #[test]
    fn test_very_small_numbers() {
        let d = Decimal::from_str("0.000000001").unwrap();
        assert_eq!(d.to_string(), "0.000000001");
        assert!(d.is_positive());

        let d = Decimal::from_str("-0.000000001").unwrap();
        assert_eq!(d.to_string(), "-0.000000001");
        assert!(d.is_negative());
    }

    #[test]
    fn test_very_large_numbers() {
        let d = Decimal::from_str("999999999999999999999999999999").unwrap();
        assert_eq!(d.to_string(), "999999999999999999999999999999");

        let d = Decimal::from_str("-999999999999999999999999999999").unwrap();
        assert_eq!(d.to_string(), "-999999999999999999999999999999");
    }

    #[test]
    fn test_max_exponent_boundary() {
        // Just under max exponent should work
        let d = Decimal::from_str("1e16000").unwrap();
        assert!(d.is_positive());

        // Just over should fail
        let result = Decimal::from_str("1e17000");
        assert!(result.is_err());
    }

    #[test]
    fn test_min_exponent_boundary() {
        // Just above min exponent should work
        let d = Decimal::from_str("1e-16000").unwrap();
        assert!(d.is_positive());

        // Just below should fail
        let result = Decimal::from_str("1e-17000");
        assert!(result.is_err());
    }

    #[test]
    fn test_odd_digit_count() {
        // Odd number of digits (tests BCD padding)
        let d = Decimal::from_str("12345").unwrap();
        assert_eq!(d.to_string(), "12345");

        let d = Decimal::from_str("1").unwrap();
        assert_eq!(d.to_string(), "1");

        let d = Decimal::from_str("123").unwrap();
        assert_eq!(d.to_string(), "123");
    }

    #[test]
    fn test_negative_number_ordering() {
        // Verify negative number byte ordering is correct
        let a = Decimal::from_str("-100").unwrap();
        let b = Decimal::from_str("-10").unwrap();
        let c = Decimal::from_str("-1").unwrap();

        // -100 < -10 < -1 numerically
        assert!(a < b);
        assert!(b < c);

        // Byte ordering should match
        assert!(a.as_bytes() < b.as_bytes());
        assert!(b.as_bytes() < c.as_bytes());
    }

    #[test]
    fn test_from_bytes_unchecked_roundtrip() {
        let original = Decimal::from_str("123.456").unwrap();
        let bytes = original.as_bytes().to_vec();
        let restored = Decimal::from_bytes_unchecked(bytes);
        assert_eq!(original, restored);
    }

    #[test]
    fn test_special_value_checks() {
        let d = Decimal::from_str("123.456").unwrap();
        assert!(!d.is_nan());
        assert!(!d.is_infinity());
        assert!(!d.is_pos_infinity());
        assert!(!d.is_neg_infinity());
        assert!(!d.is_special());
        assert!(d.is_finite());
    }

    #[test]
    fn test_equality_and_hash_consistency() {
        use std::collections::HashMap;

        let d1 = Decimal::from_str("123.456").unwrap();
        let d2 = Decimal::from_str("123.456").unwrap();
        let d3 = Decimal::from_str("123.457").unwrap();

        // Equal values should be equal
        assert_eq!(d1, d2);
        assert_ne!(d1, d3);

        // Equal values should have same hash (can be used as map keys)
        let mut map = HashMap::new();
        map.insert(d1.clone(), "first");
        map.insert(d2.clone(), "second"); // Should overwrite
        assert_eq!(map.len(), 1);
        assert_eq!(map.get(&d1), Some(&"second"));
    }

    #[test]
    fn test_scale_zero() {
        // Scale 0 should keep integer part only
        let d = Decimal::with_precision_scale("123.999", Some(10), Some(0)).unwrap();
        assert_eq!(d.to_string(), "124"); // Rounds up
    }

    #[test]
    fn test_only_fractional_with_precision_scale() {
        let d = Decimal::with_precision_scale(".5", Some(10), Some(2)).unwrap();
        assert_eq!(d.to_string(), "0.5");
    }

    #[test]
    fn test_default_impl() {
        let d = Decimal::default();
        assert!(d.is_zero());
        assert_eq!(d.to_string(), "0");
    }

    #[test]
    fn test_precision_zero_integer_digits() {
        // When precision equals scale, no integer digits are allowed
        // This triggers the max_integer_digits == 0 branch
        let d = Decimal::with_precision_scale("123.456", Some(2), Some(2)).unwrap();
        assert_eq!(d.to_string(), "0.46");
    }

    #[test]
    fn test_negative_with_precision_truncation() {
        // Negative number that gets truncated by precision constraints
        let d = Decimal::with_precision_scale("-123.456", Some(3), Some(2)).unwrap();
        assert_eq!(d.to_string(), "-3.46");
    }

    #[test]
    fn test_invalid_sign_byte() {
        // Sign bytes: SIGN_NEGATIVE=0x00, SIGN_ZERO=0x80, SIGN_POSITIVE=0xFF
        // Any other sign byte is invalid

        // Sign byte 0x01 is invalid
        let result = Decimal::from_bytes(&[0x01, 0x40, 0x00, 0x12]);
        assert!(result.is_err());

        // Sign byte 0x7F is also invalid
        let result = Decimal::from_bytes(&[0x7F, 0x40, 0x00, 0x12]);
        assert!(result.is_err());

        // Sign byte 0xFE is also invalid
        let result = Decimal::from_bytes(&[0xFE, 0x40, 0x00, 0x12]);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_bcd_encoding() {
        // Construct bytes that would decode to invalid BCD (digit > 9)
        // SIGN_POSITIVE = 0xFF, with valid exponent, but invalid BCD mantissa
        // A valid BCD digit must have each nibble 0-9. 0xAB has A=10, B=11 (both > 9)
        let invalid_bytes = vec![
            0xFF, // SIGN_POSITIVE
            0x80, 0x00, // Valid exponent (middle of range)
            0xAB, // Invalid BCD: high nibble = 10, low nibble = 11
        ];
        let result = Decimal::from_bytes(&invalid_bytes);
        assert!(result.is_err());

        // Also test with just high nibble invalid
        let invalid_bytes = vec![
            0xFF, // SIGN_POSITIVE
            0x80, 0x00, // Valid exponent
            0xA1, // Invalid BCD: high nibble = 10, low nibble = 1
        ];
        let result = Decimal::from_bytes(&invalid_bytes);
        assert!(result.is_err());

        // Also test with just low nibble invalid
        let invalid_bytes = vec![
            0xFF, // SIGN_POSITIVE
            0x80, 0x00, // Valid exponent
            0x1B, // Invalid BCD: high nibble = 1, low nibble = 11
        ];
        let result = Decimal::from_bytes(&invalid_bytes);
        assert!(result.is_err());
    }

    #[test]
    fn test_reserved_exponent_positive() {
        // SIGN_POSITIVE = 0xFF
        // Reserved exponents: 0xFFFE (Infinity), 0xFFFF (NaN)
        // If we have more than 3 bytes, special value check is skipped
        // but decode_exponent will catch the reserved value

        // Reserved NaN exponent (0xFFFF) with extra mantissa byte
        let bytes_with_reserved_exp = vec![
            0xFF, // SIGN_POSITIVE
            0xFF, 0xFF, // Reserved for NaN
            0x12, // Some mantissa (makes it 4 bytes, not 3)
        ];
        let result = Decimal::from_bytes(&bytes_with_reserved_exp);
        assert!(result.is_err());

        // Reserved Infinity exponent (0xFFFE) with extra mantissa byte
        let bytes_with_reserved_exp = vec![
            0xFF, // SIGN_POSITIVE
            0xFF, 0xFE, // Reserved for Infinity
            0x12, // Some mantissa (makes it 4 bytes, not 3)
        ];
        let result = Decimal::from_bytes(&bytes_with_reserved_exp);
        assert!(result.is_err());
    }

    #[test]
    fn test_reserved_exponent_negative() {
        // SIGN_NEGATIVE = 0x00
        // Reserved exponent for -Infinity: 0x0000
        // If we have more than 3 bytes, special value check is skipped

        let bytes_with_reserved_exp = vec![
            0x00, // SIGN_NEGATIVE
            0x00, 0x00, // Reserved for -Infinity
            0x12, // Some mantissa (makes it 4 bytes, not 3)
        ];
        let result = Decimal::from_bytes(&bytes_with_reserved_exp);
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_mantissa_bytes() {
        // Construct bytes with valid sign and exponent but no mantissa
        // This should decode to 0 via the empty digits path in format_decimal
        let bytes_no_mantissa = vec![
            0xFF, // SIGN_POSITIVE
            0x80, 0x00, // Valid exponent
                  // No mantissa bytes
        ];
        let d = Decimal::from_bytes(&bytes_no_mantissa).unwrap();
        assert_eq!(d.to_string(), "0");
    }

    // ==================== rust_decimal Interop Tests ====================

    #[cfg(feature = "rust_decimal")]
    mod rust_decimal_tests {
        use super::*;

        #[test]
        fn test_from_rust_decimal() {
            use rust_decimal::Decimal as RustDecimal;

            let rd = RustDecimal::new(12345, 2); // 123.45
            let d: Decimal = rd.try_into().unwrap();
            assert_eq!(d.to_string(), "123.45");
        }

        #[test]
        fn test_to_rust_decimal() {
            use rust_decimal::Decimal as RustDecimal;

            let d = Decimal::from_str("123.45").unwrap();
            let rd: RustDecimal = (&d).try_into().unwrap();
            assert_eq!(rd.to_string(), "123.45");
        }

        #[test]
        fn test_rust_decimal_roundtrip() {
            use rust_decimal::Decimal as RustDecimal;

            let values = vec!["0", "1", "-1", "123.456", "-999.999", "0.001"];

            for s in values {
                let d = Decimal::from_str(s).unwrap();
                let rd: RustDecimal = (&d).try_into().unwrap();
                let d2: Decimal = rd.try_into().unwrap();
                assert_eq!(d, d2, "Roundtrip failed for {}", s);
            }
        }

        #[test]
        fn test_rust_decimal_arithmetic() {
            use rust_decimal::Decimal as RustDecimal;

            // Start with decimal-bytes values
            let a = Decimal::from_str("100.50").unwrap();
            let b = Decimal::from_str("25.25").unwrap();

            // Convert to rust_decimal for arithmetic
            let ra: RustDecimal = (&a).try_into().unwrap();
            let rb: RustDecimal = (&b).try_into().unwrap();
            let sum = ra + rb;

            // Convert back to decimal-bytes for storage
            let result: Decimal = sum.try_into().unwrap();
            assert_eq!(result.to_string(), "125.75");
        }

        #[test]
        fn test_rust_decimal_from_owned() {
            use rust_decimal::Decimal as RustDecimal;

            // Test TryFrom<Decimal> (owned) for RustDecimal
            let d = Decimal::from_str("456.789").unwrap();
            let rd: RustDecimal = d.try_into().unwrap();
            assert_eq!(rd.to_string(), "456.789");
        }

        #[test]
        fn test_rust_decimal_special_values_fail() {
            use rust_decimal::Decimal as RustDecimal;

            // Infinity cannot convert to rust_decimal
            let inf = Decimal::infinity();
            let result: Result<RustDecimal, _> = (&inf).try_into();
            assert!(result.is_err());

            // NaN cannot convert to rust_decimal
            let nan = Decimal::nan();
            let result: Result<RustDecimal, _> = (&nan).try_into();
            assert!(result.is_err());
        }
    }

    // ==================== bigdecimal Interop Tests ====================

    #[cfg(feature = "bigdecimal")]
    mod bigdecimal_tests {
        use super::*;

        #[test]
        fn test_from_bigdecimal() {
            use bigdecimal::BigDecimal;
            use std::str::FromStr;

            let bd = BigDecimal::from_str("123.45").unwrap();
            let d: Decimal = bd.try_into().unwrap();
            assert_eq!(d.to_string(), "123.45");
        }

        #[test]
        fn test_to_bigdecimal() {
            use bigdecimal::BigDecimal;

            let d = Decimal::from_str("123.45").unwrap();
            let bd: BigDecimal = (&d).try_into().unwrap();
            assert_eq!(bd.to_string(), "123.45");
        }

        #[test]
        fn test_bigdecimal_roundtrip() {
            use bigdecimal::BigDecimal;

            let values = vec!["0", "1", "-1", "123.456", "-999.999", "0.001"];

            for s in values {
                let d = Decimal::from_str(s).unwrap();
                let bd: BigDecimal = (&d).try_into().unwrap();
                let d2: Decimal = bd.try_into().unwrap();
                assert_eq!(d, d2, "Roundtrip failed for {}", s);
            }
        }

        #[test]
        fn test_bigdecimal_from_owned() {
            use bigdecimal::BigDecimal;

            // Test TryFrom<Decimal> (owned) for BigDecimal
            let d = Decimal::from_str("456.789").unwrap();
            let bd: BigDecimal = d.try_into().unwrap();
            assert_eq!(bd.to_string(), "456.789");
        }

        #[test]
        fn test_bigdecimal_from_ref() {
            use bigdecimal::BigDecimal;
            use std::str::FromStr;

            // Test TryFrom<&BigDecimal> for Decimal
            let bd = BigDecimal::from_str("789.012").unwrap();
            let d: Decimal = (&bd).try_into().unwrap();
            assert_eq!(d.to_string(), "789.012");
        }

        #[test]
        fn test_bigdecimal_special_values_fail() {
            use bigdecimal::BigDecimal;

            // Infinity cannot convert to BigDecimal
            let inf = Decimal::infinity();
            let result: Result<BigDecimal, _> = (&inf).try_into();
            assert!(result.is_err());

            // NaN cannot convert to BigDecimal
            let nan = Decimal::nan();
            let result: Result<BigDecimal, _> = (&nan).try_into();
            assert!(result.is_err());
        }
    }
}