positive 0.7.1

A type-safe wrapper for guaranteed positive decimal values
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
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 05/10/26
******************************************************************************/

//! The [`StrictlyPositive`] type: a decimal that is always greater than zero.
//!
//! `StrictlyPositive` sits next to [`Positive`] rather than replacing it, so a
//! crate can hold both `> 0` and `>= 0` values at once. Its invariant does not
//! depend on any feature flag.
//!
//! # Design
//!
//! The type is a `#[repr(transparent)]` newtype over `Positive`, so its layout
//! is exactly that of a `Decimal`. Every operation delegates to the matching
//! `Positive` method and then re-checks the strict invariant in one place,
//! [`StrictlyPositive::lift`]. The arithmetic, the rounding and the overflow
//! handling therefore cannot drift between the two types.
//!
//! # Errors
//!
//! A value that is zero or negative is reported as
//! [`PositiveError::OutOfBounds`] whose `min` is [`StrictlyPositive::MIN`]
//! (`1e-28`, the smallest strictly positive `Decimal`) and whose `max` is
//! `Decimal::MAX`. The bounds are the same under every feature configuration.

use crate::error::PositiveError;
use crate::positive::{
    Positive, dec_add, overflow_panic, precision_panic, unwrap_or_panic, validate_precision,
};
use approx::{AbsDiffEq, RelativeEq};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt;
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub};
use std::str::FromStr;

/// A decimal value that is guaranteed to be strictly greater than zero.
///
/// Unlike [`Positive`], whose meaning changes with the deprecated `non-zero`
/// feature, `StrictlyPositive` always means `> 0`. Use it for quantities where
/// zero is as invalid as a negative number, such as prices, divisors or
/// volatilities, and keep `Positive` for quantities that may be zero.
///
/// # Examples
///
/// ```rust
/// use positive::{Positive, PositiveError, StrictlyPositive};
/// use rust_decimal_macros::dec;
///
/// struct Instrument {
///     price: StrictlyPositive, // > 0
///     daily_volume: Positive,  // >= 0
/// }
///
/// # fn main() -> Result<(), PositiveError> {
/// let instrument = Instrument {
///     price: StrictlyPositive::new_decimal(dec!(101.25))?,
///     daily_volume: Positive::new_decimal(dec!(1500))?,
/// };
/// assert!(instrument.price > dec!(0));
/// assert!(StrictlyPositive::new_decimal(dec!(0)).is_err());
/// # let _ = instrument.daily_volume;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct StrictlyPositive(Positive);

/// Panics with a uniform message when an operation on `StrictlyPositive`
/// would produce a value that is zero or negative.
#[cold]
#[inline(never)]
fn strict_invariant_panic(op: &'static str) -> ! {
    panic!("StrictlyPositive invariant broken in {op}: result would be zero or negative")
}

/// Panics with a uniform message when a clamp range is inverted.
#[cold]
#[inline(never)]
fn strict_inverted_range_panic() -> ! {
    panic!("StrictlyPositive clamp range is inverted: min is greater than max")
}

/// Builds the error reported for a value that breaks the strict invariant.
#[cold]
#[inline(never)]
fn strict_out_of_bounds(value: Decimal) -> PositiveError {
    PositiveError::out_of_bounds(value, StrictlyPositive::MIN.to_dec(), Decimal::MAX)
}

/// Converts a checked result into the panic documented on the panicking
/// wrapper. It is the single point at which a typed error on a
/// `StrictlyPositive` operation becomes a panic.
#[inline]
fn strict_unwrap_or_panic(
    result: Result<StrictlyPositive, PositiveError>,
    op: &'static str,
) -> StrictlyPositive {
    match result {
        Ok(value) => value,
        Err(PositiveError::OutOfBounds { .. }) => strict_invariant_panic(op),
        Err(PositiveError::ArithmeticError { .. }) => overflow_panic(op),
        Err(PositiveError::InvalidPrecision { precision, .. }) => precision_panic(precision),
        Err(
            error @ (PositiveError::InvalidValue { .. } | PositiveError::ConversionError { .. }),
        ) => panic!("StrictlyPositive {op} failed: {error}"),
    }
}

