libitofin 0.14.0

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
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
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
//! Bootstrap helpers for inflation term structures.
//!
//! Port of the helper base `ZeroCouponInflationSwapHelper` derives from
//! (`ql/termstructures/inflation/inflationhelpers.hpp:36-37`, a
//! `RelativeDateBootstrapHelper<ZeroInflationTermStructure>`) together with the
//! plain `BootstrapHelper<ZeroInflationTermStructure>` the traits name as their
//! helper type (`inflationtraits.hpp:43`) and its year-on-year twin
//! (`:121`). They are the inflation twins of
//! [`RateHelper`](crate::termstructures::bootstraphelper::RateHelper) and
//! [`DefaultProbabilityHelper`](crate::termstructures::credit::defaultprobabilityhelpers::DefaultProbabilityHelper),
//! and they carry no behaviour of their own: everything is inherited from the
//! shared [`BootstrapHelperBase`], instantiated here over
//! [`ZeroInflationTermStructure`].
//!
//! Where C++ gets every family from one class template, this port needs one
//! trait per family, because a trait generic over its term structure cannot be
//! made into the bare trait object the curves are typed on. The shared driver
//! reaches all three through [`BootstrapHelperShared`], implemented below on
//! `dyn ZeroInflationHelper`.
//!
//! [`ZeroCouponInflationSwapHelper`] (`inflationhelpers.hpp:35`) and
//! [`YearOnYearInflationSwapHelper`] (`:118`) are the concrete helpers, one per
//! family, and each plugs into its own base.
//!
//! ## Deferred within EPIC Inflation (#705)
//!
//! - The **start/end-date constructor** (`cpp:52-69`, #806), so every helper here
//!   rebuilds its schedule off the evaluation date, and the deprecated
//!   nominal-curve constructor (`cpp:71-86`).
//! - The `Pillar::CustomDate` choice, on both helpers (`cpp:140-150` and
//!   `cpp:271-281`, #808), which needs an explicit pillar date threaded through
//!   construction plus its bounds check.

use std::cell::{Ref, RefCell, RefMut};
use std::rc::Weak;

use crate::errors::QlResult;
use crate::handle::{Handle, RelinkableHandle};
use crate::indexes::Index;
use crate::indexes::inflationindex::{
    CpiInterpolationType, InflationIndex, YoYInflationIndex, ZeroInflationIndex, inflation_period,
};
use crate::instrument::Instrument;
use crate::instruments::{SwapType, YearOnYearInflationSwap, ZeroCouponInflationSwap};
use crate::interestrate::Compounding;
use crate::patterns::observable::AsObservable;
use crate::pricingengine::PricingEngine;
use crate::pricingengines::DiscountingSwapEngine;
use crate::quotes::Quote;
use crate::require;
use crate::settings::Settings;
use crate::shared::{Shared, SharedMut, shared, shared_mut};
use crate::termstructures::bootstraphelper::{BootstrapHelperBase, BootstrapHelperShared};
use crate::termstructures::inflation::inflationtermstructure::{
    YoYInflationTermStructure, ZeroInflationTermStructure,
};
use crate::termstructures::yields::{FlatForward, Pillar};
use crate::termstructures::yieldtermstructure::YieldTermStructure;
use crate::time::businessdayconvention::BusinessDayConvention;
use crate::time::calendar::Calendar;
use crate::time::calendars::NullCalendar;
use crate::time::date::Date;
use crate::time::daycounter::DayCounter;
use crate::time::frequency::Frequency;
use crate::time::period::Period;
use crate::time::schedule::MakeSchedule;
use crate::time::timeunit::TimeUnit;
use crate::types::Real;

/// The shared state of an inflation bootstrap helper: a
/// [`BootstrapHelperBase`] whose back-pointer is a zero-inflation curve.
pub type ZeroInflationHelperBase = BootstrapHelperBase<dyn ZeroInflationTermStructure>;

/// Bootstrap helper for the zero-inflation-curve bootstrap
/// (`BootstrapHelper<ZeroInflationTermStructure>`).
///
/// Mirrors [`RateHelper`](crate::termstructures::bootstraphelper::RateHelper)
/// exactly, over [`ZeroInflationTermStructure`]: a concrete helper embeds a
/// [`ZeroInflationHelperBase`], returns it from [`base`](Self::base) and
/// supplies [`implied_quote`](Self::implied_quote); the rest of the interface
/// is derived from the base. The same ownership contract holds - the curve is
/// held [`Weak`](std::rc::Weak) and never observed - since it is the one base
/// that enforces it.
pub trait ZeroInflationHelper: AsObservable {
    /// The embedded shared state.
    fn base(&self) -> &ZeroInflationHelperBase;

    /// The quote implied by the current curve, computed by the concrete helper.
    ///
    /// The helper does not observe the curve, so this must force any
    /// recalculation it needs itself rather than trusting a cached value.
    fn implied_quote(&self) -> QlResult<Real>;

    /// The market quote the helper fits the curve to.
    fn quote(&self) -> &Handle<dyn Quote> {
        self.base().quote()
    }

    /// The bootstrap's root: market quote minus implied quote, driven to zero.
    fn quote_error(&self) -> QlResult<Real> {
        Ok(self.base().quote_value()? - self.implied_quote()?)
    }

    /// Sets the curve being bootstrapped (non-owning, unobserved).
    ///
    /// A concrete helper that hands the curve to an instrument overrides this
    /// to relink that handle first, then delegates here.
    fn set_term_structure(&self, term_structure: &Shared<dyn ZeroInflationTermStructure>) {
        self.base().set_term_structure(term_structure);
    }

    /// The earliest date data are needed at.
    fn earliest_date(&self) -> Date {
        self.base().earliest_date()
    }

    /// The instrument's maturity date.
    fn maturity_date(&self) -> Date {
        self.base().maturity_date()
    }

    /// The latest date data are needed at.
    fn latest_relevant_date(&self) -> Date {
        self.base().latest_relevant_date()
    }

    /// The pillar date, at which the curve node this helper sets sits.
    fn pillar_date(&self) -> Date {
        self.base().pillar_date()
    }

    /// The latest date, equal to the pillar date.
    fn latest_date(&self) -> Date {
        self.base().latest_date()
    }
}

/// The shared state of a year-on-year inflation bootstrap helper: a
/// [`BootstrapHelperBase`] whose back-pointer is a year-on-year curve.
pub type YoYInflationHelperBase = BootstrapHelperBase<dyn YoYInflationTermStructure>;

/// Bootstrap helper for the year-on-year-inflation-curve bootstrap
/// (`BootstrapHelper<YoYInflationTermStructure>`, `inflationtraits.hpp:121`).
///
/// The zero twin above over [`YoYInflationTermStructure`], and separate from it
/// for the same reason the two curve families are separate: the term structure
/// a helper is handed is part of its interface, and a single trait generic over
/// it could not be made into the bare trait object
/// [`PiecewiseYoYInflationCurve`](super::piecewiseyoyinflationcurve::PiecewiseYoYInflationCurve)
/// names as its helper family.
///
/// [`YearOnYearInflationSwapHelper`] (`inflationhelpers.cpp:209-346`) is the
/// concrete helper implementing it.
pub trait YoYInflationHelper: AsObservable {
    /// The embedded shared state.
    fn base(&self) -> &YoYInflationHelperBase;

    /// The quote implied by the current curve, computed by the concrete helper.
    ///
    /// The helper does not observe the curve, so this must force any
    /// recalculation it needs itself rather than trusting a cached value.
    fn implied_quote(&self) -> QlResult<Real>;

    /// The market quote the helper fits the curve to.
    fn quote(&self) -> &Handle<dyn Quote> {
        self.base().quote()
    }

    /// The bootstrap's root: market quote minus implied quote, driven to zero.
    fn quote_error(&self) -> QlResult<Real> {
        Ok(self.base().quote_value()? - self.implied_quote()?)
    }

    /// Sets the curve being bootstrapped (non-owning, unobserved).
    ///
    /// A concrete helper that hands the curve to an instrument overrides this
    /// to relink that handle first, then delegates here.
    fn set_term_structure(&self, term_structure: &Shared<dyn YoYInflationTermStructure>) {
        self.base().set_term_structure(term_structure);
    }

    /// The earliest date data are needed at.
    fn earliest_date(&self) -> Date {
        self.base().earliest_date()
    }

    /// The instrument's maturity date.
    fn maturity_date(&self) -> Date {
        self.base().maturity_date()
    }

    /// The latest date data are needed at.
    fn latest_relevant_date(&self) -> Date {
        self.base().latest_relevant_date()
    }

    /// The pillar date, at which the curve node this helper sets sits.
    fn pillar_date(&self) -> Date {
        self.base().pillar_date()
    }

    /// The latest date, equal to the pillar date.
    fn latest_date(&self) -> Date {
        self.base().latest_date()
    }
}

