1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum RateLimitTier {
8 Standard,
10 Premium,
12 Enterprise,
14 Noop,
16 #[serde(other)]
18 Unknown,
19}
20
21impl std::fmt::Display for RateLimitTier {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 match self {
24 Self::Standard => write!(f, "standard"),
25 Self::Premium => write!(f, "premium"),
26 Self::Enterprise => write!(f, "enterprise"),
27 Self::Noop => write!(f, "noop"),
28 Self::Unknown => write!(f, "unknown"),
29 }
30 }
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct ParseRateLimitTierError(String);
36
37impl std::fmt::Display for ParseRateLimitTierError {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(f, "Invalid rate limit tier: {}", self.0)
40 }
41}
42
43impl std::error::Error for ParseRateLimitTierError {}
44
45impl std::str::FromStr for RateLimitTier {
46 type Err = ParseRateLimitTierError;
47
48 fn from_str(s: &str) -> Result<Self, Self::Err> {
49 match s {
50 "standard" => Ok(Self::Standard),
51 "premium" => Ok(Self::Premium),
52 "enterprise" => Ok(Self::Enterprise),
53 "noop" => Ok(Self::Noop),
54 _ => Ok(Self::Unknown),
55 }
56 }
57}
58
59impl TryFrom<&str> for RateLimitTier {
60 type Error = ParseRateLimitTierError;
61
62 fn try_from(value: &str) -> Result<Self, Self::Error> {
63 value.parse()
64 }
65}
66
67impl TryFrom<String> for RateLimitTier {
68 type Error = ParseRateLimitTierError;
69
70 fn try_from(value: String) -> Result<Self, Self::Error> {
71 value.parse()
72 }
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
78pub struct ApiKeyInfo {
79 pub name: String,
81 pub scopes: Vec<String>,
83 #[serde(default, skip_serializing_if = "Option::is_none")]
85 pub last_used_at: Option<DateTime<Utc>>,
86 pub created_at: DateTime<Utc>,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
92#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
93pub struct RateLimitInfo {
94 pub tier: RateLimitTier,
96 #[serde(default, skip_serializing_if = "Option::is_none")]
98 pub limit: Option<i64>,
99 pub current_count: i64,
101 #[serde(default, skip_serializing_if = "Option::is_none")]
103 pub remaining: Option<i64>,
104 pub reset_in_seconds: i64,
106 pub reset_at: DateTime<Utc>,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
112#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
113pub struct UsageApiKeyResponse {
114 pub api_key: ApiKeyInfo,
116 pub rate_limit: RateLimitInfo,
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
122#[serde(rename_all = "lowercase")]
123pub enum AuthenticationMethod {
124 OAuth,
126}
127
128impl std::fmt::Display for AuthenticationMethod {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 match self {
131 Self::OAuth => write!(f, "oauth"),
132 }
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
138pub struct ParseAuthenticationMethodError(String);
139
140impl std::fmt::Display for ParseAuthenticationMethodError {
141 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
142 write!(f, "Invalid authentication method: {}", self.0)
143 }
144}
145
146impl std::error::Error for ParseAuthenticationMethodError {}
147
148impl std::str::FromStr for AuthenticationMethod {
149 type Err = ParseAuthenticationMethodError;
150
151 fn from_str(s: &str) -> Result<Self, Self::Err> {
152 match s {
153 "oauth" => Ok(Self::OAuth),
154 _ => Err(ParseAuthenticationMethodError(s.to_string())),
155 }
156 }
157}
158
159impl TryFrom<&str> for AuthenticationMethod {
160 type Error = ParseAuthenticationMethodError;
161
162 fn try_from(value: &str) -> Result<Self, Self::Error> {
163 value.parse()
164 }
165}
166
167impl TryFrom<String> for AuthenticationMethod {
168 type Error = ParseAuthenticationMethodError;
169
170 fn try_from(value: String) -> Result<Self, Self::Error> {
171 value.parse()
172 }
173}
174
175#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
177#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
178pub struct UsageOAuthResponse {
179 pub authentication_method: AuthenticationMethod,
181 pub message: String,
183}
184
185#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
187#[serde(untagged)]
188pub enum UsageResponse {
189 ApiKey(UsageApiKeyResponse),
191 OAuth(UsageOAuthResponse),
193}