impl StrictlyPositive {
    /// The smallest value a `StrictlyPositive` can hold: `1e-28`, the smallest
    /// strictly positive `Decimal`.
    ///
    /// This is also the `min` reported by [`PositiveError::OutOfBounds`] when
    /// a value is rejected for being zero or negative.
    pub const MIN: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(1e-28));
    /// The largest value a `StrictlyPositive` can hold: `Decimal::MAX`.
    pub const MAX: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::MAX);
    /// The value one.
    pub const ONE: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::ONE);
    /// The value two.
    pub const TWO: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::TWO);
    /// The value three.
    pub const THREE: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(3));
    /// The value four.
    pub const FOUR: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(4));
    /// The value five.
    pub const FIVE: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(5));
    /// The value six.
    pub const SIX: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(6));
    /// The value seven.
    pub const SEVEN: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(7));
    /// The value eight.
    pub const EIGHT: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(8));
    /// The value nine.
    pub const NINE: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(9));
    /// The value ten.
    pub const TEN: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::TEN);
    /// The value one hundred.
    pub const HUNDRED: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(100));
    /// The value one thousand.
    pub const THOUSAND: StrictlyPositive = StrictlyPositive::from_decimal_const(dec!(1000));
    /// The mathematical constant π, at `Decimal` precision.
    pub const PI: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::PI);
    /// Euler's number e, at `Decimal` precision.
    pub const E: StrictlyPositive = StrictlyPositive::from_decimal_const(Decimal::E);

    /// Stores a literal without validation, so constants can be built at
    /// compile time.
    ///
    /// Crate-private on purpose: every caller passes a literal that is
    /// strictly positive, which satisfies the `Positive` invariant under every
    /// feature configuration as well as this type's own.
    #[inline]
    #[must_use]
    const fn from_decimal_const(value: Decimal) -> Self {
        StrictlyPositive(Positive::from_decimal_const(value))
    }

    /// Wraps an already validated `Positive`, rejecting zero.
    ///
    /// Every path that yields a `StrictlyPositive` ends here, so the strict
    /// invariant is checked in exactly one place.
    #[inline]
    fn from_positive_checked(value: Positive) -> Result<Self, PositiveError> {
        if value.is_zero() {
            Err(strict_out_of_bounds(value.to_dec()))
        } else {
            Ok(StrictlyPositive(value))
        }
    }

    /// Lifts the result of a `Positive` operation into a `StrictlyPositive`.
    ///
    /// An `OutOfBounds` error from `Positive` is re-reported with this type's
    /// bounds, so the error a caller sees does not depend on the `non-zero`
    /// feature. Every other error passes through unchanged. `Deserialize`
    /// cannot use this (serde errors are opaque), see its docs.
    #[inline]
    fn lift(result: Result<Positive, PositiveError>) -> Result<Self, PositiveError> {
        match result {
            Ok(value) => Self::from_positive_checked(value),
            Err(PositiveError::OutOfBounds { value, .. }) => Err(strict_out_of_bounds(value)),
            Err(error) => Err(error),
        }
    }

    /// Creates a `StrictlyPositive` from a 64-bit floating-point number.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::InvalidValue`] when `value` is `NaN` or
    /// infinite, [`PositiveError::ConversionError`] when it is outside the
    /// range `Decimal` can represent, and [`PositiveError::OutOfBounds`] when
    /// it is zero or negative.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert!(StrictlyPositive::new(1.5).is_ok());
    /// assert!(matches!(
    ///     StrictlyPositive::new(0.0),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[must_use = "constructor returns a Result; ignoring it discards a validated invariant"]
    pub fn new(value: f64) -> Result<Self, PositiveError> {
        Self::lift(Positive::new(value))
    }

    /// Creates a `StrictlyPositive` from a `Decimal`.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero or
    /// negative. The reported value and bounds are exact `Decimal`s.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    /// use rust_decimal_macros::dec;
    ///
    /// assert!(StrictlyPositive::new_decimal(dec!(0.01)).is_ok());
    /// assert!(matches!(
    ///     StrictlyPositive::new_decimal(dec!(0)),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[must_use = "constructor returns a Result; ignoring it discards a validated invariant"]
    pub fn new_decimal(value: Decimal) -> Result<Self, PositiveError> {
        if value > Decimal::ZERO {
            Ok(StrictlyPositive::from_decimal_const(value))
        } else {
            Err(strict_out_of_bounds(value))
        }
    }

    /// Returns the inner value as a `Decimal`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::TWO.value(), dec!(2));
    /// ```
    #[inline]
    #[must_use]
    pub fn value(&self) -> Decimal {
        self.0.to_dec()
    }

    /// Returns the inner value as a `Decimal` (alias for [`Self::value`]).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::TWO.to_dec(), dec!(2));
    /// ```
    #[inline]
    #[must_use]
    pub fn to_dec(&self) -> Decimal {
        self.0.to_dec()
    }

    /// Returns the value as a [`Positive`]. This never fails: every strictly
    /// positive value is also positive.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.to_positive(), Positive::TWO);
    /// ```
    #[inline]
    #[must_use]
    pub fn to_positive(&self) -> Positive {
        self.0
    }

    /// Converts the value to an `f64`.
    ///
    /// Infallible but lossy beyond `f64`'s ~15 significant digits; see
    /// [`Positive::to_f64`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TWO.to_f64(), 2.0);
    /// ```
    #[inline]
    #[must_use]
    pub fn to_f64(&self) -> f64 {
        self.0.to_f64()
    }

    /// Converts the value to an `i64`, truncating toward zero. Returns `None`
    /// when the truncated value exceeds `i64::MAX`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TEN.to_i64_checked(), Some(10));
    /// assert_eq!(StrictlyPositive::MAX.to_i64_checked(), None);
    /// ```
    #[inline]
    #[must_use]
    pub fn to_i64_checked(&self) -> Option<i64> {
        self.0.to_i64_checked()
    }

    /// Converts the value to a `u64`, truncating toward zero. Returns `None`
    /// when the truncated value exceeds `u64::MAX`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TEN.to_u64_checked(), Some(10));
    /// ```
    #[inline]
    #[must_use]
    pub fn to_u64_checked(&self) -> Option<u64> {
        self.0.to_u64_checked()
    }

    /// Converts the value to a `usize`, truncating toward zero. Returns
    /// `None` when the truncated value exceeds `usize::MAX`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TEN.to_usize_checked(), Some(10));
    /// ```
    #[inline]
    #[must_use]
    pub fn to_usize_checked(&self) -> Option<usize> {
        self.0.to_usize_checked()
    }

    // -----------------------------------------------------------------------
    // Checked arithmetic
    // -----------------------------------------------------------------------

    /// Adds two `StrictlyPositive` values. Non-panicking counterpart of `+`.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the sum overflows
    /// `Decimal`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_add(&StrictlyPositive::ONE), Ok(StrictlyPositive::THREE));
    /// assert!(matches!(
    ///     StrictlyPositive::MAX.checked_add(&StrictlyPositive::ONE),
    ///     Err(PositiveError::ArithmeticError { .. })
    /// ));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the overflow error"]
    pub fn checked_add(&self, rhs: &Self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_add(&rhs.0))
    }

    /// Adds a [`Positive`] to this value. The sum of a strictly positive and
    /// a non-negative value is strictly positive, so the result keeps this
    /// type. Non-panicking counterpart of `StrictlyPositive + Positive`.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the sum overflows
    /// `Decimal`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// let sum = StrictlyPositive::ONE.checked_add_positive(&Positive::TWO);
    /// assert_eq!(sum, Ok(StrictlyPositive::THREE));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the overflow error"]
    pub fn checked_add_positive(&self, rhs: &Positive) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_add(rhs))
    }

    /// Subtracts `rhs`, requiring the difference to stay strictly positive.
    ///
    /// The `-` operator returns a [`Positive`] instead, because the
    /// difference of two strictly positive values may be zero.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when the difference is zero or
    /// negative.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::THREE.checked_sub(&StrictlyPositive::ONE), Ok(StrictlyPositive::TWO));
    /// assert!(matches!(
    ///     StrictlyPositive::ONE.checked_sub(&StrictlyPositive::ONE),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the invariant error"]
    pub fn checked_sub(&self, rhs: &Self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_sub(&rhs.0))
    }

    /// Multiplies two `StrictlyPositive` values. Non-panicking counterpart of
    /// `*`.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the product overflows
    /// `Decimal`, and [`PositiveError::OutOfBounds`] when it underflows to
    /// zero, as `1e-20 * 1e-20` does: `Decimal` keeps at most 28 decimal
    /// places.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_mul(&StrictlyPositive::THREE), Ok(StrictlyPositive::SIX));
    ///
    /// let tiny = StrictlyPositive::new_decimal(dec!(1e-20))?;
    /// assert!(matches!(tiny.checked_mul(&tiny), Err(PositiveError::OutOfBounds { .. })));
    /// # Ok::<(), PositiveError>(())
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the overflow error"]
    pub fn checked_mul(&self, rhs: &Self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_mul(&rhs.0))
    }

    /// Multiplies by a [`Positive`]. The result is a `Positive`, because the
    /// factor may be zero. Non-panicking counterpart of
    /// `StrictlyPositive * Positive`.
    ///
    /// # Errors
    ///
    /// Same as [`Positive::checked_mul`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_mul_positive(&Positive::THREE), Ok(Positive::SIX));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the overflow error"]
    pub fn checked_mul_positive(&self, rhs: &Positive) -> Result<Positive, PositiveError> {
        self.0.checked_mul(rhs)
    }

    /// Divides two `StrictlyPositive` values. Non-panicking counterpart of
    /// `/`.
    ///
    /// Rounds with [`crate::DIV_ROUNDING_STRATEGY`] (banker's rounding) at 28
    /// decimal places, exactly as [`Positive::checked_div`] does. The divisor
    /// cannot be zero.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the quotient overflows
    /// `Decimal`, and [`PositiveError::OutOfBounds`] when it underflows to
    /// zero, as `1e-28 / 10` does.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::SIX.checked_div(&StrictlyPositive::TWO), Ok(StrictlyPositive::THREE));
    /// assert!(matches!(
    ///     StrictlyPositive::MIN.checked_div(&StrictlyPositive::TEN),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the underflow error"]
    pub fn checked_div(&self, rhs: &Self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_div(&rhs.0))
    }

    /// Divides by a [`Positive`], returning a `Positive`. Non-panicking
    /// counterpart of `StrictlyPositive / Positive`.
    ///
    /// Rounds with [`crate::DIV_ROUNDING_STRATEGY`], like
    /// [`Positive::checked_div`].
    ///
    /// # Errors
    ///
    /// Same as [`Positive::checked_div`]; in particular, a zero divisor
    /// returns [`PositiveError::ArithmeticError`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::SIX.checked_div_positive(&Positive::TWO), Ok(Positive::THREE));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the division-by-zero error"]
    pub fn checked_div_positive(&self, rhs: &Positive) -> Result<Positive, PositiveError> {
        self.0.checked_div(rhs)
    }

    /// Sums a **non-empty** iterator of `StrictlyPositive` values.
    ///
    /// There is deliberately no `std::iter::Sum` impl: the sum of an empty
    /// iterator is zero, which this type cannot hold.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the iterator is empty
    /// or the total overflows `Decimal`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// let values = [StrictlyPositive::ONE, StrictlyPositive::TWO];
    /// assert_eq!(StrictlyPositive::checked_sum(values), Ok(StrictlyPositive::THREE));
    /// assert!(StrictlyPositive::checked_sum(Vec::<StrictlyPositive>::new()).is_err());
    /// ```
    #[must_use = "checked aggregation returns a Result; ignoring it silences the error"]
    pub fn checked_sum<I, T>(iter: I) -> Result<StrictlyPositive, PositiveError>
    where
        I: IntoIterator<Item = T>,
        T: Borrow<StrictlyPositive>,
    {
        let mut iter = iter.into_iter();
        let first = iter.next().ok_or_else(|| {
            PositiveError::arithmetic_error("sum", "an empty iterator has no strictly positive sum")
        })?;
        let mut total = first.borrow().to_dec();
        for value in iter {
            total = dec_add(total, value.borrow().to_dec(), "sum")?;
        }
        StrictlyPositive::new_decimal(total)
    }

    // -----------------------------------------------------------------------
    // Mathematical functions
    // -----------------------------------------------------------------------

    /// Square root.
    ///
    /// # Panics
    ///
    /// Panics when the root underflows to zero, which cannot happen for any
    /// representable input in practice. Use [`Self::checked_sqrt`] for the
    /// non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::new_decimal(dec!(16))?.sqrt(), StrictlyPositive::FOUR);
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[must_use]
    pub fn sqrt(&self) -> Self {
        strict_unwrap_or_panic(self.checked_sqrt(), "sqrt")
    }

    /// Square root, returning an error instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the root cannot be
    /// computed and [`PositiveError::OutOfBounds`] when it underflows to zero.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::FOUR.checked_sqrt(), Ok(StrictlyPositive::TWO));
    /// ```
    #[inline]
    #[must_use = "checked mathematics returns a Result; ignoring it silences the error"]
    pub fn checked_sqrt(&self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_sqrt())
    }

    /// Natural logarithm, as a `Decimal` (it is negative below one).
    ///
    /// Infallible: the only point outside the logarithm's domain is zero,
    /// which this type cannot hold.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal::Decimal;
    ///
    /// assert_eq!(StrictlyPositive::ONE.ln(), Decimal::ZERO);
    /// ```
    #[inline]
    #[must_use]
    pub fn ln(&self) -> Decimal {
        self.0.ln()
    }

    /// Base-10 logarithm, as a `Decimal` (it is negative below one).
    ///
    /// Infallible, for the same reason as [`Self::ln`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::HUNDRED.log10(), dec!(2));
    /// ```
    #[inline]
    #[must_use]
    pub fn log10(&self) -> Decimal {
        self.0.log10()
    }

    /// Computes e^x.
    ///
    /// # Panics
    ///
    /// Panics when the result overflows `Decimal`. Use [`Self::checked_exp`]
    /// for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert!(StrictlyPositive::ONE.exp() > StrictlyPositive::TWO);
    /// ```
    #[must_use]
    pub fn exp(&self) -> Self {
        strict_unwrap_or_panic(self.checked_exp(), "exp")
    }

    /// Computes e^x, returning an error instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the result overflows
    /// `Decimal`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert!(StrictlyPositive::ONE.checked_exp().is_ok());
    /// assert!(matches!(
    ///     StrictlyPositive::THOUSAND.checked_exp(),
    ///     Err(PositiveError::ArithmeticError { .. })
    /// ));
    /// ```
    #[inline]
    #[must_use = "checked mathematics returns a Result; ignoring it silences the overflow error"]
    pub fn checked_exp(&self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_exp())
    }

    /// Raises this value to a signed integer power.
    ///
    /// # Panics
    ///
    /// Panics when the power overflows or underflows to zero. Use
    /// [`Self::checked_powi`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TWO.powi(3), StrictlyPositive::EIGHT);
    /// ```
    #[must_use]
    pub fn powi(&self, n: i64) -> Self {
        strict_unwrap_or_panic(self.checked_powi(n), "powi")
    }

    /// Raises this value to a signed integer power, returning an error
    /// instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the power overflows
    /// and [`PositiveError::OutOfBounds`] when it underflows to zero.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_powi(3), Ok(StrictlyPositive::EIGHT));
    /// assert!(matches!(
    ///     StrictlyPositive::MIN.checked_powi(2),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the error"]
    pub fn checked_powi(&self, n: i64) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_powi(n))
    }

    /// Raises this value to an unsigned integer power.
    ///
    /// # Panics
    ///
    /// Panics when the power overflows or underflows to zero. Use
    /// [`Self::checked_powu`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TWO.powu(3), StrictlyPositive::EIGHT);
    /// ```
    #[must_use]
    pub fn powu(&self, n: u64) -> Self {
        strict_unwrap_or_panic(self.checked_powu(n), "powu")
    }

    /// Raises this value to an unsigned integer power, returning an error
    /// instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the power overflows
    /// and [`PositiveError::OutOfBounds`] when it underflows to zero.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_powu(3), Ok(StrictlyPositive::EIGHT));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the error"]
    pub fn checked_powu(&self, n: u64) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_powu(n))
    }

    /// Raises this value to a [`Positive`] power.
    ///
    /// # Panics
    ///
    /// Panics when the power cannot be computed, overflows, or underflows to
    /// zero. Use [`Self::checked_pow`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.pow(Positive::THREE), StrictlyPositive::EIGHT);
    /// ```
    #[must_use]
    pub fn pow(&self, n: Positive) -> Self {
        strict_unwrap_or_panic(self.checked_pow(n), "pow")
    }

    /// Raises this value to a [`Positive`] power, returning an error instead
    /// of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ArithmeticError`] when the power cannot be
    /// computed or overflows, and [`PositiveError::OutOfBounds`] when it
    /// underflows to zero.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{Positive, StrictlyPositive};
    ///
    /// assert_eq!(StrictlyPositive::TWO.checked_pow(Positive::THREE), Ok(StrictlyPositive::EIGHT));
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the error"]
    pub fn checked_pow(&self, n: Positive) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_pow(n))
    }

    /// Rounds down to the nearest integer.
    ///
    /// # Panics
    ///
    /// Panics for every value below one, whose floor is zero. Use
    /// [`Self::checked_floor`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::new_decimal(dec!(1.9))?.floor(), StrictlyPositive::ONE);
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[must_use]
    pub fn floor(&self) -> Self {
        strict_unwrap_or_panic(self.checked_floor(), "floor")
    }

    /// Rounds down to the nearest integer, returning an error instead of
    /// panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when the value is below one.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    /// use rust_decimal_macros::dec;
    ///
    /// let small = StrictlyPositive::new_decimal(dec!(0.3))?;
    /// assert!(matches!(small.checked_floor(), Err(PositiveError::OutOfBounds { .. })));
    /// # Ok::<(), PositiveError>(())
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the invariant error"]
    pub fn checked_floor(&self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_floor())
    }

    /// Rounds up to the nearest integer. The result is at least one, so this
    /// cannot break the invariant.
    ///
    /// # Panics
    ///
    /// Never panics in practice: the ceiling of a strictly positive
    /// `Decimal` is at least one and never exceeds `Decimal::MAX`. The panic
    /// path exists only so the method shares the checked core. Use
    /// [`Self::checked_ceiling`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::new_decimal(dec!(0.3))?.ceiling(), StrictlyPositive::ONE);
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[must_use]
    pub fn ceiling(&self) -> Self {
        strict_unwrap_or_panic(self.checked_ceiling(), "ceiling")
    }

    /// Rounds up to the nearest integer, returning an error instead of
    /// panicking.
    ///
    /// # Errors
    ///
    /// Same as [`Positive::checked_ceiling`]; no strictly positive input is
    /// known to fail.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// let small = StrictlyPositive::new_decimal(dec!(0.3))?;
    /// assert_eq!(small.checked_ceiling(), Ok(StrictlyPositive::ONE));
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the error"]
    pub fn checked_ceiling(&self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_ceiling())
    }

    /// Rounds to the nearest integer.
    ///
    /// # Panics
    ///
    /// Panics for every value that rounds to zero, such as `0.3`. Use
    /// [`Self::checked_round`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// assert_eq!(StrictlyPositive::new_decimal(dec!(1.6))?.round(), StrictlyPositive::TWO);
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[must_use]
    pub fn round(&self) -> Self {
        strict_unwrap_or_panic(self.checked_round(), "round")
    }

    /// Rounds to the nearest integer, returning an error instead of
    /// panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when the value rounds to zero.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    /// use rust_decimal_macros::dec;
    ///
    /// let small = StrictlyPositive::new_decimal(dec!(0.3))?;
    /// assert!(matches!(small.checked_round(), Err(PositiveError::OutOfBounds { .. })));
    /// # Ok::<(), PositiveError>(())
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the invariant error"]
    pub fn checked_round(&self) -> Result<Self, PositiveError> {
        Self::lift(self.0.checked_round())
    }

    /// Rounds to `decimal_places` decimal places, using `Decimal`'s default
    /// (banker's) rounding.
    ///
    /// # Panics
    ///
    /// Panics when `decimal_places` exceeds 28, or when the value rounds to
    /// zero at that scale, such as `0.004` at two places. Use
    /// [`Self::checked_round_to`] for the non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    /// use rust_decimal_macros::dec;
    ///
    /// let value = StrictlyPositive::new_decimal(dec!(1.2345))?;
    /// assert_eq!(value.round_to(2).to_dec(), dec!(1.23));
    /// # Ok::<(), positive::PositiveError>(())
    /// ```
    #[must_use]
    pub fn round_to(&self, decimal_places: u32) -> Self {
        strict_unwrap_or_panic(self.checked_round_to(decimal_places), "round_to")
    }

    /// Rounds to `decimal_places` decimal places, returning an error instead
    /// of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::InvalidPrecision`] when `decimal_places`
    /// exceeds 28, and [`PositiveError::OutOfBounds`] when the value rounds
    /// to zero at that scale.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    /// use rust_decimal_macros::dec;
    ///
    /// let small = StrictlyPositive::new_decimal(dec!(0.004))?;
    /// assert!(matches!(small.checked_round_to(2), Err(PositiveError::OutOfBounds { .. })));
    /// assert_eq!(small.checked_round_to(3).map(|v| v.to_dec()), Ok(dec!(0.004)));
    /// # Ok::<(), PositiveError>(())
    /// ```
    #[inline]
    #[must_use = "checked arithmetic returns a Result; ignoring it silences the invariant error"]
    pub fn checked_round_to(&self, decimal_places: u32) -> Result<Self, PositiveError> {
        let decimal_places = validate_precision(decimal_places)?;
        Self::lift(self.0.checked_round_to(decimal_places))
    }

    /// Returns the larger of two values.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::ONE.max(StrictlyPositive::TWO), StrictlyPositive::TWO);
    /// ```
    #[inline]
    #[must_use]
    pub fn max(self, other: Self) -> Self {
        if self >= other { self } else { other }
    }

    /// Returns the smaller of two values.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// assert_eq!(StrictlyPositive::ONE.min(StrictlyPositive::TWO), StrictlyPositive::ONE);
    /// ```
    #[inline]
    #[must_use]
    pub fn min(self, other: Self) -> Self {
        if self <= other { self } else { other }
    }

    /// Clamps the value to `[min, max]`.
    ///
    /// # Panics
    ///
    /// Panics when `min > max`. Use [`Self::checked_clamp`] for the
    /// non-panicking form.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::StrictlyPositive;
    ///
    /// let clamped = StrictlyPositive::TEN.clamp(StrictlyPositive::ONE, StrictlyPositive::FIVE);
    /// assert_eq!(clamped, StrictlyPositive::FIVE);
    /// ```
    #[must_use]
    pub fn clamp(self, min: Self, max: Self) -> Self {
        match self.checked_clamp(min, max) {
            Ok(clamped) => clamped,
            Err(_) => strict_inverted_range_panic(),
        }
    }

    /// Clamps the value to `[min, max]`, returning an error instead of
    /// panicking on an inverted range.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `min > max`. As with
    /// [`Positive::checked_clamp`], `value` is the requested `min` and the
    /// bounds describe the range it had to fall within.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use positive::{PositiveError, StrictlyPositive};
    ///
    /// assert!(matches!(
    ///     StrictlyPositive::TWO.checked_clamp(StrictlyPositive::FIVE, StrictlyPositive::ONE),
    ///     Err(PositiveError::OutOfBounds { .. })
    /// ));
    /// ```
    #[must_use = "checked clamping returns a Result; ignoring it silences the inverted-range error"]
    pub fn checked_clamp(self, min: Self, max: Self) -> Result<Self, PositiveError> {
        if min > max {
            return Err(PositiveError::out_of_bounds(
                min.to_dec(),
                StrictlyPositive::MIN.to_dec(),
                max.to_dec(),
            ));
        }
        Ok(if self < min {
            min
        } else if self > max {
            max
        } else {
            self
        })
    }
}