/// Inflation bootstrap helper whose date schedule is relative to the
/// evaluation date (`RelativeDateBootstrapHelper<ZeroInflationTermStructure>`).
///
/// `ZeroCouponInflationSwapHelper` derives from this
/// (`inflationhelpers.hpp:36-37`): its swap schedule is rebuilt whenever the
/// evaluation date moves. The concrete helper builds its base with
/// [`BootstrapHelperBase::new_relative`], passing a closure that calls
/// [`initialize_dates`](Self::initialize_dates).
pub trait RelativeDateZeroInflationHelper: ZeroInflationHelper {
    /// Rebuilds the helper's date schedule off the current evaluation date.
    fn initialize_dates(&self);
}

/// The year-on-year twin of [`RelativeDateZeroInflationHelper`], the port of
/// `RelativeDateBootstrapHelper<YoYInflationTermStructure>`
/// (`inflationhelpers.hpp:119`).
///
/// [`YearOnYearInflationSwapHelper`] derives from it: its swap schedule runs
/// from the evaluation date, so the contract is rebuilt whenever that moves.
pub trait RelativeDateYoYInflationHelper: YoYInflationHelper {
    /// Rebuilds the helper's contract off the current evaluation date.
    fn initialize_dates(&self);
}

/// The inflation half of the driver bound. Like the yield and credit impls,
/// every method routes through the [`ZeroInflationHelper`] trait rather than
/// straight to the base, so a concrete helper's override still runs -
/// `set_term_structure` in particular, which a helper overrides to relink its
/// own pricing handle before recording the curve.
impl BootstrapHelperShared for dyn ZeroInflationHelper {
    type TS = dyn ZeroInflationTermStructure;

    fn set_term_structure(&self, term_structure: &Shared<dyn ZeroInflationTermStructure>) {
        ZeroInflationHelper::set_term_structure(self, term_structure);
    }

    fn quote_value(&self) -> QlResult<Real> {
        self.base().quote_value()
    }

    fn quote_error(&self) -> QlResult<Real> {
        ZeroInflationHelper::quote_error(self)
    }

    fn pillar_date(&self) -> Date {
        ZeroInflationHelper::pillar_date(self)
    }

    fn latest_relevant_date(&self) -> Date {
        ZeroInflationHelper::latest_relevant_date(self)
    }

    fn maturity_date(&self) -> Date {
        ZeroInflationHelper::maturity_date(self)
    }
}

/// The year-on-year half of the driver bound, routing through
/// [`YoYInflationHelper`] for the reason the zero impl routes through its own
/// trait: a concrete helper's `set_term_structure` override has to run.
impl BootstrapHelperShared for dyn YoYInflationHelper {
    type TS = dyn YoYInflationTermStructure;

    fn set_term_structure(&self, term_structure: &Shared<dyn YoYInflationTermStructure>) {
        YoYInflationHelper::set_term_structure(self, term_structure);
    }

    fn quote_value(&self) -> QlResult<Real> {
        self.base().quote_value()
    }

    fn quote_error(&self) -> QlResult<Real> {
        YoYInflationHelper::quote_error(self)
    }

    fn pillar_date(&self) -> Date {
        YoYInflationHelper::pillar_date(self)
    }

    fn latest_relevant_date(&self) -> Date {
        YoYInflationHelper::latest_relevant_date(self)
    }

    fn maturity_date(&self) -> Date {
        YoYInflationHelper::maturity_date(self)
    }
}

/// Bootstrap helper quoting a zero-coupon inflation swap
/// (`ZeroCouponInflationSwapHelper`, `inflationhelpers.hpp:35`).
///
/// The helper prices a unit-notional zero-strike swap of its own against the
/// curve being bootstrapped and reports that contract's
/// [`fair_rate`](ZeroCouponInflationSwap::fair_rate) as
/// [`implied_quote`](ZeroInflationHelper::implied_quote); the bootstrap drives
/// `quoted rate - fair rate` to zero. **Not** the contract's NPV: the fair rate
/// is the only quantity here that is discount-invariant, both legs paying on the
/// same adjusted maturity so their discount factors cancel (`cpp:66-68`).
///
/// That invariance is why the helper needs no nominal curve from its caller and
/// builds a flat 0 % one itself (`cpp:48`). The curve reaches the contract's
/// [`DiscountingSwapEngine`] and hence its NPV, which is consequently **not**
/// zero at the bootstrapped solution; [`fair_rate`](ZeroCouponInflationSwap::fair_rate)
/// reads the indexed flow directly and never consults it.
///
/// The helper prices through a copy of the caller's index
/// ([`clone_linked_to`](ZeroInflationIndex::clone_linked_to), `cpp:106`) linked to
/// its own relinkable handle, so [`set_term_structure`](ZeroInflationHelper::set_term_structure)
/// can point the copy at the curve under construction while the caller's index
/// keeps whatever curve it had. The copy is then unregistered from that handle
/// (`cpp:107-110`): a relink per solver step would otherwise notify the copy,
/// the copy the helper, and the helper the curve that is relinking it.
pub struct ZeroCouponInflationSwapHelper {
    base: ZeroInflationHelperBase,
    swap_obs_lag: Period,
    maturity: Date,
    calendar: Calendar,
    payment_convention: BusinessDayConvention,
    day_counter: DayCounter,
    index: Shared<ZeroInflationIndex>,
    observation_interpolation: CpiInterpolationType,
    nominal_term_structure: Handle<dyn YieldTermStructure>,
    term_structure_handle: RelinkableHandle<dyn ZeroInflationTermStructure>,
    settings: Shared<Settings<Date>>,
    swap: RefCell<QlResult<ZeroCouponInflationSwap>>,
}

