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
//! Contains Rust types of deserialized responses from Marketstack REST API.

use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};

/// Pagination Information returned by Marketstack API.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PaginationInfo {
    /// Page limit.
    pub limit: u64,
    /// Page offset.
    pub offset: u64,
    /// Results count on the current page.
    pub count: u64,
    /// Total count of the results available.
    pub total: u64,
}

/// Rust representation of single data item from Marketstack `eod` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EodDataItem {
    /// Exact date/time the given data was collected in ISO-8601 format.
    pub date: DateTime<Utc>,
    /// Stock ticker symbol of the current data object.
    pub symbol: String,
    /// Exchange MIC identification associated with the current data object.
    pub exchange: String,
    /// Split factor used to adjust prices when a company splits, reverse splits or pays a
    /// distribution.
    pub split_factor: f64,
    /// Distribution of earnings to shareholders.
    pub dividend: f64,
    /// Raw opening price of the given stock ticker.
    pub open: f64,
    /// Raw high price of the given stock ticker.
    pub high: f64,
    /// Raw low price of the given stock ticker.
    pub low: f64,
    /// Raw closing price of the given stock ticker.
    pub close: f64,
    /// Raw volume of the given stock ticker.
    pub volume: f64,
    /// Adjusted opening price of the given stock ticker.
    pub adj_open: f64,
    /// Adjusted high price of the given stock ticker.
    pub adj_high: f64,
    /// Adjusted low price of the given stock ticker.
    pub adj_low: f64,
    /// Adjusted closing price of the given stock ticker.
    pub adj_close: f64,
    /// Adjusted volume of the given stock ticker.
    pub adj_volume: f64,
}

/// Rust representation of the JSON response from `eod` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EodData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<EodDataItem>,
}

/// Rust representation of single data item from Marketstack `splits` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SplitsDataItem {
    /// Exact date/time the given data was collected in ISO-8601 format.
    pub date: NaiveDate,
    /// Split factor for that symbol on the date.
    pub split_factor: f64,
    /// Stock ticker symbol of the current data object.
    pub symbol: String,
}

/// Rust representation of the JSON response from `splits` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SplitsData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<SplitsDataItem>,
}

/// Rust representation of single data item from Marketstack `dividends` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DividendsDataItem {
    /// Exact date/time the given data was collected in ISO-8601 format.
    pub date: NaiveDate,
    /// Dividend for that symbol on the date.
    pub dividend: f64,
    /// Stock ticker symbol of the current data object.
    pub symbol: String,
}

/// Rust representation of the JSON response from `dividends` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DividendsData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<DividendsDataItem>,
}

/// Rust representation of single data item from Marketstack `currencies` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CurrenciesDataItem {
    /// 3-letter code of the given currency.
    pub code: String,
    /// Name of the given currency.
    pub name: String,
    /// Text symbol of the given currency.
    pub symbol: String,
}

/// Rust representation of the JSON response from `currencies` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CurrenciesData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<CurrenciesDataItem>,
}

/// Rust representation of single data item from Marketstack `timezones` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TimezonesDataItem {
    /// Name of the given timezone.
    pub timezone: String,
    /// Abbreviation of the given timezone.
    pub abbr: String,
    /// Summer time abbreviation of the given timezone.
    pub abbr_dst: String,
}

/// Rust representation of the JSON response from `timezones` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TimezonesData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<TimezonesDataItem>,
}

/// Rust represenation of a stock exchange.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StockExchange {
    /// Name of the stock exchange associated with the given stock ticker.
    pub name: String,
    /// Name of the stock exchange associated with the given stock ticker.
    pub acronym: String,
    /// MIC identification of the stock exchange associated with the given stock ticker.
    pub mic: String,
    /// Country of the stock exchange associated with the given stock ticker.
    pub country: String,
    /// 3-letter country code of the stock exchange associated with the given stock ticker.
    pub country_code: String,
    /// City of the stock exchange associated with the given stock ticker.
    pub city: String,
    /// Website URL of the stock exchange associated with the given stock ticker.
    pub website: String,
}