// ===========================================================================
// Formatting
// ===========================================================================

impl fmt::Display for StrictlyPositive {
    /// Renders the value exactly as [`Positive`] does, honouring a precision
    /// such as `{:.2}`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl fmt::Debug for StrictlyPositive {
    /// Renders the normalised decimal, as [`Positive`]'s `Debug` does.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

// ===========================================================================
// Serde
// ===========================================================================

impl Serialize for StrictlyPositive {
    /// Serialises the exact decimal string, the same wire format as
    /// [`Positive`].
    ///
    /// # Errors
    ///
    /// Propagates any error from the serializer.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        Serialize::serialize(&self.0, serializer)
    }
}

impl<'de> Deserialize<'de> for StrictlyPositive {
    /// Deserialises with the same rules as [`Positive`] (exact decimal string,
    /// or a legacy number), then rejects zero.
    ///
    /// # Errors
    ///
    /// Fails when the input is not a valid decimal, or is zero or negative,
    /// under every feature configuration. A negative input is rejected by
    /// [`Positive`]'s deserializer, so its message reports `Positive`'s
    /// bounds for the active feature configuration.
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = Positive::deserialize(deserializer)?;
        StrictlyPositive::from_positive_checked(value).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "utoipa")]
impl utoipa::PartialSchema for StrictlyPositive {
    /// A string holding the exact decimal, like [`Positive`]'s schema. The
    /// strict lower bound is stated in the description: JSON Schema's
    /// `exclusiveMinimum` applies to numbers and is ignored for strings.
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        utoipa::openapi::ObjectBuilder::new()
            .schema_type(utoipa::openapi::schema::Type::String)
            .description(Some(
                "A decimal value that is guaranteed to be strictly greater than zero, \
                 serialised as an exact decimal string.",
            ))
            .into()
    }
}

