eeyf 0.1.0

Eric Evans' Yahoo Finance API - A rate-limited, reliable Rust adapter for Yahoo Finance API
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
use std::{collections::HashMap, fmt};

use serde::{
    Deserialize, Serialize,
    de::{self, Deserializer, MapAccess, SeqAccess, Visitor},
};
use time::OffsetDateTime;

use super::YahooError;

#[cfg(not(feature = "decimal"))]
pub mod decimal {
    pub type Decimal = f64;
    pub const ZERO: Decimal = 0.0;

    // These are provided for API compatibility with rust_decimal
    // They will be used when the disabled modules are refactored
    #[allow(dead_code)]
    pub const ONE: Decimal = 1.0;

    #[inline]
    #[allow(dead_code)]
    pub fn from_usize(n: usize) -> Decimal {
        n as Decimal
    }

    #[inline]
    #[allow(dead_code)]
    pub fn from_f64(n: f64) -> Option<Decimal> {
        Some(n)
    }
}

#[cfg(feature = "decimal")]
pub mod decimal {
    pub use rust_decimal::Decimal;
    use rust_decimal::prelude::FromPrimitive;
    pub const ZERO: Decimal = Decimal::ZERO;
    pub const ONE: Decimal = Decimal::ONE;

    #[inline]
    pub fn from_usize(n: usize) -> Decimal {
        Decimal::from(n)
    }

    #[inline]
    pub fn from_f64(n: f64) -> Option<Decimal> {
        Decimal::from_f64(n)
    }
}

pub use decimal::*;

#[derive(Deserialize, Debug)]
pub struct YResponse {
    pub chart: YChart,
}

impl YResponse {
    pub(crate) fn map_error_msg(self) -> Result<YResponse, YahooError> {
        if self.chart.result.is_none() {
            if let Some(y_error) = self.chart.error {
                return Err(YahooError::ApiError(y_error));
            }
        }
        Ok(self)
    }

    fn check_historical_consistency(&self) -> Result<&Vec<YQuoteBlock>, YahooError> {
        let Some(result) = &self.chart.result else {
            return Err(YahooError::NoResult);
        };

        for stock in result {
            let n = stock.timestamp.as_ref().map_or(0, |v| v.len());

            if n == 0 {
                return Err(YahooError::NoQuotes);
            }

            let quote = &stock.indicators.quote[0];

            if quote.open.is_none()
                || quote.high.is_none()
                || quote.low.is_none()
                || quote.volume.is_none()
                || quote.close.is_none()
            {
                return Err(YahooError::DataInconsistency);
            }

            let open_len = quote.open.as_ref().map_or(0, |v| v.len());
            let high_len = quote.high.as_ref().map_or(0, |v| v.len());
            let low_len = quote.low.as_ref().map_or(0, |v| v.len());
            let volume_len = quote.volume.as_ref().map_or(0, |v| v.len());
            let close_len = quote.close.as_ref().map_or(0, |v| v.len());

            if open_len != n || high_len != n || low_len != n || volume_len != n || close_len != n {
                return Err(YahooError::DataInconsistency);
            }
        }
        Ok(result)
    }

    pub fn from_json(json: serde_json::Value) -> Result<YResponse, YahooError> {
        Ok(serde_json::from_value(json)?)
    }

    /// Return the latest valid quote
    pub fn last_quote(&self) -> Result<Quote, YahooError> {
        let stock = &self.check_historical_consistency()?[0];

        let n = stock.timestamp.as_ref().map_or(0, |v| v.len());

        for i in (0..n).rev() {
            let quote = stock.indicators.get_ith_quote(stock.timestamp.as_ref().unwrap()[i], i);
            if quote.is_ok() {
                return quote;
            }
        }
        Err(YahooError::NoQuotes)
    }

    pub fn quotes(&self) -> Result<Vec<Quote>, YahooError> {
        let stock = &self.check_historical_consistency()?[0];

        let mut quotes = Vec::new();
        let n = stock.timestamp.as_ref().map_or(0, |v| v.len());
        for i in 0..n {
            let timestamp = stock.timestamp.as_ref().unwrap()[i];
            let quote = stock.indicators.get_ith_quote(timestamp, i);
            if let Ok(q) = quote {
                quotes.push(q);
            }
        }
        Ok(quotes)
    }