/// Rust representation of a single data item from Marketstack `tickers` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TickersDataItem {
    /// Name of the given stock ticker.
    pub name: String,
    /// Symbol of the given stock ticker.
    pub symbol: String,
    /// Whether intraday data is available for the stock ticker.
    pub has_intraday: bool,
    /// Whether eod data is available for the stock ticker.
    pub has_eod: bool,
    /// Country ticker is traded in - if available, else `None`.
    pub country: Option<String>,
    /// Stock exchange the ticker is traded in.
    pub stock_exchange: StockExchange,
}

/// Rust representation of a single data item from Marketstack `tickers/[symbol]/eod` response.
/// Implementation seems slightly repetitive, but return types from Marketstack are slightly
/// inconsistent for this endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TickerEodDataInner {
    /// Name of the given stock ticker.
    pub name: String,
    /// Symbol of the given stock ticker.
    pub symbol: String,
    /// Whether intraday data is available for the stock ticker.
    pub has_intraday: bool,
    /// Whether eod data is available for the stock ticker.
    pub has_eod: bool,
    /// Country ticker is traded in - if available, else `None`.
    pub country: Option<String>,
    /// Collection of eod data for the ticker.
    pub eod: Vec<EodDataItem>,
}

/// Rust representation of the JSON response from `tickers` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TickersData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<TickersDataItem>,
}

/// Rust representation of the JSON response from `tickers/[symbol]/eod` marketstack endpoint.
/// Implementation seems slightly repetitive, but return types from Marketstack are slightly
/// inconsistent for this endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TickersEodData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: TickerEodDataInner,
}

/// Rust representation of a single data item from Marketstack `exchanges` endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExchangesDataItem {
    /// Flattened out fields from `StockExchange` into `ExchangesDataItem`.
    #[serde(flatten)]
    pub stock_exchange: StockExchange,
    /// Timezone of the stock exchange.
    pub timezone: TimezonesDataItem,
    /// Currency of the stock exchange.
    pub currency: CurrenciesDataItem,
}

/// Rust representation of the JSON response from `exchanges` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExchangesData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<ExchangesDataItem>,
}

/// Rust representation of a single data item from Marketstack `exchanges/[mic]/eod`` response.
/// Implementation seems slightly repetitive, but return types from Marketstack are slightly
/// inconsistent for this endpoint.
/// Marketstack somehow returns inconsistent data: Country code is not returned here.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExchangesEodDataInner {
    /// Name of the stock exchange associated with the given stock ticker.
    pub name: String,
    /// Name of the stock exchange associated with the given stock ticker.
    pub acronym: String,
    /// MIC identification of the stock exchange associated with the given stock ticker.
    pub mic: String,
    /// Country of the stock exchange associated with the given stock ticker.
    pub country: String,
    /// City of the stock exchange associated with the given stock ticker.
    pub city: String,
    /// Website URL of the stock exchange associated with the given stock ticker.
    pub website: String,
    /// Collection of eod data for the exchange.
    pub eod: Vec<EodDataItem>,
}

/// Rust representation of the JSON response from `exchanges/[mic]/eod` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExchangesEodData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: ExchangesEodDataInner,
}

/// Rust representation of a single data item from Marketstack `intraday` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IntradayDataItem {
    /// Raw opening price of the given stock ticker.
    pub open: f64,
    /// Raw high price of the given stock ticker.
    pub high: f64,
    /// Raw low price of the given stock ticker.
    pub low: f64,
    /// Last executed trade of the given symbol on its exchange.
    pub last: f64,
    /// Raw closing price of the given stock ticker.
    pub close: f64,
    /// Raw volume of the given stock ticker.
    pub volume: f64,
    /// Exact UTC date/time the given data was collected in ISO-8601 format.
    pub date: DateTime<Utc>,
    /// Stock ticker symbol of the current data object.
    pub symbol: String,
    /// Exchange MIC identification associated with the current data object.
    pub exchange: String,
}

/// Rust representation of the JSON response from `intraday` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IntradayData {
    /// Corresponds to pagination entry from JSON response from marketstack.
    pub pagination: PaginationInfo,
    /// Corresponds to data entry from JSON response from marketstack.
    pub data: Vec<IntradayDataItem>,
}