#[cfg(feature = "utoipa")]
impl utoipa::ToSchema for StrictlyPositive {
    fn name() -> std::borrow::Cow<'static, str> {
        std::borrow::Cow::Borrowed("StrictlyPositive")
    }
}

// ===========================================================================
// Conversions
// ===========================================================================

impl FromStr for StrictlyPositive {
    type Err = PositiveError;

    /// Parses a `StrictlyPositive` from its decimal text representation.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::InvalidValue`] when `s` is not a decimal and
    /// [`PositiveError::OutOfBounds`] when it is zero or negative.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::lift(Positive::from_str(s))
    }
}

impl From<StrictlyPositive> for Positive {
    #[inline]
    fn from(value: StrictlyPositive) -> Self {
        value.0
    }
}

impl From<StrictlyPositive> for Decimal {
    #[inline]
    fn from(value: StrictlyPositive) -> Self {
        value.to_dec()
    }
}

impl From<StrictlyPositive> for f64 {
    /// Infallible, but lossy beyond `f64`'s ~15 significant digits.
    #[inline]
    fn from(value: StrictlyPositive) -> Self {
        value.to_f64()
    }
}

impl AsRef<Positive> for StrictlyPositive {
    #[inline]
    fn as_ref(&self) -> &Positive {
        &self.0
    }
}

