curvekit 1.0.1

Offline-first bundled-parquet risk-free rate library — Treasury yield curves + SOFR
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
//! Stateful `Curvekit` client — flat async endpoint methods.
//!
//! Fetches parquet files from GitHub raw (or a configurable origin) with an
//! XDG-compliant local cache + ETag revalidation. Falls back to stale cache on
//! network errors so existing workflows survive transient outages.
//!
//! # Example
//!
//! ```no_run
//! use curvekit::{Curvekit, Tenor};
//!
//! #[tokio::main]
//! async fn main() -> curvekit::Result<()> {
//!     let client = Curvekit::new();   // infallible
//!
//!     // Any date form works — no chrono import needed
//!     let curve = client.treasury_curve("2020-03-20").await?;
//!     println!("10Y: {:.4}%", curve.get(Tenor::Y10).unwrap_or(0.0) * 100.0);
//!
//!     let sofr = client.sofr_latest().await?;
//!     println!("SOFR {}: {:.4}%", sofr.date, sofr.rate * 100.0);
//!     Ok(())
//! }
//! ```

use chrono::Datelike;
use futures::future::try_join_all;
use std::path::PathBuf;

use crate::curve::{SofrDay, YieldCurve};
use crate::date::{Date, IntoDate};
use crate::daycount::DayCount;
use crate::error::{Error, Result};
use crate::fetcher::{default_cache_dir, resolved_base_url, CachedFetcher};
use crate::sources::effr::EffrDay;
use crate::sources::obfr::ObfrDay;
use crate::sources::parquet_io::{
    read_effr_year, read_obfr_year, read_sofr_year, read_treasury_year,
};
use crate::tenor::Tenor;

/// Stateful curvekit client.
///
/// Wraps an ETag-aware cached fetcher and exposes flat endpoint methods.
/// Create once and reuse across calls; the internal reqwest client is kept
/// alive for connection pooling.
///
/// # Infallible construction
///
/// ```no_run
/// use curvekit::Curvekit;
///
/// let client = Curvekit::new();   // never fails
/// ```
///
/// # Builder pattern
///
/// ```no_run
/// use curvekit::Curvekit;
/// use std::path::PathBuf;
///
/// let client = Curvekit::new()
///     .with_base_url("https://my-mirror.example.com/curvekit")
///     .with_cache_dir(PathBuf::from("/tmp/curvekit-test"));
/// ```
pub struct Curvekit {
    fetcher: CachedFetcher,
}

impl Curvekit {
    /// Create a client with the default GitHub raw backend and XDG cache.
    ///
    /// Reads `CURVEKIT_BASE_URL` and `CURVEKIT_CACHE_DIR` from the environment
    /// if set, otherwise uses the GitHub raw origin and `~/.cache/curvekit/`.
    ///
    /// **This function never fails.** If the underlying HTTP client cannot be
    /// built (essentially only on exotic platforms with broken TLS), the error
    /// is deferred to the first fetch call. Use [`try_new`][Self::try_new] for
    /// early detection.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use curvekit::Curvekit;
    /// let client = Curvekit::new();   // no ? needed
    /// ```
    pub fn new() -> Self {
        // If the client cannot be built we store a fallback that will fail on
        // the first actual fetch. In practice reqwest::Client::builder().build()
        // only fails on platforms that lack TLS support — essentially never.
        let http = reqwest::Client::builder()
            .user_agent("curvekit/1.0 (+https://github.com/userFRM/curvekit)")
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .unwrap_or_else(|_| reqwest::Client::new());
        Self {
            fetcher: CachedFetcher::new(http, resolved_base_url(), default_cache_dir()),
        }
    }