impl ZeroCouponInflationSwapHelper {
    /// A helper on a swap maturing at `maturity` (`cpp:34-49`).
    ///
    /// The swap's start date is the evaluation date and follows it: this is the
    /// constructor C++ marks relative by passing a null start date (`cpp:100`),
    /// so the contract is rebuilt whenever the evaluation date moves.
    ///
    /// The helper observes its quote, the index copy it prices through, and the
    /// nominal curve (`cpp:173-174`); it does **not** observe the curve it is
    /// bootstrapped against.
    ///
    /// `pillar` picks which of the two nodes the interpolated swap straddles the
    /// helper fits (`cpp:118-139`); the flat swap reads one fixing, so its single
    /// node is the pillar whatever the choice (`cpp:154-159`). C++ defaults the
    /// argument to [`Pillar::LastRelevantDate`] (`hpp:48`).
    ///
    /// On the interpolated path C++ weights that choice by the swap's *start*
    /// date where it has one, falling back to the maturity (`cpp:132`), so that
    /// helpers sharing a start date all pick the same side. This constructor is
    /// the relative-date one, whose start date is the moving evaluation date and
    /// not a schedule input, so it is always the maturity arm - the fixed-date
    /// constructor that would supply the other is #806.
    ///
    /// # Errors
    ///
    /// The swap the helper prices is built here, so a `swap_obs_lag` the index
    /// cannot observe through fails at construction, as the C++ constructor's
    /// `initializeDates` throws. The interpolated path needs a further whole
    /// index period of lag on top of the index's availability lag
    /// (`cpp:165-171`), reading as it does the fixing of the month *after* the
    /// one the lag lands in; a pair of lags [`Period`]'s partial ordering cannot
    /// decide fails there too, where C++ throws out of the comparison itself.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        quote: Handle<dyn Quote>,
        swap_obs_lag: Period,
        maturity: Date,
        calendar: Calendar,
        payment_convention: BusinessDayConvention,
        day_counter: DayCounter,
        zii: &Shared<ZeroInflationIndex>,
        observation_interpolation: CpiInterpolationType,
        pillar: Pillar,
        settings: Shared<Settings<Date>>,
    ) -> QlResult<Shared<ZeroCouponInflationSwapHelper>> {
        let fixing_period = inflation_period(maturity - swap_obs_lag, zii.frequency())?;
        let (earliest_date, latest_date, pillar_date) = Self::dates(
            fixing_period,
            maturity,
            zii,
            observation_interpolation,
            pillar,
        )?;
        if observation_interpolation == CpiInterpolationType::Linear {
            let period_shift = Period::try_from(zii.frequency())?;
            let availability_lag = zii.availability_lag();
            let excess = swap_obs_lag - period_shift;
            require!(
                excess
                    .partial_cmp(&availability_lag)
                    .is_some_and(std::cmp::Ordering::is_ge),
                "inconsistency between swap observation lag {swap_obs_lag}, index period \
                 {period_shift} and index availability {availability_lag}: need (obsLag-index \
                 period) >= availLag"
            );
        }
        let nominal_term_structure = Handle::new(shared(FlatForward::moving_with_rate(
            0,
            NullCalendar::new(),
            0.0,
            day_counter.clone(),
            Compounding::Continuous,
            Frequency::Annual,
            Shared::clone(&settings),
        )) as Shared<dyn YieldTermStructure>);

        let helper = Shared::new_cyclic(|weak: &Weak<ZeroCouponInflationSwapHelper>| {
            let weak = weak.clone();
            let on_eval_change = Box::new(move || {
                if let Some(helper) = weak.upgrade() {
                    helper.initialize_dates();
                }
            });
            let base = ZeroInflationHelperBase::new_relative(
                quote,
                Shared::clone(&settings),
                true,
                on_eval_change,
            );
            let term_structure_handle = RelinkableHandle::empty();
            let index = shared(zii.clone_linked_to(term_structure_handle.handle()));
            term_structure_handle
                .handle()
                .unregister_observer(&index.inflation_base().observer());
            index.observable().register_observer(&base.observer());
            nominal_term_structure.register_observer(&base.observer());

            base.set_earliest_date(earliest_date);
            base.set_latest_date(latest_date);
            if let Some(pillar_date) = pillar_date {
                base.set_pillar_date(pillar_date);
            }

            let helper = ZeroCouponInflationSwapHelper {
                base,
                swap_obs_lag,
                maturity,
                calendar,
                payment_convention,
                day_counter,
                index,
                observation_interpolation,
                nominal_term_structure,
                term_structure_handle,
                settings,
                swap: RefCell::new(Err(crate::errors::QlError::new(
                    "the helper's swap is built by initialize_dates",
                    file!(),
                    line!(),
                ))),
            };
            helper.initialize_dates();
            helper
        });
        if let Err(error) = helper.swap.borrow().as_ref() {
            return Err(error.clone());
        }
        Ok(helper)
    }

    /// The helper's earliest and latest dates and its pillar, if it pins one
    /// (`cpp:114-160`).
    ///
    /// The flat swap reads a single fixing, so all three collapse onto the first
    /// day of the observed fixing period (`cpp:154-159`). The interpolated swap
    /// reads the fixings bracketing its observation date, so its window opens on
    /// that day and closes the day after the period ends (`cpp:115-116`), and the
    /// pillar goes to whichever end carries the dominant interpolation weight
    /// (`cpp:118-139`).
    ///
    /// `None` is the C++ `pillarDate_ == Date()` the `dt/dp > 0.5` arm leaves
    /// behind (`cpp:136-137`): the pillar is not the fixing period's start there,
    /// it is unset, and
    /// [`BootstrapHelperBase::pillar_date`](crate::termstructures::bootstraphelper::BootstrapHelperBase::pillar_date)
    /// answers with the latest date exactly as `bootstraphelper.hpp:193-197`
    /// does.
    fn dates(
        fixing_period: (Date, Date),
        maturity: Date,
        zii: &Shared<ZeroInflationIndex>,
        observation_interpolation: CpiInterpolationType,
        pillar: Pillar,
    ) -> QlResult<(Date, Date, Option<Date>)> {
        match observation_interpolation {
            CpiInterpolationType::Flat => {
                Ok((fixing_period.0, fixing_period.0, Some(fixing_period.0)))
            }
            CpiInterpolationType::Linear => {
                let latest_date = fixing_period.1 + 1;
                let pillar_date = match pillar {
                    Pillar::MaturityDate => Some(latest_date),
                    Pillar::LastRelevantDate => {
                        let weight_period = inflation_period(maturity, zii.frequency())?;
                        let dp = Real::from(weight_period.1 + 1 - weight_period.0);
                        let dt = Real::from(maturity - weight_period.0);
                        (dt / dp <= 0.5).then_some(fixing_period.0)
                    }
                };
                Ok((fixing_period.0, latest_date, pillar_date))
            }
        }
    }

    /// The cached swap, or the error that stopped it being built (`swap()`,
    /// `hpp:84`).
    pub fn swap(&self) -> Ref<'_, QlResult<ZeroCouponInflationSwap>> {
        self.swap.borrow()
    }

    /// The cached swap, mutably, for its on-demand pricing accessors.
    pub fn swap_mut(&self) -> RefMut<'_, QlResult<ZeroCouponInflationSwap>> {
        self.swap.borrow_mut()
    }

    /// The index copy the helper prices through, linked to its own handle.
    pub fn inflation_index(&self) -> &Shared<ZeroInflationIndex> {
        &self.index
    }

    /// The self-built flat 0 % curve the contract's engine discounts on
    /// (`cpp:48`).
    pub fn nominal_term_structure(&self) -> &Handle<dyn YieldTermStructure> {
        &self.nominal_term_structure
    }

    /// The unit-notional, zero-strike swap the helper quotes, under a discounting
    /// engine over the flat 0 % curve (`initializeDates`, `cpp:186-195`).
    ///
    /// The start date is the evaluation date, which a relative-date helper always
    /// tracks; C++ reads its own `evaluationDate_` there. An evaluation date that
    /// was never set is an error rather than a panic (D10), surfaced when the
    /// cached result is next read.
    fn build_swap(&self) -> QlResult<ZeroCouponInflationSwap> {
        let start_date = match self.base.evaluation_date() {
            Some(date) => date,
            None => crate::fail!("no evaluation date set: the helper's swap starts at it"),
        };
        let mut swap = ZeroCouponInflationSwap::new(
            SwapType::Payer,
            1.0,
            start_date,
            self.maturity,
            self.calendar.clone(),
            self.payment_convention,
            self.day_counter.clone(),
            0.0,
            Shared::clone(&self.index),
            self.swap_obs_lag,
            self.observation_interpolation,
            None,
            None,
            Shared::clone(&self.settings),
        )?;
        let engine = DiscountingSwapEngine::new(
            self.nominal_term_structure.clone(),
            None,
            None,
            None,
            Shared::clone(&self.settings),
        );
        swap.base_mut()
            .set_pricing_engine(shared_mut(engine) as SharedMut<dyn PricingEngine>);
        Ok(swap)
    }
}

impl AsObservable for ZeroCouponInflationSwapHelper {
    fn observable(&self) -> &crate::patterns::observable::Observable {
        self.base.observable()
    }
}

impl ZeroInflationHelper for ZeroCouponInflationSwapHelper {
    fn base(&self) -> &ZeroInflationHelperBase {
        &self.base
    }

    /// The swap's fair rate (`impliedQuote`, `cpp:181-184`).
    ///
    /// The `deepUpdate` is kept for fidelity but carries no value here: the fair
    /// rate is read off the indexed flow, which forecasts through the index copy
    /// on every call and caches nothing, so it already reflects a node the
    /// bootstrap has just moved.
    fn implied_quote(&self) -> QlResult<Real> {
        let mut swap = self.swap.borrow_mut();
        let swap = swap.as_mut().map_err(|error| error.clone())?;
        swap.swap_mut().deep_update();
        swap.fair_rate()
    }

    /// Points the index copy's handle at the curve, then records it
    /// (`setTermStructure`, `cpp:197-206`).
    ///
    /// The link is weak and unobserved, the port of the C++ `null_deleter` plus
    /// `observer = false`: the curve owns this helper, which owns the swap, which
    /// owns the index copy, and an owning link would close that ring.
    fn set_term_structure(&self, term_structure: &Shared<dyn ZeroInflationTermStructure>) {
        self.term_structure_handle
            .link_to_weak(Shared::downgrade(term_structure));
        self.base.set_term_structure(term_structure);
    }
}

impl RelativeDateZeroInflationHelper for ZeroCouponInflationSwapHelper {
    /// Rebuilds the swap off the current evaluation date (`initializeDates`,
    /// `cpp:186-195`).
    ///
    /// The helper's own dates are not rebuilt: they come from the fixing period
    /// of `maturity - swap_obs_lag`, which no evaluation date moves.
    fn initialize_dates(&self) {
        *self.swap.borrow_mut() = self.build_swap();
    }
}