impl TryFrom<Positive> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero.
    #[inline]
    fn try_from(value: Positive) -> Result<Self, Self::Error> {
        StrictlyPositive::from_positive_checked(value)
    }
}

impl TryFrom<Decimal> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero or
    /// negative.
    #[inline]
    fn try_from(value: Decimal) -> Result<Self, Self::Error> {
        StrictlyPositive::new_decimal(value)
    }
}

impl TryFrom<&Decimal> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero or
    /// negative.
    #[inline]
    fn try_from(value: &Decimal) -> Result<Self, Self::Error> {
        StrictlyPositive::new_decimal(*value)
    }
}

impl TryFrom<f64> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Same as [`StrictlyPositive::new`].
    #[inline]
    fn try_from(value: f64) -> Result<Self, Self::Error> {
        StrictlyPositive::new(value)
    }
}

impl TryFrom<i64> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero or
    /// negative.
    #[inline]
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        StrictlyPositive::new_decimal(Decimal::from(value))
    }
}

impl TryFrom<u64> for StrictlyPositive {
    type Error = PositiveError;

    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero.
    #[inline]
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        StrictlyPositive::new_decimal(Decimal::from(value))
    }
}

impl TryFrom<usize> for StrictlyPositive {
    type Error = PositiveError;