#[cfg(test)]
mod tests {
    use chrono::{Datelike, NaiveDate};

    use crate::{
        CurrenciesData, DividendsData, EodData, EodDataItem, ExchangesData, ExchangesEodData,
        IntradayData, SplitsData, TickersData, TimezonesData,
    };

    #[test]
    fn test_deserialize_eod() {
        let json_data = r#"{
        "pagination": {
            "limit": 100,
            "offset": 0,
            "count": 100,
            "total": 9944
        },
        "data": [
            {
                "open": 129.8,
                "high": 133.04,
                "low": 129.47,
                "close": 132.995,
                "volume": 106686703.0,
                "adj_high": 133.04,
                "adj_low": 129.47,
                "adj_close": 132.995,
                "adj_open": 129.8,
                "adj_volume": 106686703.0,
                "split_factor": 1.0,
                "dividend": 0.0,
                "symbol": "AAPL",
                "exchange": "XNAS",
                "date": "2021-04-09T00:00:00+0000"
            }
        ]
    }"#;

        let eod_data: EodData = serde_json::from_str(json_data).unwrap();
        assert_eq!(eod_data.data[0].open, 129.8);
        assert_eq!(eod_data.data[0].symbol, "AAPL");
        assert_eq!(eod_data.pagination.limit, 100);
    }

    #[test]
    fn test_deserialize_splits() {
        let json_data = r#"{
            "pagination": {
              "limit": 100,
              "offset": 0,
              "count": 5,
              "total": 5
            },
            "data": [
              {
                "date": "2020-08-31",
                "split_factor": 4,
                "symbol": "AAPL"
              },
              {
                "date": "2014-06-09",
                "split_factor": 7,
                "symbol": "AAPL"
              },
              {
                "date": "2005-02-28",
                "split_factor": 2,
                "symbol": "AAPL"
              },
              {
                "date": "2000-06-21",
                "split_factor": 2,
                "symbol": "AAPL"
              },
              {
                "date": "1987-06-16",
                "split_factor": 2,
                "symbol": "AAPL"
              }
            ]
          }"#;

        let splits_data: SplitsData = serde_json::from_str(json_data).unwrap();
        assert_eq!(splits_data.data[0].split_factor, 4.0);
        assert_eq!(
            splits_data.data[0].date,
            NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
        );
        assert_eq!(splits_data.data[0].symbol, "AAPL");

        assert_eq!(splits_data.data[4].split_factor, 2.0);
        assert_eq!(
            splits_data.data[4].date,
            NaiveDate::from_ymd_opt(1987, 6, 16).unwrap()
        );
        assert_eq!(splits_data.data[4].symbol, "AAPL");
    }

    #[test]
    fn test_deserialize_dividends() {
        let json_data = r#"{
            "pagination": {
              "limit": 5,
              "offset": 0,
              "count": 5,
              "total": 68
            },
            "data": [
              {
                "date": "2023-08-11",
                "dividend": 0.24,
                "symbol": "AAPL"
              },
              {
                "date": "2023-05-12",
                "dividend": 0.24,
                "symbol": "AAPL"
              },
              {
                "date": "2023-02-10",
                "dividend": 0.23,
                "symbol": "AAPL"
              },
              {
                "date": "2022-12-23",
                "dividend": 0.17,
                "symbol": "AAPL"
              },
              {
                "date": "2022-11-04",
                "dividend": 0.23,
                "symbol": "AAPL"
              }
            ]
          }"#;

        let dividends_data: DividendsData = serde_json::from_str(json_data).unwrap();
        assert_eq!(dividends_data.pagination.limit, 5);
        assert_eq!(dividends_data.data[0].dividend, 0.24);
        assert_eq!(
            dividends_data.data[0].date,
            NaiveDate::from_ymd_opt(2023, 8, 11).unwrap()
        );
    }

    #[test]
    fn test_deserialize_currencies() {
        let json_data = r#"{
            "pagination": {
              "limit": 3,
              "offset": 0,
              "count": 3,
              "total": 42
            },
            "data": [
              {
                "code": "USD",
                "symbol": "$",
                "name": "US Dollar"
              },
              {
                "code": "ARS",
                "symbol": "AR$",
                "name": "Argentine Peso"
              },
              {
                "code": "EUR",
                "symbol": "€",
                "name": "Euro"
              }
            ]
          }"#;

        let currencies_data: CurrenciesData = serde_json::from_str(json_data).unwrap();
        assert_eq!(currencies_data.pagination.limit, 3);
        assert_eq!(currencies_data.data[0].code, "USD");
        assert_eq!(currencies_data.data[0].symbol, "$");
        assert_eq!(currencies_data.data[0].name, "US Dollar");
    }

    #[test]
    fn test_deserialize_timezones() {
        let json_data = r#"{
            "pagination": {
              "limit": 3,
              "offset": 0,
              "count": 3,
              "total": 57
            },
            "data": [
              {
                "timezone": "America/New_York",
                "abbr": "EST",
                "abbr_dst": "EDT"
              },
              {
                "timezone": "America/Argentina/Buenos_Aires",
                "abbr": "-03",
                "abbr_dst": "-03"
              },
              {
                "timezone": "Europe/Vienna",
                "abbr": "CET",
                "abbr_dst": "CEST"
              }
            ]
          }"#;

        let timezones_data: TimezonesData = serde_json::from_str(json_data).unwrap();
        assert_eq!(timezones_data.data[0].timezone, "America/New_York");
        assert_eq!(timezones_data.data[0].abbr, "EST");
        assert_eq!(timezones_data.data[0].abbr_dst, "EDT");
    }

    #[test]
    fn test_deserialize_tickers() {
        let json_data = r#"{
          "pagination": {
            "limit": 3,
            "offset": 0,
            "count": 3,
            "total": 287233
          },
          "data": [
            {
              "name": "Microsoft Corporation",
              "symbol": "MSFT",
              "has_intraday": false,
              "has_eod": true,
              "country": null,
              "stock_exchange": {
                "name": "NASDAQ Stock Exchange",
                "acronym": "NASDAQ",
                "mic": "XNAS",
                "country": "USA",
                "country_code": "US",
                "city": "New York",
                "website": "www.nasdaq.com"
              }
            },
            {
              "name": "Apple Inc",
              "symbol": "AAPL",
              "has_intraday": false,
              "has_eod": true,
              "country": null,
              "stock_exchange": {
                "name": "NASDAQ Stock Exchange",
                "acronym": "NASDAQ",
                "mic": "XNAS",
                "country": "USA",
                "country_code": "US",
                "city": "New York",
                "website": "www.nasdaq.com"
              }
            },
            {
              "name": "Amazon.com Inc",
              "symbol": "AMZN",
              "has_intraday": false,
              "has_eod": true,
              "country": null,
              "stock_exchange": {
                "name": "NASDAQ Stock Exchange",
                "acronym": "NASDAQ",
                "mic": "XNAS",
                "country": "USA",
                "country_code": "US",
                "city": "New York",
                "website": "www.nasdaq.com"
              }
            }
          ]
        }"#;

        let tickers_data: TickersData = serde_json::from_str(json_data).unwrap();
        assert_eq!(tickers_data.data[0].name, "Microsoft Corporation");
        assert_eq!(tickers_data.data[0].symbol, "MSFT");
        assert!(!tickers_data.data[0].has_intraday);
        assert!(tickers_data.data[0].has_eod);
        assert_eq!(tickers_data.data[0].country, None);
        assert_eq!(
            tickers_data.data[0].stock_exchange.name,
            "NASDAQ Stock Exchange"
        );
        assert_eq!(tickers_data.data[0].stock_exchange.acronym, "NASDAQ");
        assert_eq!(tickers_data.data[0].stock_exchange.mic, "XNAS");
        assert_eq!(tickers_data.data[0].stock_exchange.country, "USA");
        assert_eq!(tickers_data.data[0].stock_exchange.country_code, "US");
        assert_eq!(tickers_data.data[0].stock_exchange.city, "New York");
        assert_eq!(
            tickers_data.data[0].stock_exchange.website,
            "www.nasdaq.com"
        );
    }

    #[test]
    fn test_deserialize_tickers_eod_latest() {
        let json_data = r#"{
        "open": 166.91,
        "high": 168.96,
        "low": 166.83,
        "close": 168.22,
        "volume": 58468600,
        "adj_high": 168.96,
        "adj_low": 166.83,
        "adj_close": 168.22,
        "adj_open": 166.91,
        "adj_volume": 58499129,
        "split_factor": 1,
        "dividend": 0,
        "symbol": "AAPL",
        "exchange": "XNAS",
        "date": "2023-10-27T00:00:00+0000"
      }"#;

        let tickers_eod_data: EodDataItem = serde_json::from_str(json_data).unwrap();
        assert_eq!(tickers_eod_data.open, 166.91);
        assert_eq!(tickers_eod_data.symbol, "AAPL");
    }

    #[test]
    fn test_deserialize_tickers_eod_date() {
        let json_data = r#"{
            "open": 166.91,
            "high": 168.96,
            "low": 166.83,
            "close": 168.22,
            "volume": 58468600,
            "adj_high": 168.96,
            "adj_low": 166.83,
            "adj_close": 168.22,
            "adj_open": 166.91,
            "adj_volume": 58499129,
            "split_factor": 1,
            "dividend": 0,
            "symbol": "AAPL",
            "exchange": "XNAS",
            "date": "2023-10-27T00:00:00+0000"
          }"#;

        let tickers_eod_data: EodDataItem = serde_json::from_str(json_data).unwrap();
        assert_eq!(tickers_eod_data.open, 166.91);
        assert_eq!(tickers_eod_data.date.day(), 27);
    }

    #[test]
    fn test_deserialize_exchanges() {
        let json_data = r#"{
            "pagination": {
              "limit": 2,
              "offset": 0,
              "count": 2,
              "total": 69
            },
            "data": [
              {
                "name": "NASDAQ Stock Exchange",
                "acronym": "NASDAQ",
                "mic": "XNAS",
                "country": "USA",
                "country_code": "US",
                "city": "New York",
                "website": "www.nasdaq.com",
                "timezone": {
                  "timezone": "America/New_York",
                  "abbr": "EST",
                  "abbr_dst": "EDT"
                },
                "currency": {
                  "code": "USD",
                  "symbol": "$",
                  "name": "US Dollar"
                }
              },
              {
                "name": "New York Stock Exchange",
                "acronym": "NYSE",
                "mic": "XNYS",
                "country": "USA",
                "country_code": "US",
                "city": "New York",
                "website": "www.nyse.com",
                "timezone": {
                  "timezone": "America/New_York",
                  "abbr": "EST",
                  "abbr_dst": "EDT"
                },
                "currency": {
                  "code": "USD",
                  "symbol": "$",
                  "name": "US Dollar"
                }
              }
            ]
          }"#;

        let exchanges_data: ExchangesData = serde_json::from_str(json_data).unwrap();
        assert_eq!(exchanges_data.pagination.limit, 2);
        assert_eq!(
            exchanges_data.data[0].stock_exchange.name,
            "NASDAQ Stock Exchange"
        );
        assert_eq!(exchanges_data.data[0].stock_exchange.acronym, "NASDAQ");
        assert_eq!(exchanges_data.data[0].stock_exchange.mic, "XNAS");
        assert_eq!(exchanges_data.data[0].stock_exchange.country, "USA");
        assert_eq!(exchanges_data.data[0].stock_exchange.country_code, "US");
        assert_eq!(exchanges_data.data[0].stock_exchange.city, "New York");
        assert_eq!(
            exchanges_data.data[0].stock_exchange.website,
            "www.nasdaq.com"
        );
        assert_eq!(exchanges_data.data[0].timezone.timezone, "America/New_York");
        assert_eq!(exchanges_data.data[0].timezone.abbr, "EST");
        assert_eq!(exchanges_data.data[0].timezone.abbr_dst, "EDT");
        assert_eq!(exchanges_data.data[0].currency.code, "USD");
        assert_eq!(exchanges_data.data[0].currency.symbol, "$");
        assert_eq!(exchanges_data.data[0].currency.name, "US Dollar");
    }

    #[test]
    fn test_deserialize_exchanges_eod() {
        let json_data = r#"{
            "pagination": {
              "limit": 2,
              "offset": 0,
              "count": 2,
              "total": 251
            },
            "data": {
              "name": "NASDAQ Stock Exchange",
              "acronym": "NASDAQ",
              "mic": "XNAS",
              "country": "USA",
              "city": "New York",
              "website": "WWW.NASDAQ.COM",
              "eod": [
                {
                  "open": 169.35,
                  "high": 170.9,
                  "low": 167.9,
                  "close": 170.77,
                  "volume": 44768914,
                  "adj_high": 170.9,
                  "adj_low": 167.9,
                  "adj_close": 170.77,
                  "adj_open": 169.35,
                  "adj_volume": 44846017,
                  "split_factor": 1,
                  "dividend": 0,
                  "symbol": "AAPL",
                  "exchange": "XNAS",
                  "date": "2023-10-31T00:00:00+0000"
                },
                {
                  "open": 169.02,
                  "high": 171.17,
                  "low": 168.87,
                  "close": 170.29,
                  "volume": 51082900,
                  "adj_high": 171.17,
                  "adj_low": 168.87,
                  "adj_close": 170.29,
                  "adj_open": 169.02,
                  "adj_volume": 51130955,
                  "split_factor": 1,
                  "dividend": 0,
                  "symbol": "AAPL",
                  "exchange": "XNAS",
                  "date": "2023-10-30T00:00:00+0000"
                }
              ]
            }
          }"#;

        let exchanges_eod_result: ExchangesEodData = serde_json::from_str(json_data).unwrap();

        assert_eq!(exchanges_eod_result.pagination.limit, 2);
        assert_eq!(exchanges_eod_result.data.name, "NASDAQ Stock Exchange");
        assert_eq!(exchanges_eod_result.data.acronym, "NASDAQ");
        assert_eq!(exchanges_eod_result.data.mic, "XNAS");
        assert_eq!(exchanges_eod_result.data.country, "USA");
        assert_eq!(exchanges_eod_result.data.city, "New York");
        assert_eq!(exchanges_eod_result.data.website, "WWW.NASDAQ.COM");
        assert_eq!(exchanges_eod_result.data.eod[0].open, 169.35);
    }

    #[test]
    fn test_deserialize_intraday() {
        let json_data = r#"{
            "pagination": {
              "limit": 2,
              "offset": 0,
              "count": 2,
              "total": 11872
            },
            "data": [
              {
                "open": 173.75,
                "high": 176.81,
                "low": 173.37,
                "last": 176.71,
                "close": 177.57,
                "volume": 1796985,
                "date": "2023-11-03T20:00:00+0000",
                "symbol": "AAPL",
                "exchange": "IEXG"
              },
              {
                "open": 173.75,
                "high": 176.51,
                "low": 173.37,
                "last": 176.13,
                "close": 177.57,
                "volume": 1516371,
                "date": "2023-11-03T19:00:00+0000",
                "symbol": "AAPL",
                "exchange": "IEXG"
              }
            ]
          }"#;

        let intraday_data: IntradayData = serde_json::from_str(json_data).unwrap();

        assert_eq!(intraday_data.pagination.limit, 2);
        assert_eq!(intraday_data.data[0].open, 173.75);
        assert_eq!(intraday_data.data[0].high, 176.81);
        assert_eq!(intraday_data.data[0].low, 173.37);
        assert_eq!(intraday_data.data[0].last, 176.71);
        assert_eq!(intraday_data.data[0].close, 177.57);
        assert_eq!(intraday_data.data[0].volume, 1796985.0);

        let naive_date_time = NaiveDate::from_ymd_opt(2023, 11, 3)
            .unwrap()
            .and_hms_opt(20, 0, 0)
            .unwrap()
            .and_utc();
        assert_eq!(intraday_data.data[0].date, naive_date_time);
        assert_eq!(intraday_data.data[0].symbol, "AAPL");
        assert_eq!(intraday_data.data[0].exchange, "IEXG");
    }
}