/// Bootstrap helper quoting a year-on-year inflation swap
/// (`YearOnYearInflationSwapHelper`, `inflationhelpers.hpp:118-166`).
///
/// The helper prices a unit-notional, zero-strike [`YearOnYearInflationSwap`]
/// of its own against the curve being bootstrapped and reports that contract's
/// [`fair_rate`](YearOnYearInflationSwap::fair_rate) as
/// [`implied_quote`](YoYInflationHelper::implied_quote); the bootstrap drives
/// `quoted rate - fair rate` to zero.
///
/// ## It is handed its nominal curve; it does not build one
///
/// The zero-coupon twin above builds its own flat 0 % discount curve, which it
/// can because both of its legs pay one flow on the same date, so the discount
/// factors cancel out of its fair rate (`cpp:66-68`). That does not hold here:
/// this swap pays annually over its whole life, and the fair rate is a
/// discount-weighted average of the forward year-on-year rates. C++ therefore
/// takes the nominal curve as a constructor argument (`hpp:129`, member
/// `:165`) and hands it to the contract's [`DiscountingSwapEngine`]
/// (`cpp:333-334`), and so does this.
///
/// As in the zero twin, the helper prices through a copy of the caller's index
/// ([`clone_linked_to`](YoYInflationIndex::clone_linked_to), `cpp:246`) linked
/// to its own relinkable handle and unregistered from it (`cpp:247-250`), so
/// that a relink per solver step does not notify the curve that is relinking
/// it.
pub struct YearOnYearInflationSwapHelper {
    base: YoYInflationHelperBase,
    swap_obs_lag: Period,
    maturity: Date,
    calendar: Calendar,
    payment_convention: BusinessDayConvention,
    day_counter: DayCounter,
    yii: Shared<YoYInflationIndex>,
    interpolation: CpiInterpolationType,
    nominal_term_structure: Handle<dyn YieldTermStructure>,
    term_structure_handle: RelinkableHandle<dyn YoYInflationTermStructure>,
    settings: Shared<Settings<Date>>,
    swap: RefCell<QlResult<YearOnYearInflationSwap>>,
}

impl YearOnYearInflationSwapHelper {
    /// A helper on a swap maturing at `maturity` (`cpp:209-224`, delegating to
    /// `cpp:226-307`).
    ///
    /// The swap runs from the evaluation date, this being the constructor C++
    /// marks relative by passing a null start date (`cpp:222`), so the contract
    /// is rebuilt whenever the evaluation date moves.
    ///
    /// The helper observes its quote, the index copy it prices through and the
    /// nominal curve (`cpp:304-305`); it does **not** observe the curve it is
    /// bootstrapped against.
    ///
    /// `pillar` picks which of the two nodes the interpolated swap straddles the
    /// helper fits (`cpp:257-270`); the flat swap reads one fixing, so its
    /// single node is the pillar whatever the choice (`cpp:286-291`). C++
    /// defaults the argument to [`Pillar::LastRelevantDate`] (`hpp:130`).
    ///
    /// On the interpolated path C++ weights that choice by the swap's *start*
    /// date where it has one, falling back to the maturity (`cpp:263`), so that
    /// helpers sharing a start date all pick the same side. This constructor is
    /// the relative-date one, whose start date is the moving evaluation date and
    /// not a schedule input, so it is always the maturity arm - the fixed-date
    /// constructor that would supply the other is #806.
    ///
    /// # Errors
    ///
    /// The interpolated path needs a further whole index period of lag on top of
    /// the index's availability lag (`cpp:296-302`), reading as it does the
    /// fixing of the month *after* the one the lag lands in; a pair of lags
    /// [`Period`]'s partial ordering cannot decide fails there too, where C++
    /// throws out of the comparison itself. The swap the helper prices is also
    /// built here, so a `swap_obs_lag` its legs cannot be built under fails at
    /// construction, as the C++ `initializeDates` throws.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        quote: Handle<dyn Quote>,
        swap_obs_lag: Period,
        maturity: Date,
        calendar: Calendar,
        payment_convention: BusinessDayConvention,
        day_counter: DayCounter,
        yii: &Shared<YoYInflationIndex>,
        interpolation: CpiInterpolationType,
        nominal_term_structure: Handle<dyn YieldTermStructure>,
        pillar: Pillar,
        settings: Shared<Settings<Date>>,
    ) -> QlResult<Shared<YearOnYearInflationSwapHelper>> {
        let fixing_period = inflation_period(maturity - swap_obs_lag, yii.frequency())?;
        let (earliest_date, latest_date, pillar_date) =
            Self::dates(fixing_period, maturity, yii, interpolation, pillar)?;
        if interpolation == CpiInterpolationType::Linear {
            let period_shift = Period::try_from(yii.frequency())?;
            let availability_lag = yii.availability_lag();
            let excess = swap_obs_lag - period_shift;
            require!(
                excess
                    .partial_cmp(&availability_lag)
                    .is_some_and(std::cmp::Ordering::is_ge),
                "inconsistency between swap observation lag {swap_obs_lag}, index period \
                 {period_shift} and index availability {availability_lag}: need (obsLag-index \
                 period) >= availLag"
            );
        }

        let helper = Shared::new_cyclic(|weak: &Weak<YearOnYearInflationSwapHelper>| {
            let weak = weak.clone();
            let on_eval_change = Box::new(move || {
                if let Some(helper) = weak.upgrade() {
                    helper.initialize_dates();
                }
            });
            let base = YoYInflationHelperBase::new_relative(
                quote,
                Shared::clone(&settings),
                true,
                on_eval_change,
            );
            let term_structure_handle = RelinkableHandle::empty();
            let yii = shared(yii.clone_linked_to(term_structure_handle.handle()));
            term_structure_handle
                .handle()
                .unregister_observer(&yii.inflation_base().observer());
            yii.observable().register_observer(&base.observer());
            nominal_term_structure.register_observer(&base.observer());

            base.set_earliest_date(earliest_date);
            base.set_latest_date(latest_date);
            if let Some(pillar_date) = pillar_date {
                base.set_pillar_date(pillar_date);
            }

            let helper = YearOnYearInflationSwapHelper {
                base,
                swap_obs_lag,
                maturity,
                calendar,
                payment_convention,
                day_counter,
                yii,
                interpolation,
                nominal_term_structure,
                term_structure_handle,
                settings,
                swap: RefCell::new(Err(crate::errors::QlError::new(
                    "the helper's swap is built by initialize_dates",
                    file!(),
                    line!(),
                ))),
            };
            helper.initialize_dates();
            helper
        });
        if let Err(error) = helper.swap.borrow().as_ref() {
            return Err(error.clone());
        }
        Ok(helper)
    }

    /// The helper's earliest and latest dates and its pillar, if it pins one
    /// (`cpp:253-291`).
    ///
    /// The flat swap reads a single fixing, so all three collapse onto the first
    /// day of the observed fixing period (`cpp:286-291`). The interpolated swap
    /// reads the fixings bracketing its observation date, so its window opens on
    /// that day and closes the day after the period ends (`cpp:254-255`), and
    /// the pillar goes to whichever end carries the dominant interpolation
    /// weight (`cpp:257-270`).
    ///
    /// `None` is the C++ `pillarDate_ == Date()` the `dt/dp > 0.5` arm leaves
    /// behind (`cpp:267-268`): the pillar is not the fixing period's start
    /// there, it is unset, and
    /// [`BootstrapHelperBase::pillar_date`](crate::termstructures::bootstraphelper::BootstrapHelperBase::pillar_date)
    /// answers with the latest date exactly as `bootstraphelper.hpp:193-197`
    /// does.
    fn dates(
        fixing_period: (Date, Date),
        maturity: Date,
        yii: &Shared<YoYInflationIndex>,
        interpolation: CpiInterpolationType,
        pillar: Pillar,
    ) -> QlResult<(Date, Date, Option<Date>)> {
        match interpolation {
            CpiInterpolationType::Flat => {
                Ok((fixing_period.0, fixing_period.0, Some(fixing_period.0)))
            }
            CpiInterpolationType::Linear => {
                let latest_date = fixing_period.1 + 1;
                let pillar_date = match pillar {
                    Pillar::MaturityDate => Some(latest_date),
                    Pillar::LastRelevantDate => {
                        let weight_period = inflation_period(maturity, yii.frequency())?;
                        let dp = Real::from(weight_period.1 + 1 - weight_period.0);
                        let dt = Real::from(maturity - weight_period.0);
                        (dt / dp <= 0.5).then_some(fixing_period.0)
                    }
                };
                Ok((fixing_period.0, latest_date, pillar_date))
            }
        }
    }

    /// The cached swap, or the error that stopped it being built (`swap()`,
    /// `hpp:150`).
    pub fn swap(&self) -> Ref<'_, QlResult<YearOnYearInflationSwap>> {
        self.swap.borrow()
    }

    /// The cached swap, mutably, for its on-demand pricing accessors.
    pub fn swap_mut(&self) -> RefMut<'_, QlResult<YearOnYearInflationSwap>> {
        self.swap.borrow_mut()
    }

    /// The index copy the helper prices through, linked to its own handle.
    pub fn yoy_inflation_index(&self) -> &Shared<YoYInflationIndex> {
        &self.yii
    }

    /// The nominal curve the contract's engine discounts on, as handed in
    /// (`hpp:165`).
    pub fn nominal_term_structure(&self) -> &Handle<dyn YieldTermStructure> {
        &self.nominal_term_structure
    }

    /// The unit-notional, zero-strike swap the helper quotes, under a
    /// discounting engine over the nominal curve (`initializeDates`,
    /// `cpp:311-335`).
    ///
    /// Both legs run over one annual, unadjusted, backward-generated schedule
    /// from the evaluation date to the maturity; a one-year tenor never runs
    /// into a days-in-month mismatch, which is why C++ can share the schedule
    /// here while the contract itself takes two. An evaluation date that was
    /// never set is an error rather than a panic (D10), surfaced when the
    /// cached result is next read.
    fn build_swap(&self) -> QlResult<YearOnYearInflationSwap> {
        let start_date = match self.base.evaluation_date() {
            Some(date) => date,
            None => crate::fail!("no evaluation date set: the helper's swap starts at it"),
        };
        let schedule = MakeSchedule::new()
            .from(start_date)
            .to(self.maturity)
            .with_tenor(Period::new(1, TimeUnit::Years))
            .with_convention(BusinessDayConvention::Unadjusted)
            .with_calendar(self.calendar.clone())
            .backwards()
            .build();
        let mut swap = YearOnYearInflationSwap::new(
            SwapType::Payer,
            1.0,
            schedule.clone(),
            0.0,
            self.day_counter.clone(),
            schedule,
            Shared::clone(&self.yii),
            self.swap_obs_lag,
            self.interpolation,
            0.0,
            self.day_counter.clone(),
            self.calendar.clone(),
            self.payment_convention,
            Shared::clone(&self.settings),
        )?;
        let engine = DiscountingSwapEngine::new(
            self.nominal_term_structure.clone(),
            None,
            None,
            None,
            Shared::clone(&self.settings),
        );
        swap.base_mut()
            .set_pricing_engine(shared_mut(engine) as SharedMut<dyn PricingEngine>);
        Ok(swap)
    }
}