    pub fn metadata(&self) -> Result<YMetaData, YahooError> {
        let Some(result) = &self.chart.result else {
            return Err(YahooError::NoResult);
        };
        let stock = &result[0];
        Ok(stock.meta.to_owned())
    }

    /// This method retrieves information about the splits that might have
    /// occured during the considered time period
    pub fn splits(&self) -> Result<Vec<Split>, YahooError> {
        let Some(result) = &self.chart.result else {
            return Err(YahooError::NoResult);
        };
        let stock = &result[0];

        if let Some(events) = &stock.events {
            if let Some(splits) = &events.splits {
                let mut data = splits.values().cloned().collect::<Vec<Split>>();
                data.sort_unstable_by_key(|d| d.date);
                return Ok(data);
            }
        }
        Ok(vec![])
    }

    /// This method retrieves information about the dividends that have
    /// been recorded during the considered time period.
    ///
    /// Note: Date is the ex-dividend date)
    pub fn dividends(&self) -> Result<Vec<Dividend>, YahooError> {
        let Some(result) = &self.chart.result else {
            return Err(YahooError::NoResult);
        };
        let stock = &result[0];

        if let Some(events) = &stock.events {
            if let Some(dividends) = &events.dividends {
                let mut data = dividends.values().cloned().collect::<Vec<Dividend>>();
                data.sort_unstable_by_key(|d| d.date);
                return Ok(data);
            }
        }
        Ok(vec![])
    }

    /// This method retrieves information about the capital gains that might
    /// have occured during the considered time period (available only for
    /// Mutual Funds)
    pub fn capital_gains(&self) -> Result<Vec<CapitalGain>, YahooError> {
        let Some(result) = &self.chart.result else {
            return Err(YahooError::NoResult);
        };
        let stock = &result[0];

        if let Some(events) = &stock.events {
            if let Some(capital_gain) = &events.capital_gains {
                let mut data = capital_gain.values().cloned().collect::<Vec<CapitalGain>>();
                data.sort_unstable_by_key(|d| d.date);
                return Ok(data);
            }
        }
        Ok(vec![])
    }
}

/// Struct for single quote
#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Quote {
    pub timestamp: i64,
    pub open: Decimal,
    pub high: Decimal,
    pub low: Decimal,
    pub volume: u64,
    pub close: Decimal,
    pub adjclose: Decimal,
}

#[derive(Deserialize, Debug)]
pub struct YChart {
    pub result: Option<Vec<YQuoteBlock>>,
    pub error: Option<YErrorMessage>,
}

#[derive(Deserialize, Debug)]
pub struct YQuoteBlock {
    pub meta: YMetaData,
    pub timestamp: Option<Vec<i64>>,
    pub events: Option<EventsBlock>,
    pub indicators: QuoteBlock,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct YMetaData {
    pub currency: Option<String>,
    pub symbol: String,
    pub long_name: Option<String>,
    pub short_name: Option<String>,
    pub instrument_type: String,
    pub exchange_name: String,
    pub full_exchange_name: String,
    #[serde(default)]
    pub first_trade_date: Option<i32>,
    pub regular_market_time: Option<u32>,
    pub gmtoffset: i32,
    pub timezone: String,
    pub exchange_timezone_name: String,
    pub regular_market_price: Option<Decimal>,
    pub chart_previous_close: Option<Decimal>,
    pub previous_close: Option<Decimal>,
    pub has_pre_post_market_data: bool,
    pub fifty_two_week_high: Option<Decimal>,
    pub fifty_two_week_low: Option<Decimal>,
    pub regular_market_day_high: Option<Decimal>,
    pub regular_market_day_low: Option<Decimal>,
    pub regular_market_volume: Option<Decimal>,
    #[serde(default)]
    pub scale: Option<i32>,
    pub price_hint: i32,
    pub current_trading_period: CurrentTradingPeriod,
    #[serde(default)]
    pub trading_periods: TradingPeriods,
    pub data_granularity: String,
    pub range: String,
    pub valid_ranges: Vec<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct TradingPeriods {
    pub pre: Option<Vec<Vec<PeriodInfo>>>,
    pub regular: Option<Vec<Vec<PeriodInfo>>>,
    pub post: Option<Vec<Vec<PeriodInfo>>>,
}

impl<'de> Deserialize<'de> for TradingPeriods {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            Regular,
            Pre,
            Post,
        }

        struct TradingPeriodsVisitor;

        impl<'de> Visitor<'de> for TradingPeriodsVisitor {
            type Value = TradingPeriods;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct (or array) TradingPeriods")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<TradingPeriods, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let mut regular: Vec<PeriodInfo> = Vec::new();

                while let Ok(Some(mut e)) = seq.next_element::<Vec<PeriodInfo>>() {
                    regular.append(&mut e);
                }

                Ok(TradingPeriods {
                    pre: None,
                    regular: Some(vec![regular]),
                    post: None,
                })
            }

            fn visit_map<V>(self, mut map: V) -> Result<TradingPeriods, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut pre = None;
                let mut post = None;
                let mut regular = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Pre => {
                            if pre.is_some() {
                                return Err(de::Error::duplicate_field("pre"));
                            }
                            pre = Some(map.next_value()?);
                        },
                        Field::Post => {
                            if post.is_some() {
                                return Err(de::Error::duplicate_field("post"));
                            }
                            post = Some(map.next_value()?);
                        },
                        Field::Regular => {
                            if regular.is_some() {
                                return Err(de::Error::duplicate_field("regular"));
                            }
                            regular = Some(map.next_value()?);
                        },
                    }
                }
                Ok(TradingPeriods { pre, post, regular })
            }
        }

        deserializer.deserialize_any(TradingPeriodsVisitor)
    }
}