    /// Converts exactly, without going through `f64`.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::OutOfBounds`] when `value` is zero.
    #[inline]
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        StrictlyPositive::new_decimal(Decimal::from(value))
    }
}

impl TryFrom<StrictlyPositive> for u64 {
    type Error = PositiveError;

    /// Truncates toward zero.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ConversionError`] when the truncated value
    /// exceeds `u64::MAX`.
    #[inline]
    fn try_from(value: StrictlyPositive) -> Result<Self, Self::Error> {
        u64::try_from(value.0)
    }
}

impl TryFrom<StrictlyPositive> for i64 {
    type Error = PositiveError;

    /// Truncates toward zero.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ConversionError`] when the truncated value
    /// exceeds `i64::MAX`.
    #[inline]
    fn try_from(value: StrictlyPositive) -> Result<Self, Self::Error> {
        i64::try_from(value.0)
    }
}

impl TryFrom<StrictlyPositive> for usize {
    type Error = PositiveError;

    /// Truncates toward zero.
    ///
    /// # Errors
    ///
    /// Returns [`PositiveError::ConversionError`] when the truncated value
    /// exceeds `usize::MAX`.
    #[inline]
    fn try_from(value: StrictlyPositive) -> Result<Self, Self::Error> {
        usize::try_from(value.0)
    }
}