impl AsObservable for YearOnYearInflationSwapHelper {
    fn observable(&self) -> &crate::patterns::observable::Observable {
        self.base.observable()
    }
}

impl YoYInflationHelper for YearOnYearInflationSwapHelper {
    fn base(&self) -> &YoYInflationHelperBase {
        &self.base
    }

    /// The swap's fair rate (`impliedQuote`, `cpp:309-312`).
    ///
    /// The `deepUpdate` is load-bearing here, unlike in the zero twin: the fair
    /// rate is a *priced* result, cached on the contract behind its lazy flag,
    /// so a node the bootstrap has just moved would otherwise go unseen.
    fn implied_quote(&self) -> QlResult<Real> {
        let mut swap = self.swap.borrow_mut();
        let swap = swap.as_mut().map_err(|error| error.clone())?;
        swap.swap_mut().deep_update();
        swap.fair_rate()
    }

    /// Points the index copy's handle at the curve, then records it
    /// (`setTermStructure`, `cpp:337-346`).
    ///
    /// The link is weak and unobserved, the port of the C++ `null_deleter` plus
    /// `observer = false`, and for the same reason as in the zero twin: the
    /// curve owns this helper, which owns the swap, which owns the index copy.
    fn set_term_structure(&self, term_structure: &Shared<dyn YoYInflationTermStructure>) {
        self.term_structure_handle
            .link_to_weak(Shared::downgrade(term_structure));
        self.base.set_term_structure(term_structure);
    }
}