#[derive(Deserialize, Debug, Clone)]
pub struct CurrentTradingPeriod {
    pub pre: PeriodInfo,
    pub regular: PeriodInfo,
    pub post: PeriodInfo,
}

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct PeriodInfo {
    pub timezone: String,
    pub start: u32,
    pub end: u32,
    pub gmtoffset: i32,
}

#[derive(Deserialize, Debug)]
pub struct QuoteBlock {
    quote: Vec<QuoteList>,
    #[serde(default)]
    adjclose: Option<Vec<AdjClose>>,
}

impl QuoteBlock {
    fn get_ith_quote(&self, timestamp: i64, i: usize) -> Result<Quote, YahooError> {
        let adjclose = match &self.adjclose {
            Some(vec_of_adjclose) => match vec_of_adjclose[0].adjclose {
                Some(ref adjclose) => adjclose[i],
                None => None,
            },
            None => None,
        };

        let quote = &self.quote[0];
        // reject if close is not set

        let open = match quote.open {
            Some(ref open) => open[i],
            None => None,
        };

        let high = match quote.high {
            Some(ref high) => high[i],
            None => None,
        };

        let low = match quote.low {
            Some(ref low) => low[i],
            None => None,
        };

        let volume = match quote.volume {
            Some(ref volume) => volume[i],
            None => None,
        };

        let close = match quote.close {
            Some(ref close) => close[i],
            None => None,
        };

        if close.is_none() {
            return Err(YahooError::NoQuotes);
        }

        Ok(Quote {
            timestamp,
            open: open.unwrap_or(ZERO),
            high: high.unwrap_or(ZERO),
            low: low.unwrap_or(ZERO),
            volume: volume.unwrap_or(0),
            close: close.unwrap(),
            adjclose: adjclose.unwrap_or(ZERO),
        })
    }
}

#[derive(Deserialize, Debug)]
pub struct AdjClose {
    adjclose: Option<Vec<Option<Decimal>>>,
}

#[derive(Deserialize, Debug)]
pub struct QuoteList {
    pub volume: Option<Vec<Option<u64>>>,
    pub high: Option<Vec<Option<Decimal>>>,
    pub close: Option<Vec<Option<Decimal>>>,
    pub low: Option<Vec<Option<Decimal>>>,
    pub open: Option<Vec<Option<Decimal>>>,
}

#[derive(Deserialize, Debug)]
pub struct EventsBlock {
    pub splits: Option<HashMap<i64, Split>>,
    pub dividends: Option<HashMap<i64, Dividend>>,
    #[serde(rename = "capitalGains")]
    pub capital_gains: Option<HashMap<i64, CapitalGain>>,
}