// ===========================================================================
// Comparisons
// ===========================================================================
//
// All exact, and all delegate to `Positive`'s impls so the two types agree.

impl PartialEq<Positive> for StrictlyPositive {
    #[inline]
    fn eq(&self, other: &Positive) -> bool {
        self.0 == *other
    }
}

impl PartialEq<StrictlyPositive> for Positive {
    #[inline]
    fn eq(&self, other: &StrictlyPositive) -> bool {
        *self == other.0
    }
}

impl PartialOrd<Positive> for StrictlyPositive {
    #[inline]
    fn partial_cmp(&self, other: &Positive) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialOrd<StrictlyPositive> for Positive {
    #[inline]
    fn partial_cmp(&self, other: &StrictlyPositive) -> Option<Ordering> {
        self.partial_cmp(&other.0)
    }
}

impl PartialEq<Decimal> for StrictlyPositive {
    #[inline]
    fn eq(&self, other: &Decimal) -> bool {
        self.0 == *other
    }
}

impl PartialEq<StrictlyPositive> for Decimal {
    #[inline]
    fn eq(&self, other: &StrictlyPositive) -> bool {
        *self == other.0
    }
}

impl PartialOrd<Decimal> for StrictlyPositive {
    #[inline]
    fn partial_cmp(&self, other: &Decimal) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialOrd<StrictlyPositive> for Decimal {
    #[inline]
    fn partial_cmp(&self, other: &StrictlyPositive) -> Option<Ordering> {
        self.partial_cmp(&other.0)
    }
}

impl PartialEq<f64> for StrictlyPositive {
    /// Exact comparison; see [`Positive`]'s `PartialEq<f64>`.
    #[inline]
    fn eq(&self, other: &f64) -> bool {
        self.0 == *other
    }
}

impl PartialEq<StrictlyPositive> for f64 {
    #[inline]
    fn eq(&self, other: &StrictlyPositive) -> bool {
        *self == other.0
    }
}

impl PartialOrd<f64> for StrictlyPositive {
    /// Exact comparison; `NaN` is unordered.
    #[inline]
    fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
        self.0.partial_cmp(other)
    }
}

impl PartialOrd<StrictlyPositive> for f64 {
    #[inline]
    fn partial_cmp(&self, other: &StrictlyPositive) -> Option<Ordering> {
        self.partial_cmp(&other.0)
    }
}

impl AbsDiffEq for StrictlyPositive {
    type Epsilon = Decimal;