impl RelativeDateYoYInflationHelper for YearOnYearInflationSwapHelper {
    /// Rebuilds the swap off the current evaluation date (`initializeDates`,
    /// `cpp:311-335`).
    ///
    /// The helper's own dates are not rebuilt: they come from the fixing period
    /// of `maturity - swap_obs_lag`, which no evaluation date moves.
    fn initialize_dates(&self) {
        *self.swap.borrow_mut() = self.build_swap();
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;

    use super::*;
    use crate::patterns::observable::Observable;
    use crate::quotes::SimpleQuote;
    use crate::shared::shared;
    use crate::termstructures::inflation::interpolatedzeroinflationcurve::ZeroInflationCurve;
    use crate::time::date::Month;
    use crate::time::daycounters::actual360::Actual360;
    use crate::time::frequency::Frequency;

    /// A helper that overrides both hooks a concrete helper overrides, and
    /// records that each ran. Its quote is 0.03 and its implied quote 0.01, so
    /// the default `quote_error` would be 0.02 - a value the overridden one
    /// deliberately does not return.
    struct StubHelper {
        base: ZeroInflationHelperBase,
        curve_set: Cell<bool>,
        error_called: Cell<bool>,
    }

    impl StubHelper {
        fn new() -> Shared<StubHelper> {
            let quote = shared(SimpleQuote::new(Some(0.03)));
            let base = ZeroInflationHelperBase::new(Handle::new(quote));
            base.set_pillar_date(Date::new(1, Month::June, 2030));
            base.set_latest_relevant_date(Date::new(1, Month::July, 2030));
            base.set_maturity_date(Date::new(1, Month::June, 2030));
            shared(StubHelper {
                base,
                curve_set: Cell::new(false),
                error_called: Cell::new(false),
            })
        }
    }

    impl AsObservable for StubHelper {
        fn observable(&self) -> &Observable {
            self.base.observable()
        }
    }

    impl ZeroInflationHelper for StubHelper {
        fn base(&self) -> &ZeroInflationHelperBase {
            &self.base
        }

        fn implied_quote(&self) -> QlResult<Real> {
            Ok(0.01)
        }

        fn quote_error(&self) -> QlResult<Real> {
            self.error_called.set(true);
            Ok(-1.0)
        }

        fn set_term_structure(&self, term_structure: &Shared<dyn ZeroInflationTermStructure>) {
            self.curve_set.set(true);
            self.base.set_term_structure(term_structure);
        }
    }

    fn curve() -> Shared<dyn ZeroInflationTermStructure> {
        let reference = Date::new(27, Month::January, 2026);
        let dates = vec![
            Date::new(1, Month::December, 2025),
            Date::new(1, Month::December, 2030),
        ];
        let curve = ZeroInflationCurve::new(
            reference,
            dates,
            vec![0.02, 0.02],
            Frequency::Monthly,
            Actual360::new(),
            crate::math::interpolations::linear::Linear,
            None,
        )
        .unwrap();
        shared(curve) as Shared<dyn ZeroInflationTermStructure>
    }

    /// The inflation family satisfies the bound the bootstrap driver puts on
    /// `PiecewiseCurve::Helper`, so a piecewise zero-inflation curve can name
    /// `dyn ZeroInflationHelper` there against a
    /// `dyn ZeroInflationTermStructure` curve. The two associated types must
    /// agree, which is the whole point of the generalization, and only a paired
    /// instantiation checks it.
    #[test]
    fn inflation_helpers_satisfy_the_driver_bound() {
        fn accepts_driver_helper<H>()
        where
            H: BootstrapHelperShared<TS = dyn ZeroInflationTermStructure> + ?Sized,
        {
        }
        accepts_driver_helper::<dyn ZeroInflationHelper>();
    }

    /// What a driver sees of a helper.
    struct DriverView {
        quote_value: Real,
        quote_error: Real,
        pillar_date: Date,
        latest_relevant_date: Date,
        maturity_date: Date,
    }

    /// Exercises the helper the way
    /// [`IterativeBootstrap::calculate`](crate::termstructures::iterativebootstrap::IterativeBootstrap::calculate)
    /// does: through a type parameter bounded by [`BootstrapHelperShared`], so
    /// only that impl's methods are in scope.
    ///
    /// Reaching the bound through a type parameter is what makes the assertions
    /// below bite. Calling the same method names straight on a
    /// `Shared<dyn ZeroInflationHelper>` resolves to [`ZeroInflationHelper`]
    /// instead and never enters the impl under test, which would leave the
    /// routing claim untested.
    fn drive<H>(helper: &Shared<H>, curve: &Shared<dyn ZeroInflationTermStructure>) -> DriverView
    where
        H: BootstrapHelperShared<TS = dyn ZeroInflationTermStructure> + ?Sized,
    {
        helper.set_term_structure(curve);
        DriverView {
            quote_value: helper.quote_value().unwrap(),
            quote_error: helper.quote_error().unwrap(),
            pillar_date: helper.pillar_date(),
            latest_relevant_date: helper.latest_relevant_date(),
            maturity_date: helper.maturity_date(),
        }
    }

    /// Every driver-facing method routes through the [`ZeroInflationHelper`]
    /// trait, so a concrete helper's overrides run. Short-circuiting the impl
    /// to the base would leave a helper pricing off an unlinked handle and
    /// would silently swap its quote error for the default one - here, the
    /// stub's `-1.0` would become the default `0.03 - 0.01`. The curve is bound
    /// to a local because the base holds it weakly and never owns it.
    #[test]
    fn the_driver_bound_routes_through_the_trait_so_overrides_run() {
        let helper = StubHelper::new();
        let driver: Shared<dyn ZeroInflationHelper> = Shared::clone(&helper) as _;
        let curve = curve();

        let view = drive(&driver, &curve);

        assert!(
            helper.curve_set.get(),
            "set_term_structure override skipped"
        );
        assert!(helper.base.term_structure().is_ok());
        assert!(helper.error_called.get(), "quote_error override skipped");
        assert_eq!(view.quote_error, -1.0);
        assert_eq!(view.quote_value, 0.03);
    }

    #[test]
    fn the_driver_bound_reports_the_bases_dates() {
        let helper = StubHelper::new();
        let driver: Shared<dyn ZeroInflationHelper> = Shared::clone(&helper) as _;
        let curve = curve();

        let view = drive(&driver, &curve);

        assert_eq!(view.pillar_date, Date::new(1, Month::June, 2030));
        assert_eq!(view.latest_relevant_date, Date::new(1, Month::July, 2030));
        assert_eq!(view.maturity_date, Date::new(1, Month::June, 2030));
    }

    mod zero_coupon_swap_helper {
        //! The helper's place inside a bootstrap is exercised by the piecewise
        //! zero-inflation curve; what is checkable here is everything the helper
        //! decides on its own - its dates, the swap it caches, and the two
        //! wirings a bootstrap depends on but no numeric assertion would catch:
        //! that its index copy reads the curve handed to
        //! [`set_term_structure`](ZeroInflationHelper::set_term_structure), and
        //! that the relink does not travel back to the helper.

        use super::*;
        use crate::indexes::Index;
        use crate::indexes::inflation::UkRpi;
        use crate::instrument::Instrument;
        use crate::math::interpolations::linear::Linear;
        use crate::test_support::{Flag, as_observer};
        use crate::time::businessdayconvention::BusinessDayConvention;
        use crate::time::calendars::unitedkingdom::{Market, UnitedKingdom};
        use crate::time::date::Month::{August, June, May};
        use crate::time::daycounters::actual365fixed::Actual365Fixed;
        use crate::time::period::Period;
        use crate::time::timeunit::TimeUnit;
        use crate::types::Rate;

        /// The May 2007 figure, which the swap's base observation reads.
        const BASE_FIXING: Real = 195.0;
        /// The June 2007 figure, which is also the curve's base date.
        const CURVE_BASE_FIXING: Real = 200.0;

        fn today() -> Date {
            Date::new(13, Month::August, 2007)
        }

        /// One year out, so the fixing period observed under a three-month lag is
        /// May 2008 - a date the curve carries a node at.
        fn maturity() -> Date {
            Date::new(13, August, 2008)
        }

        fn curve_base_date() -> Date {
            Date::new(1, June, 2007)
        }

        fn lag() -> Period {
            Period::new(3, TimeUnit::Months)
        }

        fn settings_today() -> Shared<Settings<Date>> {
            let settings = shared(Settings::<Date>::new());
            settings.set_evaluation_date(today());
            settings
        }

        fn a_curve(rates: Vec<Rate>) -> Shared<dyn ZeroInflationTermStructure> {
            shared(
                ZeroInflationCurve::new(
                    today(),
                    vec![
                        curve_base_date(),
                        Date::new(1, May, 2008),
                        Date::new(1, June, 2012),
                    ],
                    rates,
                    Frequency::Monthly,
                    Actual360::new(),
                    Linear,
                    None,
                )
                .expect("a well-formed zero inflation curve"),
            ) as Shared<dyn ZeroInflationTermStructure>
        }

        /// UK RPI with the two figures the swap needs on record: its base
        /// observation and the curve's base date. The index is left on an empty
        /// curve handle, so any forecast it produced itself would fail - only the
        /// helper's own copy, relinked to the bootstrapped curve, can forecast.
        fn an_index(settings: &Shared<Settings<Date>>) -> Shared<ZeroInflationIndex> {
            let index = shared(UkRpi::new(Shared::clone(settings)));
            index
                .add_fixing(Date::new(1, May, 2007), BASE_FIXING)
                .expect("a published figure");
            index
                .add_fixing(curve_base_date(), CURVE_BASE_FIXING)
                .expect("a published figure");
            index
        }

        fn a_helper(
            settings: &Shared<Settings<Date>>,
            interpolation: CpiInterpolationType,
        ) -> QlResult<Shared<ZeroCouponInflationSwapHelper>> {
            a_helper_with(
                settings,
                interpolation,
                Pillar::LastRelevantDate,
                maturity(),
                lag(),
            )
        }

        fn a_helper_with(
            settings: &Shared<Settings<Date>>,
            interpolation: CpiInterpolationType,
            pillar: Pillar,
            maturity: Date,
            obs_lag: Period,
        ) -> QlResult<Shared<ZeroCouponInflationSwapHelper>> {
            ZeroCouponInflationSwapHelper::new(
                Handle::new(shared(SimpleQuote::new(Some(0.03))) as Shared<dyn Quote>),
                obs_lag,
                maturity,
                UnitedKingdom::new(Market::Settlement),
                BusinessDayConvention::ModifiedFollowing,
                Actual365Fixed::new(),
                &an_index(settings),
                interpolation,
                pillar,
                Shared::clone(settings),
            )
        }

        /// All four dates collapse onto the first day of the observed fixing
        /// period (`cpp:158-159`): 13 August 2008 less three months is 13 May
        /// 2008, whose monthly period starts on 1 May 2008. C++ leaves the
        /// relevant, maturity and pillar fields unset there and lets the base's
        /// fallbacks answer; this port sets the pillar explicitly - the same value
        /// - and leaves the other two to the fallbacks, as C++ does.
        ///
        /// The swap observes the *unrounded* 13 May 2008, the fixing period being
        /// the helper's rounding and not the contract's.
        #[test]
        fn the_dates_collapse_onto_the_observed_fixing_period() {
            let helper = a_helper(&settings_today(), CpiInterpolationType::Flat)
                .expect("a three-month lag covers UK RPI's availability");
            let period_start = Date::new(1, May, 2008);

            assert_eq!(helper.earliest_date(), period_start);
            assert_eq!(helper.latest_date(), period_start);
            assert_eq!(helper.pillar_date(), period_start);
            assert_eq!(helper.latest_relevant_date(), period_start);
            assert_eq!(helper.maturity_date(), period_start);

            let swap = helper.swap();
            let swap = swap.as_ref().expect("the swap builds");
            assert_eq!(swap.obs_date(), Date::new(13, May, 2008));
            assert_eq!(swap.maturity_date(), maturity());
            assert_eq!(swap.fixed_rate(), 0.0);
            assert_eq!(swap.nominal(), 1.0);
        }

        /// The contract starts at the evaluation date and follows it, which is
        /// what makes this a relative-date helper (`cpp:100`, `cpp:188`). The
        /// helper's own dates come from the maturity and so do not move.
        #[test]
        fn moving_the_evaluation_date_rebuilds_the_swap() {
            let settings = settings_today();
            let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a valid lag");
            assert_eq!(
                helper
                    .swap()
                    .as_ref()
                    .expect("the swap builds")
                    .start_date(),
                today()
            );

            let moved = Date::new(14, August, 2007);
            settings.set_evaluation_date(moved);

            assert_eq!(
                helper
                    .swap()
                    .as_ref()
                    .expect("the swap rebuilds")
                    .start_date(),
                moved
            );
            assert_eq!(helper.pillar_date(), Date::new(1, May, 2008));
        }

        /// The oracle, without a bootstrap: the quote the helper implies off a
        /// directly built curve is the fair rate of the same swap built by hand
        /// on that curve.
        ///
        /// It pins the whole chain the bootstrap relies on. The helper's index is
        /// a *copy* reading a handle that is empty until `set_term_structure`
        /// relinks it, so a copy that kept the caller's curve, or a relink that
        /// never reached the copy, would fail to forecast at all rather than
        /// answer a different number.
        #[test]
        fn the_implied_quote_is_the_swaps_fair_rate_on_the_curve_it_is_given() {
            let settings = settings_today();
            let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a valid lag");
            let curve = a_curve(vec![0.02, 0.03, 0.04]);

            assert!(
                helper.implied_quote().is_err(),
                "no curve has been handed over yet"
            );
            ZeroInflationHelper::set_term_structure(helper.as_ref(), &curve);

            let by_hand = ZeroCouponInflationSwap::new(
                SwapType::Payer,
                1.0,
                today(),
                maturity(),
                UnitedKingdom::new(Market::Settlement),
                BusinessDayConvention::ModifiedFollowing,
                Actual365Fixed::new(),
                0.0,
                shared(an_index(&settings).clone_linked_to(Handle::new(Shared::clone(&curve)))),
                lag(),
                CpiInterpolationType::Flat,
                None,
                None,
                Shared::clone(&settings),
            )
            .expect("a valid lag");

            let implied = helper.implied_quote().expect("the curve forecasts");
            assert!(implied > 0.0);
            assert!(
                (implied - by_hand.fair_rate().expect("the curve forecasts")).abs() < 1e-14,
                "implied {implied}"
            );
        }

        /// The H7 hazard, pinned: the helper's own swap is struck at zero on a
        /// flat 0 % nominal curve, so its NPV is nowhere near zero at the same
        /// point where the implied quote is a perfectly good rate. A bootstrap
        /// written against `helper.swap().npv() == 0` would never converge.
        ///
        /// The curve is bound to a local: the helper links it weakly, so a
        /// temporary would be dropped before the quote is read.
        #[test]
        fn the_cached_swap_does_not_price_to_zero_at_the_implied_quote() {
            let settings = settings_today();
            let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a valid lag");
            let curve = a_curve(vec![0.02, 0.03, 0.04]);
            ZeroInflationHelper::set_term_structure(helper.as_ref(), &curve);

            let implied = helper.implied_quote().expect("the curve forecasts");
            let mut swap = helper.swap_mut();
            let npv = swap
                .as_mut()
                .expect("the swap builds")
                .npv()
                .expect("the flat nominal curve discounts");

            assert!(implied.is_finite() && implied > 0.0);
            assert!(npv.abs() > 0.01, "npv was {npv}");
        }

        /// `cpp:107-110` and `cpp:199`, asserted structurally: the helper must not
        /// hear about the curve it is being bootstrapped against. Two paths could
        /// carry the news back - the copy still observing the handle the helper
        /// relinks, and the relink itself subscribing to the curve - and both are
        /// closed here. Either one open turns a solver step into a notification
        /// the helper rebroadcasts to the curve that is moving it.
        #[test]
        fn the_relink_reaches_neither_the_helper_nor_its_index_copy() {
            let helper =
                a_helper(&settings_today(), CpiInterpolationType::Flat).expect("a valid lag");
            let curve = a_curve(vec![0.02, 0.03, 0.04]);

            let on_helper = Flag::new();
            helper
                .observable()
                .register_observer(&as_observer(&on_helper));
            let on_index = Flag::new();
            helper
                .inflation_index()
                .observable()
                .register_observer(&as_observer(&on_index));

            ZeroInflationHelper::set_term_structure(helper.as_ref(), &curve);
            assert!(
                !Flag::is_up(&on_index),
                "the copy still observes the handle"
            );
            assert!(!Flag::is_up(&on_helper), "the relink reached the helper");

            curve.observable().notify_observers();
            assert!(
                !Flag::is_up(&on_helper),
                "the helper must not observe the curve it is bootstrapped against"
            );
        }

        /// The interpolated window straddles the observed fixing period
        /// (`cpp:115-116`). The swap observes 13 May 2008, whose monthly period is
        /// 1 to 31 May, and it reads the fixings bracketing that day: the May
        /// figure, dated 1 May 2008, and the June one, dated the day after May's
        /// period ends - 1 June 2008. The flat helper collapses both onto 1 May.
        ///
        /// The relevant and maturity dates follow the far end rather than the
        /// near one, both falling back to the latest date
        /// (`bootstraphelper.hpp:190-197`), and it is the relevant date that sets
        /// how far a bootstrapped curve reaches.
        #[test]
        fn the_interpolated_dates_straddle_the_observed_fixing_period() {
            let helper = a_helper(&settings_today(), CpiInterpolationType::Linear)
                .expect("a three-month lag leaves a month over UK RPI's availability");

            assert_eq!(helper.earliest_date(), Date::new(1, May, 2008));
            assert_eq!(helper.latest_date(), Date::new(1, June, 2008));
            assert_eq!(helper.latest_relevant_date(), Date::new(1, June, 2008));
            assert_eq!(helper.maturity_date(), Date::new(1, June, 2008));
        }

        /// [`Pillar::MaturityDate`] pins the node at the window's far end
        /// (`cpp:119-121`), the June figure's date - not the near end the weighted
        /// choice picks for this same maturity below.
        #[test]
        fn the_maturity_date_pillar_is_the_windows_far_end() {
            let helper = a_helper_with(
                &settings_today(),
                CpiInterpolationType::Linear,
                Pillar::MaturityDate,
                maturity(),
                lag(),
            )
            .expect("a valid lag");

            assert_eq!(helper.pillar_date(), Date::new(1, June, 2008));
        }

        /// [`Pillar::LastRelevantDate`] pins the node at whichever end carries the
        /// dominant interpolation weight (`cpp:122-138`), weighed on the maturity
        /// because this relative-date helper has no schedule start date
        /// (`cpp:132`). 13 August 2008 falls 12 days into a 31-day month, so
        /// `dt/dp` is 12/31 = 0.387 and the near end wins: the pillar is the
        /// window's start, a full month before its far end.
        #[test]
        fn the_weighted_pillar_takes_the_windows_start_early_in_the_month() {
            let helper =
                a_helper(&settings_today(), CpiInterpolationType::Linear).expect("a valid lag");

            assert_eq!(helper.pillar_date(), Date::new(1, May, 2008));
        }

        /// The far side of the same threshold: 25 August 2008 falls 24 days into
        /// that 31-day month, `dt/dp` is 24/31 = 0.774, and the far end wins. C++
        /// leaves the pillar unset there rather than assigning one (`cpp:136-137`)
        /// and the base answers with the latest date
        /// (`bootstraphelper.hpp:193-197`); this port does the same. The window
        /// itself does not move - 25 May 2008 sits in the same May period - so the
        /// weight is the only thing the later maturity changes.
        #[test]
        fn the_weighted_pillar_takes_the_windows_far_end_late_in_the_month() {
            let helper = a_helper_with(
                &settings_today(),
                CpiInterpolationType::Linear,
                Pillar::LastRelevantDate,
                Date::new(25, August, 2008),
                lag(),
            )
            .expect("a valid lag");

            assert_eq!(helper.earliest_date(), Date::new(1, May, 2008));
            assert_eq!(helper.pillar_date(), Date::new(1, June, 2008));
        }

        /// The interpolated path needs a whole index period of observation lag on
        /// top of the index's availability lag (`cpp:165-171`), since it reads the
        /// month *after* the one the lag lands in. UK RPI publishes a month in
        /// arrears, so a one-month lag leaves nothing for that second figure.
        ///
        /// The message names all three quantities, which is what tells this apart
        /// from the swap's own availability failure - the flat path builds on the
        /// same one-month lag.
        #[test]
        fn an_interpolated_lag_short_of_an_index_period_is_rejected() {
            let settings = settings_today();
            let short_lag = Period::new(1, TimeUnit::Months);
            let error = a_helper_with(
                &settings,
                CpiInterpolationType::Linear,
                Pillar::LastRelevantDate,
                maturity(),
                short_lag,
            )
            .err()
            .expect("one month of lag cannot cover the interpolation");

            assert!(
                error
                    .message()
                    .contains("need (obsLag-index period) >= availLag"),
                "err was: {error}"
            );
            assert!(
                a_helper_with(
                    &settings,
                    CpiInterpolationType::Flat,
                    Pillar::LastRelevantDate,
                    maturity(),
                    short_lag,
                )
                .is_ok(),
                "the flat path has no such requirement"
            );
        }
    }
}
#[cfg(test)]
mod yoy_swap_helper_tests {
    //! The year-on-year helper's own wiring, off a **flat, directly built**
    //! curve linked onto its handle. `test-suite/inflation.cpp`'s oracle
    //! (`testYYTermStructure`, `:1109`) exercises it against a bootstrapped
    //! curve instead and lands with that curve's tests.
    //!
    //! A flat curve makes the implied quote the curve's own rate whatever the
    //! nominal curve discounts at, so this pins the plumbing - the index copy
    //! reaching the linked curve, the contract being built and priced - and
    //! *not* which nominal curve the engine got.