    /// Create a client with early failure detection.
    ///
    /// Like [`new`][Self::new] but returns an error immediately if the HTTP
    /// client cannot be constructed. Prefer [`new`][Self::new] for typical use;
    /// use this in contexts where surfacing TLS init failures immediately matters.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] if the underlying reqwest client cannot be constructed
    /// (TLS init failure — essentially never in practice).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use curvekit::Curvekit;
    /// let client = Curvekit::try_new()?;
    /// # Ok::<(), curvekit::Error>(())
    /// ```
    pub fn try_new() -> Result<Self> {
        let http = reqwest::Client::builder()
            .user_agent("curvekit/1.0 (+https://github.com/userFRM/curvekit)")
            .timeout(std::time::Duration::from_secs(30))
            .build()?;
        Ok(Self {
            fetcher: CachedFetcher::new(http, resolved_base_url(), default_cache_dir()),
        })
    }

    /// Override the origin URL.
    ///
    /// Default: `https://raw.githubusercontent.com/userFRM/curvekit/main/data`.
    /// Useful for pointing at a fork or a self-hosted mirror.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use curvekit::Curvekit;
    /// let client = Curvekit::new().with_base_url("https://my-mirror.example.com/curvekit");
    /// ```
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.fetcher.set_base_url(url.into());
        self
    }

    /// Override the on-disk cache directory.
    ///
    /// Default: `~/.cache/curvekit/` (XDG via the `directories` crate).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use curvekit::Curvekit;
    /// use std::path::PathBuf;
    /// let client = Curvekit::new().with_cache_dir(PathBuf::from("/tmp/curvekit-test"));
    /// ```
    pub fn with_cache_dir(mut self, dir: PathBuf) -> Self {
        self.fetcher.set_cache_dir(dir);
        self
    }

    /// Override the CDN mirror URL used when the primary fetch fails.
    ///
    /// Default: jsDelivr CDN mirror of this repo. The mirror is activated
    /// only when the primary GitHub raw fetch exhausts its retry budget.
    ///
    /// - `Some(url)` — use a custom mirror (self-hosted, R2, Fastly, …).
    /// - `None` — disable mirror fallback entirely. A primary failure
    ///   returns the error directly. Useful in tests where you want to
    ///   observe primary-path behavior without the fallback masking it.
    ///
    /// Equivalent to `CURVEKIT_MIRROR_URL` env var. Builder form takes
    /// precedence — the env var is read only if this method is not called.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use curvekit::Curvekit;
    ///
    /// // Disable mirror entirely (e.g. in tests)
    /// let client = Curvekit::new().with_mirror_url(None);
    ///
    /// // Use a custom self-hosted mirror
    /// let client = Curvekit::new().with_mirror_url(Some("https://mirror.example.com/curvekit".into()));
    /// ```
    pub fn with_mirror_url(mut self, url: Option<String>) -> Self {
        self.fetcher.set_mirror_url(url);
        self
    }

    // ── Treasury endpoints ────────────────────────────────────────────────────

    /// Fetch the full US Treasury Par Yield Curve for a single date.
    ///
    /// Returns the curve exactly as published by Treasury — **par yields**
    /// (Bond Equivalent Yield, converted to continuously compounded). For
    /// discount-factor arithmetic you usually want
    /// [`treasury_zero_curve`][Self::treasury_zero_curve]
    /// instead, which bootstraps zero rates from the par curve.
    ///
    /// Accepts any date form — no `chrono` import required:
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    ///
    /// // ISO string
    /// let curve = client.treasury_curve("2020-03-20").await?;
    ///
    /// // Compact integer YYYYMMDD
    /// let curve = client.treasury_curve(20200320u32).await?;
    ///
    /// // YMD tuple (year as i32, month/day as u32)
    /// let curve = client.treasury_curve((2020i32, 3u32, 20u32)).await?;
    ///
    /// // Existing NaiveDate (still works — infallible From conversion)
    /// use chrono::NaiveDate;
    /// let nd = NaiveDate::from_ymd_opt(2020, 3, 20).expect("valid date");
    /// let curve = client.treasury_curve(nd).await?;
    /// # Ok(()) }
    /// ```
    ///
    /// Resolves the date to a year file (`treasury-{year}.parquet`), fetches
    /// and caches it (ETag revalidation), then filters to the requested date.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached file for the year.
    /// - `date` is not present in the year file (weekend, holiday, or outside
    ///   coverage 2002–present).
    /// - Invalid date string or YYYYMMDD value.
    ///
    /// # See also
    ///
    /// - [`treasury_par_curve`][Self::treasury_par_curve] — explicit alias (preferred for clarity)
    /// - [`treasury_zero_curve`][Self::treasury_zero_curve] — bootstrapped zero/spot rates
    #[deprecated(
        since = "1.0.0",
        note = "use `treasury_par_curve` or `treasury_zero_curve` for explicit yield type"
    )]
    pub async fn treasury_curve(&self, date: impl IntoDate) -> Result<YieldCurve> {
        let date = date.into_date()?;
        let year = date.inner().year();
        let curves = self.treasury_year(year).await?;
        curves
            .into_iter()
            .find(|c| c.date == date.inner())
            .ok_or_else(|| Error::DateNotFound(format!("no treasury curve for {date}")))
    }

    /// Fetch the US Treasury **par yield** curve for a single date.
    ///
    /// Returns the curve exactly as published — par yields (BEY, converted to
    /// continuously compounded). `yield_type` is [`YieldType::Par`][crate::YieldType::Par].
    ///
    /// Use this when you need the raw published data or want to perform your
    /// own curve math. For discount-factor work, prefer
    /// [`treasury_zero_curve`][Self::treasury_zero_curve].
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let par = client.treasury_par_curve("2020-03-20").await?;
    /// println!("Par type: {:?}", par.yield_type);
    /// # Ok(()) }
    /// ```
    pub async fn treasury_par_curve(&self, date: impl IntoDate) -> Result<YieldCurve> {
        let date = date.into_date()?;
        let year = date.inner().year();
        let curves = self.treasury_year(year).await?;
        curves
            .into_iter()
            .find(|c| c.date == date.inner())
            .ok_or_else(|| Error::DateNotFound(format!("no treasury curve for {date}")))
    }

    /// Fetch the US Treasury **zero-coupon (spot)** curve for a single date.
    ///
    /// Fetches the par curve and bootstraps zero rates using the iterative
    /// semi-annual coupon bootstrap. See [`YieldCurve::bootstrap_zero`] for
    /// the full algorithm description.
    ///
    /// `yield_type` is [`YieldType::Zero`][crate::YieldType::Zero].
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let zero = client.treasury_zero_curve("2020-03-20").await?;
    /// let df_10y = (-zero.get(3650u32).unwrap() * 10.0).exp();
    /// println!("10Y discount factor: {df_10y:.6}");
    /// # Ok(()) }
    /// ```
    pub async fn treasury_zero_curve(&self, date: impl IntoDate) -> Result<YieldCurve> {
        let par = self.treasury_par_curve(date).await?;
        par.bootstrap_zero()
    }

    /// Interpolated continuously-compounded par rate at `tenor` for `date`,
    /// using [`DayCount::Act365Fixed`].
    ///
    /// For a rate with an explicit day-count convention see
    /// [`treasury_rate_with_convention`][Self::treasury_rate_with_convention].
    ///
    /// The returned rate is a **par yield** (continuously compounded). For
    /// zero rates use [`treasury_zero_curve`][Self::treasury_zero_curve].
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::{Curvekit, Tenor};
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let r = client.treasury_rate("2026-04-14", Tenor::Y10).await?;
    /// # Ok(()) }
    /// ```
    pub async fn treasury_rate(&self, date: impl IntoDate, tenor: impl Into<Tenor>) -> Result<f64> {
        let tenor = tenor.into();
        let curve = self.treasury_par_curve(date).await?;
        curve
            .get(tenor)
            .ok_or_else(|| Error::Interpolation(format!("no treasury data at {}", tenor)))
    }

    /// Interpolated rate at `tenor` for `date` scaled to the given day-count
    /// convention's year fraction.
    ///
    /// The raw rate in the curve is continuously compounded on an Act/365 basis
    /// (matching the Treasury's days-to-maturity indexing). This method
    /// multiplies by the ratio `Act365Fixed / convention` so that the returned
    /// rate is expressed in the requested convention.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::{Curvekit, Tenor, DayCount};
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let r_act360 = client
    ///     .treasury_rate_with_convention("2026-04-14", Tenor::M3, DayCount::Act360)
    ///     .await?;
    /// # Ok(()) }
    /// ```
    pub async fn treasury_rate_with_convention(
        &self,
        date: impl IntoDate,
        tenor: impl Into<Tenor>,
        convention: DayCount,
    ) -> Result<f64> {
        let date_val = date.into_date()?;
        let tenor = tenor.into();
        let curve = self.treasury_par_curve(date_val).await?;
        let r = curve
            .get(tenor)
            .ok_or_else(|| Error::Interpolation(format!("no treasury data at {}", tenor)))?;
        // r is Act365Fixed continuously compounded. Convert to the target convention
        // by scaling the year fraction: r_conv = r * (T_act365 / T_conv).
        let maturity = date_val
            .inner()
            .checked_add_signed(chrono::Duration::days(tenor.as_days() as i64))
            .ok_or_else(|| Error::Other("tenor overflow computing maturity date".into()))?;
        let t_act365 = DayCount::Act365Fixed.year_fraction(date_val.inner(), maturity);
        let t_conv = convention.year_fraction(date_val.inner(), maturity);
        if t_conv == 0.0 {
            return Ok(r);
        }
        Ok(r * t_act365 / t_conv)
    }

    /// Fetch all Treasury yield curves in `[start, end]` (inclusive).
    ///
    /// Determines the year span, fetches each year file in parallel, then
    /// filters to the requested date range. Non-trading days are absent.
    ///
    /// Both `start` and `end` accept any date form (string, u32, tuple, NaiveDate).
    ///
    /// # Errors
    ///
    /// - `start > end`.
    /// - Network failure for any year in the span with no cached file.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let curves = client.treasury_range("2020-01-01", "2020-12-31").await?;
    /// println!("Trading days in 2020: {}", curves.len());
    /// # Ok(()) }
    /// ```
    pub async fn treasury_range(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<YieldCurve>> {
        let start = start.into_date()?;
        let end = end.into_date()?;
        if start > end {
            return Err(Error::Other(format!(
                "treasury_range: start {start} > end {end}"
            )));
        }
        let start_nd = start.inner();
        let end_nd = end.inner();
        let years: Vec<i32> = (start_nd.year()..=end_nd.year()).collect();
        let fetches = years
            .iter()
            .map(|&y| self.treasury_year(y))
            .collect::<Vec<_>>();
        let all_years = try_join_all(fetches).await?;
        let mut out: Vec<YieldCurve> = all_years
            .into_iter()
            .flatten()
            .filter(|c| c.date >= start_nd && c.date <= end_nd)
            .collect();
        out.sort_by_key(|c| c.date);
        Ok(out)
    }

    /// Latest available Treasury yield curve.
    ///
    /// Fetches the current calendar year; falls back to the previous year if
    /// no data is present yet (e.g. early January before the first trading day).
    ///
    /// # Errors
    ///
    /// - Network failure with no cached files for both the current and previous year.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let curve = client.treasury_latest().await?;
    /// println!("Latest: {}", curve.date);
    /// # Ok(()) }
    /// ```
    pub async fn treasury_latest(&self) -> Result<YieldCurve> {
        use chrono::Utc;
        let current_year = Utc::now().year();
        for year in [current_year, current_year - 1] {
            if let Ok(curves) = self.treasury_year(year).await {
                if let Some(latest) = curves.into_iter().max_by_key(|c| c.date) {
                    return Ok(latest);
                }
            }
        }
        Err(Error::DateNotFound("no treasury data available".into()))
    }

    /// Earliest date for which Treasury data is available remotely.
    ///
    /// Fetches `treasury-2000.parquet` and returns the minimum date found.
    /// Coverage in practice starts 2002-01-02.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached file for year 2000.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let d = client.treasury_earliest_date().await?;
    /// println!("Earliest treasury: {d}");
    /// # Ok(()) }
    /// ```
    pub async fn treasury_earliest_date(&self) -> Result<chrono::NaiveDate> {
        let curves = self.treasury_year(2000).await?;
        curves
            .into_iter()
            .map(|c| c.date)
            .min()
            .ok_or_else(|| Error::DateNotFound("no data in treasury-2000.parquet".into()))
    }

    // ── SOFR endpoints ────────────────────────────────────────────────────────

    /// Fetch the SOFR overnight rate (continuously compounded) for a single date.
    ///
    /// Accepts any date form — no `chrono` import required.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached file for the year.
    /// - `date` not found in the year file (weekend, holiday, or before
    ///   SOFR inception 2018-04-02).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let r = client.sofr("2026-04-14").await?;
    /// println!("SOFR: {r:.6}");
    /// # Ok(()) }
    /// ```
    pub async fn sofr(&self, date: impl IntoDate) -> Result<f64> {
        let date = date.into_date()?;
        let year = date.inner().year();
        let rates = self.sofr_year(year).await?;
        rates
            .into_iter()
            .find(|r| r.date == date.inner())
            .map(|r| r.rate)
            .ok_or_else(|| Error::DateNotFound(format!("no SOFR for {date}")))
    }

    /// Fetch all SOFR observations in `[start, end]` (inclusive).
    ///
    /// Fetches each calendar year in the span in parallel. Non-business days
    /// are absent from the result.
    ///
    /// # Errors
    ///
    /// - `start > end`.
    /// - Network failure for any year in the span with no cached file.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let rates = client.sofr_range("2023-01-01", "2023-12-31").await?;
    /// println!("SOFR observations in 2023: {}", rates.len());
    /// # Ok(()) }
    /// ```
    pub async fn sofr_range(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<SofrDay>> {
        let start = start.into_date()?;
        let end = end.into_date()?;
        if start > end {
            return Err(Error::Other(format!(
                "sofr_range: start {start} > end {end}"
            )));
        }
        let start_nd = start.inner();
        let end_nd = end.inner();
        let years: Vec<i32> = (start_nd.year()..=end_nd.year()).collect();
        let fetches = years.iter().map(|&y| self.sofr_year(y)).collect::<Vec<_>>();
        let all_years = try_join_all(fetches).await?;
        let mut out: Vec<SofrDay> = all_years
            .into_iter()
            .flatten()
            .filter(|r| r.date >= start_nd && r.date <= end_nd)
            .collect();
        out.sort_by_key(|r| r.date);
        Ok(out)
    }

    /// Latest available SOFR observation.
    ///
    /// Fetches the current calendar year; falls back to the previous year if
    /// no data is present yet.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached files for both the current and previous year.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let sofr = client.sofr_latest().await?;
    /// println!("SOFR {}: {:.4}%", sofr.date, sofr.rate * 100.0);
    /// # Ok(()) }
    /// ```
    pub async fn sofr_latest(&self) -> Result<SofrDay> {
        use chrono::Utc;
        let current_year = Utc::now().year();
        for year in [current_year, current_year - 1] {
            if let Ok(rates) = self.sofr_year(year).await {
                if let Some(latest) = rates.into_iter().max_by_key(|r| r.date) {
                    return Ok(latest);
                }
            }
        }
        Err(Error::DateNotFound("no SOFR data available".into()))
    }

    /// Earliest date for which SOFR data is available remotely.
    ///
    /// SOFR began 2018-04-02. Fetches `sofr-2018.parquet` and returns the
    /// minimum date found.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached file for year 2018.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let d = client.sofr_earliest_date().await?;
    /// println!("SOFR inception: {d}");
    /// # Ok(()) }
    /// ```
    pub async fn sofr_earliest_date(&self) -> Result<chrono::NaiveDate> {
        let rates = self.sofr_year(2018).await?;
        rates
            .into_iter()
            .map(|r| r.date)
            .min()
            .ok_or_else(|| Error::DateNotFound("no data in sofr-2018.parquet".into()))
    }

    // ── EFFR endpoints ────────────────────────────────────────────────────────

    /// Fetch the Effective Federal Funds Rate for a single date.
    ///
    /// Returns the continuously-compounded overnight rate.
    ///
    /// # Data source
    ///
    /// Parquet files `data/effr-{year}.parquet` served from the curvekit data
    /// repository. Populated by `curvekit-cli backfill --source effr`.
    ///
    /// # Errors
    ///
    /// - Network failure with no cached file for the year.
    /// - `date` not found (weekend/holiday, or before EFFR coverage).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let r = client.effr("2026-04-14").await?;
    /// println!("EFFR: {r:.6}");
    /// # Ok(()) }
    /// ```
    pub async fn effr(&self, date: impl IntoDate) -> Result<f64> {
        let date = date.into_date()?;
        let year = date.inner().year();
        let rates = self.effr_year(year).await?;
        rates
            .into_iter()
            .find(|r| r.date == date.inner())
            .map(|r| r.rate)
            .ok_or_else(|| Error::DateNotFound(format!("no EFFR for {date}")))
    }

    /// Fetch all EFFR observations in `[start, end]` (inclusive).
    pub async fn effr_range(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<EffrDay>> {
        let start = start.into_date()?;
        let end = end.into_date()?;
        if start > end {
            return Err(Error::Other(format!(
                "effr_range: start {start} > end {end}"
            )));
        }
        let start_nd = start.inner();
        let end_nd = end.inner();
        let years: Vec<i32> = (start_nd.year()..=end_nd.year()).collect();
        let fetches = years.iter().map(|&y| self.effr_year(y)).collect::<Vec<_>>();
        let all_years = futures::future::try_join_all(fetches).await?;
        let mut out: Vec<EffrDay> = all_years
            .into_iter()
            .flatten()
            .filter(|r| r.date >= start_nd && r.date <= end_nd)
            .collect();
        out.sort_by_key(|r| r.date);
        Ok(out)
    }

    /// Latest available EFFR observation.
    pub async fn effr_latest(&self) -> Result<EffrDay> {
        use chrono::Utc;
        let current_year = Utc::now().year();
        for year in [current_year, current_year - 1] {
            if let Ok(rates) = self.effr_year(year).await {
                if let Some(latest) = rates.into_iter().max_by_key(|r| r.date) {
                    return Ok(latest);
                }
            }
        }
        Err(Error::DateNotFound("no EFFR data available".into()))
    }

    /// Earliest date for which EFFR data is available (fetches oldest year file).
    pub async fn effr_earliest_date(&self) -> Result<chrono::NaiveDate> {
        // EFFR history goes back to 1954; curvekit typically backfills from 2000.
        let rates = self.effr_year(2000).await?;
        rates
            .into_iter()
            .map(|r| r.date)
            .min()
            .ok_or_else(|| Error::DateNotFound("no data in effr-2000.parquet".into()))
    }

    // ── OBFR endpoints ────────────────────────────────────────────────────────

    /// Fetch the Overnight Bank Funding Rate for a single date.
    ///
    /// Coverage starts 2016-03-01. Returns the continuously-compounded rate.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use curvekit::Curvekit;
    /// # async fn run() -> curvekit::Result<()> {
    /// let client = Curvekit::new();
    /// let r = client.obfr("2026-04-14").await?;
    /// println!("OBFR: {r:.6}");
    /// # Ok(()) }
    /// ```
    pub async fn obfr(&self, date: impl IntoDate) -> Result<f64> {
        let date = date.into_date()?;
        let year = date.inner().year();
        let rates = self.obfr_year(year).await?;
        rates
            .into_iter()
            .find(|r| r.date == date.inner())
            .map(|r| r.rate)
            .ok_or_else(|| Error::DateNotFound(format!("no OBFR for {date}")))
    }

    /// Fetch all OBFR observations in `[start, end]` (inclusive).
    pub async fn obfr_range(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<ObfrDay>> {
        let start = start.into_date()?;
        let end = end.into_date()?;
        if start > end {
            return Err(Error::Other(format!(
                "obfr_range: start {start} > end {end}"
            )));
        }
        let start_nd = start.inner();
        let end_nd = end.inner();
        let years: Vec<i32> = (start_nd.year()..=end_nd.year()).collect();
        let fetches = years.iter().map(|&y| self.obfr_year(y)).collect::<Vec<_>>();
        let all_years = futures::future::try_join_all(fetches).await?;
        let mut out: Vec<ObfrDay> = all_years
            .into_iter()
            .flatten()
            .filter(|r| r.date >= start_nd && r.date <= end_nd)
            .collect();
        out.sort_by_key(|r| r.date);
        Ok(out)
    }

    /// Latest available OBFR observation.
    pub async fn obfr_latest(&self) -> Result<ObfrDay> {
        use chrono::Utc;
        let current_year = Utc::now().year();
        for year in [current_year, current_year - 1] {
            if let Ok(rates) = self.obfr_year(year).await {
                if let Some(latest) = rates.into_iter().max_by_key(|r| r.date) {
                    return Ok(latest);
                }
            }
        }
        Err(Error::DateNotFound("no OBFR data available".into()))
    }

    /// Earliest date for which OBFR data is available (2016-03-01).
    pub async fn obfr_earliest_date(&self) -> Result<chrono::NaiveDate> {
        let rates = self.obfr_year(2016).await?;
        rates
            .into_iter()
            .map(|r| r.date)
            .min()
            .ok_or_else(|| Error::DateNotFound("no data in obfr-2016.parquet".into()))
    }

    // ── Blocking wrappers ─────────────────────────────────────────────────────

    /// Blocking variant of [`treasury_curve`][Self::treasury_curve].
    ///
    /// Works from both sync and async contexts:
    /// - Inside a tokio runtime: uses `block_in_place` + `Handle::block_on`.
    /// - Outside a runtime: spins up a single-threaded runtime for the call.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use curvekit::{Curvekit, Tenor};
    ///
    /// // From synchronous code — no async needed
    /// let client = Curvekit::new();
    /// let curve = client.treasury_curve_blocking("2020-03-20")?;
    /// println!("10Y: {:.4}%", curve.get(Tenor::Y10).unwrap_or(0.0) * 100.0);
    /// # Ok::<(), curvekit::Error>(())
    /// ```
    pub fn treasury_curve_blocking(&self, date: impl IntoDate) -> Result<YieldCurve> {
        let date: Date = date.into_date()?;
        block(self.treasury_par_curve(date))
    }

    /// Blocking variant of [`treasury_range`][Self::treasury_range].
    pub fn treasury_range_blocking(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<YieldCurve>> {
        let start: Date = start.into_date()?;
        let end: Date = end.into_date()?;
        block(self.treasury_range(start, end))
    }

    /// Blocking variant of [`treasury_rate`][Self::treasury_rate].
    pub fn treasury_rate_blocking(
        &self,
        date: impl IntoDate,
        tenor: impl Into<Tenor>,
    ) -> Result<f64> {
        let date: Date = date.into_date()?;
        let tenor: Tenor = tenor.into();
        block(self.treasury_rate(date, tenor))
    }

    /// Blocking variant of [`treasury_latest`][Self::treasury_latest].
    pub fn treasury_latest_blocking(&self) -> Result<YieldCurve> {
        block(self.treasury_latest())
    }

    /// Blocking variant of [`sofr`][Self::sofr].
    pub fn sofr_blocking(&self, date: impl IntoDate) -> Result<f64> {
        let date: Date = date.into_date()?;
        block(self.sofr(date))
    }

    /// Blocking variant of [`sofr_range`][Self::sofr_range].
    pub fn sofr_range_blocking(
        &self,
        start: impl IntoDate,
        end: impl IntoDate,
    ) -> Result<Vec<SofrDay>> {
        let start: Date = start.into_date()?;
        let end: Date = end.into_date()?;
        block(self.sofr_range(start, end))
    }

    /// Blocking variant of [`sofr_latest`][Self::sofr_latest].
    pub fn sofr_latest_blocking(&self) -> Result<SofrDay> {
        block(self.sofr_latest())
    }

    // ── Internal helpers ──────────────────────────────────────────────────────

    /// Fetch and decode one full treasury year file.
    async fn treasury_year(&self, year: i32) -> Result<Vec<YieldCurve>> {
        let key = format!("treasury-{year}");
        let bytes = self.fetcher.fetch(&key).await?;
        let tmp = tempfile_for_bytes(&bytes, &format!("{key}.parquet"))?;
        let curves = read_treasury_year(tmp.path())?;
        Ok(curves)
    }

    /// Fetch and decode one full SOFR year file.
    async fn sofr_year(&self, year: i32) -> Result<Vec<SofrDay>> {
        let key = format!("sofr-{year}");
        let bytes = self.fetcher.fetch(&key).await?;
        let tmp = tempfile_for_bytes(&bytes, &format!("{key}.parquet"))?;
        let rates = read_sofr_year(tmp.path())?;
        Ok(rates)
    }

    /// Fetch and decode one full EFFR year file.
    async fn effr_year(&self, year: i32) -> Result<Vec<EffrDay>> {
        let key = format!("effr-{year}");
        let bytes = self.fetcher.fetch(&key).await?;
        let tmp = tempfile_for_bytes(&bytes, &format!("{key}.parquet"))?;
        let rates = read_effr_year(tmp.path())?;
        Ok(rates)
    }

    /// Fetch and decode one full OBFR year file.
    async fn obfr_year(&self, year: i32) -> Result<Vec<ObfrDay>> {
        let key = format!("obfr-{year}");
        let bytes = self.fetcher.fetch(&key).await?;
        let tmp = tempfile_for_bytes(&bytes, &format!("{key}.parquet"))?;
        let rates = read_obfr_year(tmp.path())?;
        Ok(rates)
    }
}

impl Default for Curvekit {
    fn default() -> Self {
        Self::new()
    }
}

// ── Date → TryInto wiring ─────────────────────────────────────────────────────
//
// All `TryFrom<T, Error = DateError> for Date` impls live in `date.rs`.
// The client methods use `impl IntoDate`, so any type
// that has `TryFrom<T, Error = DateError> for Date` works:
//   - `&str`, `String`  — parsed as ISO / slashed / compact
//   - `u32`             — interpreted as YYYYMMDD
//   - `(i32,u32,u32)`, `(u32,u32,u32)` — YMD components
//   - `NaiveDate`       — direct wrap
//   - `Date`            — identity
//
// `DateError` converts into `curvekit::Error` via `From<DateError> for Error`,
// so callers can use `?` freely in `-> Result<_, curvekit::Error>` functions.

// ── Blocking helper ───────────────────────────────────────────────────────────

/// Drive a future to completion from any context (sync or async).
///
/// - Inside a tokio multi-thread runtime: `block_in_place` + `Handle::block_on`
///   (avoids the "cannot call block_on inside async" panic).
/// - Outside any runtime: spin up a minimal current-thread runtime.
fn block<F: std::future::Future<Output = Result<T>>, T>(fut: F) -> Result<T> {
    match tokio::runtime::Handle::try_current() {
        Ok(handle) => tokio::task::block_in_place(|| handle.block_on(fut)),
        Err(_) => {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(Error::Io)?;
            rt.block_on(fut)
        }
    }
}

/// Write bytes to a named temp file and return the [`tempfile::NamedTempFile`].
fn tempfile_for_bytes(bytes: &bytes::Bytes, _hint: &str) -> Result<tempfile::NamedTempFile> {
    use std::io::Write;
    let mut tmp = tempfile::NamedTempFile::new()?;
    tmp.write_all(bytes)?;
    tmp.flush()?;
    Ok(tmp)
}