    fn default_epsilon() -> Self::Epsilon {
        Positive::default_epsilon()
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
        self.0.abs_diff_eq(&other.0, epsilon)
    }
}

impl RelativeEq for StrictlyPositive {
    fn default_max_relative() -> Self::Epsilon {
        Positive::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.0.relative_eq(&other.0, epsilon, max_relative)
    }
}

// ===========================================================================
// Operators
// ===========================================================================
//
// Operators whose result is a `StrictlyPositive` are thin wrappers over the
// matching `checked_*` method. Operators whose result is a `Positive` delegate
// to `Positive`'s own operator, so they share its panics exactly.

impl Add for StrictlyPositive {
    type Output = StrictlyPositive;
    /// # Panics
    ///
    /// Panics on overflow. See [`StrictlyPositive::checked_add`].
    #[inline]
    fn add(self, rhs: StrictlyPositive) -> StrictlyPositive {
        strict_unwrap_or_panic(self.checked_add(&rhs), "add")
    }
}

impl Add<Positive> for StrictlyPositive {
    type Output = StrictlyPositive;
    /// # Panics
    ///
    /// Panics on overflow. See [`StrictlyPositive::checked_add_positive`].
    #[inline]
    fn add(self, rhs: Positive) -> StrictlyPositive {
        strict_unwrap_or_panic(self.checked_add_positive(&rhs), "add")
    }
}

impl Add<StrictlyPositive> for Positive {
    type Output = StrictlyPositive;
    /// # Panics
    ///
    /// Panics on overflow. See [`StrictlyPositive::checked_add_positive`].
    #[inline]
    fn add(self, rhs: StrictlyPositive) -> StrictlyPositive {
        strict_unwrap_or_panic(rhs.checked_add_positive(&self), "add")
    }
}

impl AddAssign for StrictlyPositive {
    /// # Panics
    ///
    /// Panics on overflow. See [`StrictlyPositive::checked_add`].
    #[inline]
    fn add_assign(&mut self, rhs: StrictlyPositive) {
        *self = strict_unwrap_or_panic(self.checked_add(&rhs), "add_assign");
    }
}

impl AddAssign<Positive> for StrictlyPositive {
    /// # Panics
    ///
    /// Panics on overflow. See [`StrictlyPositive::checked_add_positive`].
    #[inline]
    fn add_assign(&mut self, rhs: Positive) {
        *self = strict_unwrap_or_panic(self.checked_add_positive(&rhs), "add_assign");
    }
}

impl Sub for StrictlyPositive {
    type Output = Positive;
    /// Returns a [`Positive`], because the difference may be zero.
    ///
    /// # Panics
    ///
    /// Same as `Positive - Positive`: panics when the difference is negative,
    /// or zero under the deprecated `non-zero` feature. See
    /// [`Positive::checked_sub`], or [`StrictlyPositive::checked_sub`] to
    /// require a strictly positive difference.
    #[inline]
    fn sub(self, rhs: StrictlyPositive) -> Positive {
        unwrap_or_panic(self.0.checked_sub(&rhs.0), "sub")
    }
}

impl Mul for StrictlyPositive {
    type Output = StrictlyPositive;
    /// # Panics
    ///
    /// Panics on overflow, or when the product underflows to zero. See
    /// [`StrictlyPositive::checked_mul`].
    #[inline]
    fn mul(self, rhs: StrictlyPositive) -> StrictlyPositive {
        strict_unwrap_or_panic(self.checked_mul(&rhs), "mul")
    }
}

impl Mul<Positive> for StrictlyPositive {
    type Output = Positive;
    /// # Panics
    ///
    /// Same as `Positive * Positive`. See
    /// [`StrictlyPositive::checked_mul_positive`].
    #[inline]
    fn mul(self, rhs: Positive) -> Positive {
        self.0 * rhs
    }
}

impl Mul<StrictlyPositive> for Positive {
    type Output = Positive;
    /// # Panics
    ///
    /// Same as `Positive * Positive`. See [`Positive::checked_mul`].
    #[inline]
    fn mul(self, rhs: StrictlyPositive) -> Positive {
        self * rhs.0
    }
}

impl MulAssign for StrictlyPositive {
    /// # Panics
    ///
    /// Panics on overflow, or when the product underflows to zero. See
    /// [`StrictlyPositive::checked_mul`].
    #[inline]
    fn mul_assign(&mut self, rhs: StrictlyPositive) {
        *self = strict_unwrap_or_panic(self.checked_mul(&rhs), "mul_assign");
    }
}

impl Div for StrictlyPositive {
    type Output = StrictlyPositive;
    /// Rounds with [`crate::DIV_ROUNDING_STRATEGY`] (banker's rounding).
    ///
    /// # Panics
    ///
    /// Panics on overflow, or when the quotient underflows to zero. See
    /// [`StrictlyPositive::checked_div`].
    #[inline]
    fn div(self, rhs: StrictlyPositive) -> StrictlyPositive {
        strict_unwrap_or_panic(self.checked_div(&rhs), "div")
    }
}

impl Div<Positive> for StrictlyPositive {
    type Output = Positive;
    /// Rounds with [`crate::DIV_ROUNDING_STRATEGY`] (banker's rounding).
    ///
    /// # Panics
    ///
    /// Same as `Positive / Positive`, including on a zero divisor. See
    /// [`StrictlyPositive::checked_div_positive`].
    #[inline]
    fn div(self, rhs: Positive) -> Positive {
        self.0 / rhs
    }
}

impl Div<StrictlyPositive> for Positive {
    type Output = Positive;
    /// Rounds with [`crate::DIV_ROUNDING_STRATEGY`] (banker's rounding). The
    /// divisor is never zero.
    ///
    /// # Panics
    ///
    /// Same as `Positive / Positive`. See [`Positive::checked_div`].
    #[inline]
    fn div(self, rhs: StrictlyPositive) -> Positive {
        self / rhs.0
    }
}

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

    #[test]
    fn test_strictly_positive_repr_is_transparent_over_decimal() {
        assert_eq!(
            std::mem::size_of::<StrictlyPositive>(),
            std::mem::size_of::<Decimal>()
        );
    }

    #[test]
    fn test_strictly_positive_constants_are_strictly_positive() {
        let constants = [
            StrictlyPositive::MIN,
            StrictlyPositive::MAX,
            StrictlyPositive::ONE,
            StrictlyPositive::TWO,
            StrictlyPositive::THREE,
            StrictlyPositive::FOUR,
            StrictlyPositive::FIVE,
            StrictlyPositive::SIX,
            StrictlyPositive::SEVEN,
            StrictlyPositive::EIGHT,
            StrictlyPositive::NINE,
            StrictlyPositive::TEN,
            StrictlyPositive::HUNDRED,
            StrictlyPositive::THOUSAND,
            StrictlyPositive::PI,
            StrictlyPositive::E,
        ];
        for constant in constants {
            assert!(constant.to_dec() > Decimal::ZERO);
            assert_eq!(
                StrictlyPositive::new_decimal(constant.to_dec()),
                Ok(constant)
            );
        }
    }

    #[test]
    fn test_strictly_positive_min_is_smallest_decimal_step() {
        assert_eq!(StrictlyPositive::MIN.to_dec(), Decimal::new(1, 28));
    }

    #[test]
    fn test_strictly_positive_lift_relabels_bounds() {
        let err = StrictlyPositive::lift(Positive::new_decimal(dec!(-1)));
        assert_eq!(
            err,
            Err(PositiveError::OutOfBounds {
                value: dec!(-1),
                min: Decimal::new(1, 28),
                max: Decimal::MAX,
            })
        );
    }
}