    use super::*;
    use crate::currency::Currency;
    use crate::indexes::Region;
    use crate::math::interpolations::linear::Linear;
    use crate::quotes::SimpleQuote;
    use crate::termstructures::inflation::interpolatedyoyinflationcurve::YoYInflationCurve;
    use crate::time::calendars::unitedkingdom::{self, UnitedKingdom};
    use crate::time::date::Month::{August, July, June};
    use crate::time::daycounters::actual360::Actual360;
    use crate::time::daycounters::thirty360::{Convention, Thirty360};

    /// The flat year-on-year rate the curve publishes everywhere.
    const CURVE_RATE: Real = 0.03;

    fn today() -> Date {
        Date::new(13, August, 2007)
    }

    fn maturity() -> Date {
        Date::new(13, August, 2012)
    }

    fn lag() -> Period {
        Period::new(2, TimeUnit::Months)
    }

    fn settings_today() -> Shared<Settings<Date>> {
        let settings = shared(Settings::<Date>::new());
        settings.set_evaluation_date(today());
        settings
    }

    fn an_index(settings: &Shared<Settings<Date>>) -> Shared<YoYInflationIndex> {
        shared(YoYInflationIndex::new(
            "YY_RPI".into(),
            Region::uk(),
            false,
            Frequency::Monthly,
            Period::new(1, TimeUnit::Months),
            Currency::gbp(),
            Shared::clone(settings),
        ))
    }

