Skip to main content

the_odds_api/
types.rs

1//! Type definitions for API parameters and enums.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// Region codes for bookmaker filtering.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum Region {
10    /// United States
11    Us,
12    /// United States (secondary)
13    Us2,
14    /// United Kingdom
15    Uk,
16    /// Australia
17    Au,
18    /// Europe
19    Eu,
20}
21
22impl fmt::Display for Region {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Region::Us => write!(f, "us"),
26            Region::Us2 => write!(f, "us2"),
27            Region::Uk => write!(f, "uk"),
28            Region::Au => write!(f, "au"),
29            Region::Eu => write!(f, "eu"),
30        }
31    }
32}
33
34/// Market types for odds requests.
35#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
36#[serde(rename_all = "snake_case")]
37pub enum Market {
38    /// Head to head (moneyline)
39    H2h,
40    /// Point spreads
41    Spreads,
42    /// Totals (over/under)
43    Totals,
44    /// Outright/futures
45    Outrights,
46    /// Head to head for draw (3-way)
47    H2hLay,
48    /// Custom/alternate market (for event odds endpoint)
49    #[serde(untagged)]
50    Custom(String),
51}
52
53impl fmt::Display for Market {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            Market::H2h => write!(f, "h2h"),
57            Market::Spreads => write!(f, "spreads"),
58            Market::Totals => write!(f, "totals"),
59            Market::Outrights => write!(f, "outrights"),
60            Market::H2hLay => write!(f, "h2h_lay"),
61            Market::Custom(s) => write!(f, "{}", s),
62        }
63    }
64}
65
66impl From<&str> for Market {
67    fn from(s: &str) -> Self {
68        match s {
69            "h2h" => Market::H2h,
70            "spreads" => Market::Spreads,
71            "totals" => Market::Totals,
72            "outrights" => Market::Outrights,
73            "h2h_lay" => Market::H2hLay,
74            other => Market::Custom(other.to_string()),
75        }
76    }
77}
78
79/// Date format for API responses.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
81#[serde(rename_all = "lowercase")]
82pub enum DateFormat {
83    /// ISO 8601 format (default)
84    #[default]
85    Iso,
86    /// Unix timestamp
87    Unix,
88}
89
90impl fmt::Display for DateFormat {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        match self {
93            DateFormat::Iso => write!(f, "iso"),
94            DateFormat::Unix => write!(f, "unix"),
95        }
96    }
97}
98
99/// Odds format for API responses.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
101#[serde(rename_all = "lowercase")]
102pub enum OddsFormat {
103    /// Decimal odds (default)
104    #[default]
105    Decimal,
106    /// American odds
107    American,
108}
109
110impl fmt::Display for OddsFormat {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            OddsFormat::Decimal => write!(f, "decimal"),
114            OddsFormat::American => write!(f, "american"),
115        }
116    }
117}
118
119/// Sport key type alias for clarity.
120pub type SportKey = String;
121
122/// Event ID type alias for clarity.
123pub type EventId = String;
124
125/// Helper to format a slice as comma-separated values.
126pub(crate) fn format_csv<T: fmt::Display>(items: &[T]) -> String {
127    items
128        .iter()
129        .map(|i| i.to_string())
130        .collect::<Vec<_>>()
131        .join(",")
132}