/// This structure simply models a split that has occured.
#[derive(Deserialize, Debug, Clone)]
pub struct Split {
    /// This is the date (timestamp) when the split occured
    pub date: i64,
    /// Numerator of the split. For instance a 1:5 split means you get 5 share
    /// wherever you had one before the split. (Here the numerator is 1 and
    /// denom is 5). A reverse split is considered as nothing but a regular
    /// split with a numerator > denom.
    pub numerator: Decimal,
    /// Denominator of the split. For instance a 1:5 split means you get 5 share
    /// wherever you had one before the split. (Here the numerator is 1 and
    /// denom is 5). A reverse split is considered as nothing but a regular
    /// split with a numerator > denom.
    pub denominator: Decimal,
    /// A textual representation of the split.
    #[serde(rename = "splitRatio")]
    pub split_ratio: String,
}

/// This structure simply models a dividend which has been recorded.
#[derive(Deserialize, Debug, Clone)]
pub struct Dividend {
    /// This is the price of the dividend
    pub amount: Decimal,
    /// This is the ex-dividend date as UNIX timestamp
    pub date: i64,
}

/// This structure simply models a capital gain which has been recorded.
#[derive(Deserialize, Debug, Clone)]
pub struct CapitalGain {
    /// This is the amount of capital gain distributed by the fund
    pub amount: f64,
    /// This is the recorded date of the capital gain
    pub date: i64,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct YQuoteSummary {
    #[serde(rename = "quoteSummary")]
    pub quote_summary: Option<ExtendedQuoteSummary>,
    pub finance: Option<YFinance>,
}

#[derive(Deserialize, Debug)]
pub struct YFinance {
    pub result: Option<serde_json::Value>,
    pub error: Option<YErrorMessage>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct YErrorMessage {
    pub code: Option<String>,
    pub description: Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct ExtendedQuoteSummary {
    pub result: Option<Vec<YSummaryData>>,
    pub error: Option<YErrorMessage>,
}

impl YQuoteSummary {
    pub fn from_json(json: serde_json::Value) -> Result<YQuoteSummary, YahooError> {
        Ok(serde_json::from_value(json)?)
    }
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct YSummaryData {
    pub asset_profile: Option<AssetProfile>,
    pub summary_detail: Option<SummaryDetail>,
    pub default_key_statistics: Option<DefaultKeyStatistics>,
    pub quote_type: Option<QuoteType>,
    pub financial_data: Option<FinancialData>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AssetProfile {
    pub address1: Option<String>,
    pub city: Option<String>,
    pub state: Option<String>,
    pub zip: Option<String>,
    pub country: Option<String>,
    pub phone: Option<String>,
    pub website: Option<String>,
    pub industry: Option<String>,
    pub sector: Option<String>,
    pub long_business_summary: Option<String>,
    pub full_time_employees: Option<u32>,
    pub company_officers: Vec<CompanyOfficer>,
    pub audit_risk: Option<u16>,
    pub board_risk: Option<u16>,
    pub compensation_risk: Option<u16>,
    pub share_holder_rights_risk: Option<u16>,
    pub overall_risk: Option<u16>,
    pub governance_epoch_date: Option<u32>,
    pub compensation_as_of_epoch_date: Option<u32>,
    pub ir_website: Option<String>,
    pub max_age: Option<u32>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CompanyOfficer {
    pub name: String,
    pub age: Option<u32>,
    pub title: String,
    pub year_born: Option<u32>,
    pub fiscal_year: Option<u32>,
    pub total_pay: Option<ValueWrapper>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ValueWrapper {
    pub raw: Option<i64>,
    pub fmt: Option<String>,
    pub long_fmt: Option<String>,
}

fn deserialize_f64_special<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
where
    D: Deserializer<'de>,
{
    let s: serde_json::Value = Deserialize::deserialize(deserializer)?;
    match s {
        serde_json::Value::String(ref v) if v.eq_ignore_ascii_case("infinity") => {
            Ok(Some(f64::INFINITY))
        },
        serde_json::Value::String(ref v) if v.eq_ignore_ascii_case("-infinity") => {
            Ok(Some(f64::NEG_INFINITY))
        },
        serde_json::Value::String(ref v) if v.eq_ignore_ascii_case("nan") => Ok(Some(f64::NAN)),
        serde_json::Value::Number(n) => {
            n.as_f64().ok_or_else(|| serde::de::Error::custom("Invalid number")).map(Some)
        },
        serde_json::Value::Null => Ok(None),
        _ => Err(serde::de::Error::custom(format!(
            "Invalid type for f64: {:?}",
            s
        ))),
    }
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SummaryDetail {
    pub max_age: Option<i64>,
    pub price_hint: Option<i64>,
    pub previous_close: Option<f64>,
    pub open: Option<f64>,
    pub day_low: Option<f64>,
    pub day_high: Option<f64>,
    pub regular_market_previous_close: Option<f64>,
    pub regular_market_open: Option<f64>,
    pub regular_market_day_low: Option<f64>,
    pub regular_market_day_high: Option<f64>,
    pub dividend_rate: Option<f64>,
    pub dividend_yield: Option<f64>,
    pub ex_dividend_date: Option<i64>,
    pub payout_ratio: Option<f64>,
    pub five_year_avg_dividend_yield: Option<f64>,
    pub beta: Option<f64>,
    /// The trailing_pe field may contain the string "Infinity" instead of f64,
    /// in which case we return f64::MAX
    #[serde(
        default,
        deserialize_with = "deserialize_f64_special",
        rename = "trailingPE"
    )]
    pub trailing_pe: Option<f64>,
    #[serde(
        default,
        rename = "forwardPE",
        deserialize_with = "deserialize_f64_special"
    )]
    pub forward_pe: Option<f64>,
    pub volume: Option<u64>,
    pub regular_market_volume: Option<u64>,
    pub average_volume: Option<u64>,
    #[serde(rename = "averageVolume10days")]
    pub average_volume_10days: Option<u64>,
    #[serde(rename = "averageDailyVolume10Day")]
    pub average_daily_volume_10day: Option<u64>,
    pub bid: Option<f64>,
    pub ask: Option<f64>,
    pub bid_size: Option<i64>,
    pub ask_size: Option<i64>,
    pub market_cap: Option<u64>,
    pub fifty_two_week_low: Option<f64>,
    pub fifty_two_week_high: Option<f64>,
    #[serde(
        default,
        rename = "priceToSalesTrailing12Months",
        deserialize_with = "deserialize_f64_special"
    )]
    pub price_to_sales_trailing12months: Option<f64>,
    pub fifty_day_average: Option<f64>,
    pub two_hundred_day_average: Option<f64>,
    pub trailing_annual_dividend_rate: Option<f64>,
    #[serde(default, deserialize_with = "deserialize_f64_special")]
    pub trailing_annual_dividend_yield: Option<f64>,
    pub currency: Option<String>,
    pub from_currency: Option<String>,
    pub to_currency: Option<String>,
    pub last_market: Option<String>,
    pub coin_market_cap_link: Option<String>,
    pub algorithm: Option<String>,
    pub tradeable: Option<bool>,
    pub expire_date: Option<u32>,
    pub strike_price: Option<u32>,
    pub open_interest: Option<Decimal>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DefaultKeyStatistics {
    pub max_age: Option<i64>,
    pub price_hint: Option<u64>,
    pub enterprise_value: Option<i64>,
    #[serde(
        default,
        rename = "forwardPE",
        deserialize_with = "deserialize_f64_special"
    )]
    pub forward_pe: Option<f64>,
    pub profit_margins: Option<f64>,
    pub float_shares: Option<u64>,
    pub shares_outstanding: Option<u64>,
    pub shares_short: Option<u64>,
    pub shares_short_prior_month: Option<u64>,
    pub shares_short_previous_month_date: Option<u64>,
    pub date_short_interest: Option<i64>,
    pub shares_percent_shares_out: Option<f64>,
    pub held_percent_insiders: Option<f64>,
    pub held_percent_institutions: Option<f64>,
    pub short_ratio: Option<f64>,
    pub short_percent_of_float: Option<f64>,
    pub beta: Option<f64>,
    pub implied_shares_outstanding: Option<u64>,
    pub category: Option<String>,
    pub book_value: Option<f64>,
    pub price_to_book: Option<f64>,
    pub fund_family: Option<String>,
    pub fund_inception_date: Option<u32>,
    pub legal_type: Option<String>,
    pub last_fiscal_year_end: Option<i64>,
    pub next_fiscal_year_end: Option<i64>,
    pub most_recent_quarter: Option<i64>,
    pub earnings_quarterly_growth: Option<f64>,
    pub net_income_to_common: Option<i64>,
    pub trailing_eps: Option<f64>,
    pub forward_eps: Option<f64>,
    pub last_split_factor: Option<String>,
    pub last_split_date: Option<i64>,
    pub enterprise_to_revenue: Option<f64>,
    pub enterprise_to_ebitda: Option<f64>,
    #[serde(rename = "52WeekChange")]
    pub fifty_two_week_change: Option<f64>,
    #[serde(rename = "SandP52WeekChange")]
    pub sand_p_fifty_two_week_change: Option<f64>,
    pub last_dividend_value: Option<f64>,
    pub last_dividend_date: Option<i64>,
    pub latest_share_class: Option<String>,
    pub lead_investor: Option<String>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuoteType {
    pub exchange: Option<String>,
    pub quote_type: Option<String>,
    pub symbol: Option<String>,
    pub underlying_symbol: Option<String>,
    pub short_name: Option<String>,
    pub long_name: Option<String>,
    pub first_trade_date_epoch_utc: Option<i64>,
    #[serde(rename = "timeZoneFullName")]
    pub timezone_full_name: Option<String>,
    #[serde(rename = "timeZoneShortName")]
    pub timezone_short_name: Option<String>,
    pub uuid: Option<String>,
    pub message_board_id: Option<String>,
    pub gmt_off_set_milliseconds: Option<i64>,
    pub max_age: Option<i64>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct FinancialData {
    pub max_age: Option<i64>,
    pub current_price: Option<f64>,
    pub target_high_price: Option<f64>,
    pub target_low_price: Option<f64>,
    pub target_mean_price: Option<f64>,
    pub target_median_price: Option<f64>,
    pub recommendation_mean: Option<f64>,
    pub recommendation_key: Option<String>,
    pub number_of_analyst_opinions: Option<u64>,
    pub total_cash: Option<u64>,
    pub total_cash_per_share: Option<f64>,
    pub ebitda: Option<i64>,
    pub total_debt: Option<u64>,
    pub quick_ratio: Option<f64>,
    pub current_ratio: Option<f64>,
    pub total_revenue: Option<i64>,
    pub debt_to_equity: Option<f64>,
    pub revenue_per_share: Option<f64>,
    pub return_on_assets: Option<f64>,
    pub return_on_equity: Option<f64>,
    pub gross_profits: Option<i64>,
    pub free_cashflow: Option<i64>,
    pub operating_cashflow: Option<i64>,
    pub earnings_growth: Option<f64>,
    pub revenue_growth: Option<f64>,
    pub gross_margins: Option<f64>,
    pub ebitda_margins: Option<f64>,
    pub operating_margins: Option<f64>,
    pub profit_margins: Option<f64>,
    pub financial_currency: Option<String>,
}

// Структуры для earnings dates response
#[derive(Deserialize, Debug, Clone)]
pub struct YEarningsResponse {
    pub finance: YEarningsFinance,
}

#[derive(Deserialize, Debug, Clone)]
pub struct YEarningsFinance {
    pub result: Vec<YEarningsResult>,
    pub error: Option<serde_json::Value>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct YEarningsResult {
    pub documents: Vec<YEarningsDocument>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct YEarningsDocument {
    pub columns: Vec<YEarningsColumn>,
    pub rows: Vec<Vec<serde_json::Value>>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct YEarningsColumn {
    pub label: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct FinancialEvent {
    pub earnings_date: OffsetDateTime,
    pub event_type: String,
    pub eps_estimate: Option<f64>,
    pub reported_eps: Option<f64>,
    pub surprise_percent: Option<f64>,
    pub timezone: Option<String>,
}

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

    #[test]
    fn test_deserialize_period_info() {
        let period_info_json = r#"
        {
            "timezone": "EST",
            "start": 1705501800,
            "end": 1705525200,
            "gmtoffset": -18000
        }
        "#;
        let period_info_expected = PeriodInfo {
            timezone: "EST".to_string(),
            start: 1705501800,
            end: 1705525200,
            gmtoffset: -18000,
        };
        let period_info_deserialized: PeriodInfo = serde_json::from_str(period_info_json).unwrap();
        assert_eq!(&period_info_deserialized, &period_info_expected);
    }

    #[test]
    fn test_deserialize_trading_periods_simple() {
        let trading_periods_json = r#"
        [
            [
                {
                    "timezone": "EST",
                    "start": 1705501800,
                    "end": 1705525200,
                    "gmtoffset": -18000
                }

            ]
        ]
        "#;
        let trading_periods_expected = TradingPeriods {
            pre: None,
            regular: Some(vec![vec![PeriodInfo {
                timezone: "EST".to_string(),
                start: 1705501800,
                end: 1705525200,
                gmtoffset: -18000,
            }]]),
            post: None,
        };
        let trading_periods_deserialized: TradingPeriods =
            serde_json::from_str(trading_periods_json).unwrap();
        assert_eq!(&trading_periods_expected, &trading_periods_deserialized);
    }

    #[test]
    fn test_deserialize_trading_periods_complex_regular_only() {
        let trading_periods_json = r#"
        {
            "regular": [
              [
                {
                  "timezone": "EST",
                  "start": 1705501800,
                  "end": 1705525200,
                  "gmtoffset": -18000
                }
              ]
            ]
        }
       "#;
        let trading_periods_expected = TradingPeriods {
            pre: None,
            regular: Some(vec![vec![PeriodInfo {
                timezone: "EST".to_string(),
                start: 1705501800,
                end: 1705525200,
                gmtoffset: -18000,
            }]]),
            post: None,
        };
        let trading_periods_deserialized: TradingPeriods =
            serde_json::from_str(trading_periods_json).unwrap();
        assert_eq!(&trading_periods_expected, &trading_periods_deserialized);
    }

    #[test]
    fn test_deserialize_trading_periods_complex() {
        let trading_periods_json = r#"
        {
            "pre": [
              [
                {
                  "timezone": "EST",
                  "start": 1705482000,
                  "end": 1705501800,
                  "gmtoffset": -18000
                }
              ]
            ],
            "post": [
              [
                {
                  "timezone": "EST",
                  "start": 1705525200,
                  "end": 1705539600,
                  "gmtoffset": -18000
                }
              ]
            ],
            "regular": [
              [
                {
                  "timezone": "EST",
                  "start": 1705501800,
                  "end": 1705525200,
                  "gmtoffset": -18000
                }
              ]
            ]
        }
       "#;
        let trading_periods_expected = TradingPeriods {
            pre: Some(vec![vec![PeriodInfo {
                timezone: "EST".to_string(),
                start: 1705482000,
                end: 1705501800,
                gmtoffset: -18000,
            }]]),
            regular: Some(vec![vec![PeriodInfo {
                timezone: "EST".to_string(),
                start: 1705501800,
                end: 1705525200,
                gmtoffset: -18000,
            }]]),
            post: Some(vec![vec![PeriodInfo {
                timezone: "EST".to_string(),
                start: 1705525200,
                end: 1705539600,
                gmtoffset: -18000,
            }]]),
        };
        let trading_periods_deserialized: TradingPeriods =
            serde_json::from_str(trading_periods_json).unwrap();
        assert_eq!(&trading_periods_expected, &trading_periods_deserialized);
    }

    #[test]
    fn test_deserialize_f64_special() {
        #[derive(Debug, Deserialize)]
        #[allow(dead_code)]
        struct MyStruct {
            #[serde(default, deserialize_with = "deserialize_f64_special")]
            bad: Option<f64>,
            good: Option<f64>,
        }

        let json_data = r#"{ "bad": "Infinity", "good": 999.999 }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();

        let json_data = r#"{ "bad": 123.45 }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();

        let json_data = r#"{ "bad": null }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();

        let json_data = r#"{ "bad": "NaN" }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();

        let json_data = r#"{ "bad": "-Infinity" }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();

        let json_data = r#"{ }"#;
        let _: MyStruct = serde_json::from_str(json_data).unwrap();
    }
}