    /// Flat at [`CURVE_RATE`] over the whole span the coupons observe.
    fn a_flat_curve() -> Shared<dyn YoYInflationTermStructure> {
        shared(
            YoYInflationCurve::new(
                today(),
                vec![Date::new(1, July, 2007), Date::new(1, July, 2015)],
                vec![CURVE_RATE, CURVE_RATE],
                Frequency::Monthly,
                Actual360::new(),
                Linear,
                None,
            )
            .expect("a well-formed year-on-year curve"),
        ) as Shared<dyn YoYInflationTermStructure>
    }

    fn a_nominal_curve() -> Handle<dyn YieldTermStructure> {
        Handle::new(shared(FlatForward::with_rate(
            today(),
            0.05,
            Actual360::new(),
            Compounding::Continuous,
            Frequency::Annual,
        )) as Shared<dyn YieldTermStructure>)
    }

    fn a_helper(
        settings: &Shared<Settings<Date>>,
        interpolation: CpiInterpolationType,
    ) -> QlResult<Shared<YearOnYearInflationSwapHelper>> {
        a_helper_with(
            settings,
            interpolation,
            Pillar::LastRelevantDate,
            maturity(),
            lag(),
        )
    }

    fn a_helper_with(
        settings: &Shared<Settings<Date>>,
        interpolation: CpiInterpolationType,
        pillar: Pillar,
        maturity: Date,
        obs_lag: Period,
    ) -> QlResult<Shared<YearOnYearInflationSwapHelper>> {
        YearOnYearInflationSwapHelper::new(
            Handle::new(shared(SimpleQuote::new(Some(CURVE_RATE)))),
            obs_lag,
            maturity,
            UnitedKingdom::new(unitedkingdom::Market::Settlement),
            BusinessDayConvention::ModifiedFollowing,
            Thirty360::with_convention(Convention::BondBasis),
            &an_index(settings),
            interpolation,
            a_nominal_curve(),
            pillar,
            Shared::clone(settings),
        )
    }

    /// On a curve flat at `r` every forward year-on-year rate is `r`, so the
    /// swap that is fair against it is the one struck at `r`, and the helper's
    /// implied quote is `r` however the legs are discounted.
    #[test]
    fn the_implied_quote_is_the_flat_curves_rate() {
        let settings = settings_today();
        let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a well-formed helper");
        let curve = a_flat_curve();
        helper.set_term_structure(&curve);

        assert!((helper.implied_quote().unwrap() - CURVE_RATE).abs() < 1e-8);
        assert!((helper.quote_error().unwrap()).abs() < 1e-8);
    }

    /// The flat path collapses the helper's window onto the first day of the
    /// period the observation lands in: 13 August 2012 less two months is in
    /// June 2012 (`cpp:289-291`).
    #[test]
    fn the_flat_helper_pins_one_date() {
        let settings = settings_today();
        let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a well-formed helper");
        let period_start = Date::new(1, crate::time::date::Month::June, 2012);

        assert_eq!(helper.earliest_date(), period_start);
        assert_eq!(helper.latest_date(), period_start);
        assert_eq!(helper.pillar_date(), period_start);
    }

    /// The helper's contract is a unit-notional, zero-strike payer swap from the
    /// evaluation date to the maturity, both legs on one annual schedule
    /// (`cpp:314-334`), priced by an engine over the nominal curve it was
    /// handed rather than one it built.
    #[test]
    fn the_contract_is_a_unit_zero_strike_payer_swap() {
        let settings = settings_today();
        let helper = a_helper(&settings, CpiInterpolationType::Flat).expect("a well-formed helper");
        let swap = helper.swap();
        let swap = swap.as_ref().expect("the contract was built");

        assert_eq!(swap.swap_type(), SwapType::Payer);
        assert_eq!(swap.nominal(), 1.0);
        assert_eq!(swap.fixed_rate(), 0.0);
        assert_eq!(swap.spread(), 0.0);
        assert_eq!(swap.fixed_schedule().dates(), swap.yoy_schedule().dates());
        assert_eq!(swap.yoy_coupons().len(), 5);
        assert_eq!(
            helper
                .nominal_term_structure()
                .current_link()
                .unwrap()
                .reference_date()
                .unwrap(),
            today(),
            "the helper kept the curve it was handed"
        );
    }

    /// The interpolated window straddles the observed fixing period
    /// (`cpp:254-255`). The observation falls in June 2012, whose monthly period
    /// is 1 to 30 June, and the swap reads the fixings bracketing it: the June
    /// figure, dated 1 June, and the July one, dated the day after June's period
    /// ends - 1 July 2012. The flat helper collapses both onto 1 June.
    #[test]
    fn the_interpolated_window_reaches_a_month_past_the_flat_one() {
        let settings = settings_today();
        let interpolated = a_helper(&settings, CpiInterpolationType::Linear)
            .expect("a two-month lag leaves a month over the index's availability");
        let flat = a_helper(&settings, CpiInterpolationType::Flat).expect("a well-formed helper");

        assert_eq!(interpolated.earliest_date(), Date::new(1, June, 2012));
        assert_eq!(interpolated.latest_date(), Date::new(1, July, 2012));
        assert!(interpolated.latest_date() > flat.latest_date());
    }

    /// [`Pillar::MaturityDate`] pins the node at the window's far end
    /// (`cpp:258-260`), the July figure's date - not the near end the weighted
    /// choice picks for this same maturity below.
    #[test]
    fn the_maturity_date_pillar_is_the_windows_far_end() {
        let helper = a_helper_with(
            &settings_today(),
            CpiInterpolationType::Linear,
            Pillar::MaturityDate,
            maturity(),
            lag(),
        )
        .expect("a well-formed helper");

        assert_eq!(helper.pillar_date(), helper.latest_date());
        assert_eq!(helper.pillar_date(), Date::new(1, July, 2012));
    }

    /// [`Pillar::LastRelevantDate`] pins the node at whichever end carries the
    /// dominant interpolation weight (`cpp:261-278`), weighed on the maturity
    /// because this relative-date helper has no schedule start date
    /// (`cpp:263`). 13 August 2012 falls 12 days into a 31-day month, so `dt/dp`
    /// is 12/31 = 0.387 and the near end wins: the pillar is the window's start,
    /// a full month before its far end.
    #[test]
    fn the_weighted_pillar_takes_the_windows_start_early_in_the_month() {
        let helper = a_helper(&settings_today(), CpiInterpolationType::Linear)
            .expect("a well-formed helper");

        assert_eq!(helper.pillar_date(), Date::new(1, June, 2012));
    }

    /// The far side of the same threshold: 25 August 2012 falls 24 days into
    /// that 31-day month, `dt/dp` is 24/31 = 0.774, and the far end wins. C++
    /// leaves the pillar unset there rather than assigning one (`cpp:267-268`)
    /// and the base answers with the latest date
    /// (`bootstraphelper.hpp:193-197`); this port does the same. The window
    /// itself does not move - 25 June 2012 sits in the same June period - so the
    /// weight is the only thing the later maturity changes.
    #[test]
    fn the_weighted_pillar_takes_the_windows_far_end_late_in_the_month() {
        let helper = a_helper_with(
            &settings_today(),
            CpiInterpolationType::Linear,
            Pillar::LastRelevantDate,
            Date::new(25, August, 2012),
            lag(),
        )
        .expect("a well-formed helper");

        assert_eq!(helper.earliest_date(), Date::new(1, June, 2012));
        assert_eq!(helper.pillar_date(), Date::new(1, July, 2012));
    }

    /// The interpolated path needs a whole index period of observation lag on
    /// top of the index's availability lag (`cpp:296-302`), since it reads the
    /// month *after* the one the lag lands in. The index publishes a month in
    /// arrears, so a one-month lag leaves nothing for that second figure.
    ///
    /// The message names all three quantities, which is what tells this apart
    /// from any failure the contract itself would raise - the flat path builds
    /// on the same one-month lag.
    #[test]
    fn an_interpolated_lag_short_of_an_index_period_is_rejected() {
        let settings = settings_today();
        let short_lag = Period::new(1, TimeUnit::Months);
        let error = a_helper_with(
            &settings,
            CpiInterpolationType::Linear,
            Pillar::LastRelevantDate,
            maturity(),
            short_lag,
        )
        .err()
        .expect("one month of lag cannot cover the interpolation");

        assert!(
            error
                .message()
                .contains("need (obsLag-index period) >= availLag"),
            "err was: {error}"
        );
        assert!(
            a_helper_with(
                &settings,
                CpiInterpolationType::Flat,
                Pillar::LastRelevantDate,
                maturity(),
                short_lag,
            )
            .is_ok(),
            "the flat path has no such requirement"
        );
    }
}