sure_client_rs/models/
usage.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4/// Rate limit tier
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum RateLimitTier {
8    /// Standard tier
9    Standard,
10    /// Premium tier
11    Premium,
12    /// Enterprise tier
13    Enterprise,
14    /// No operation (testing/development)
15    Noop,
16    /// Unknown tier
17    #[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/// Error returned when parsing a `RateLimitTier` from a string fails.
34#[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/// API key information
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
78pub struct ApiKeyInfo {
79    /// API key name
80    pub name: String,
81    /// API key scopes
82    pub scopes: Vec<String>,
83    /// Last used timestamp
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub last_used_at: Option<DateTime<Utc>>,
86    /// Creation timestamp
87    pub created_at: DateTime<Utc>,
88}
89
90/// Rate limit information
91#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
92#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
93pub struct RateLimitInfo {
94    /// Rate limit tier
95    pub tier: RateLimitTier,
96    /// Rate limit
97    #[serde(default, skip_serializing_if = "Option::is_none")]
98    pub limit: Option<i64>,
99    /// Current count
100    pub current_count: i64,
101    /// Remaining requests
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub remaining: Option<i64>,
104    /// Reset time in seconds
105    pub reset_in_seconds: i64,
106    /// Reset timestamp
107    pub reset_at: DateTime<Utc>,
108}
109
110/// Usage response for API key authentication
111#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
112#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
113pub struct UsageApiKeyResponse {
114    /// API key information
115    pub api_key: ApiKeyInfo,
116    /// Rate limit information
117    pub rate_limit: RateLimitInfo,
118}
119
120/// Authentication method
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
122#[serde(rename_all = "lowercase")]
123pub enum AuthenticationMethod {
124    /// OAuth authentication
125    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/// Error returned when parsing an `AuthenticationMethod` from a string fails.
137#[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/// Usage response for OAuth authentication
176#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
177#[cfg_attr(feature = "strict", serde(deny_unknown_fields))]
178pub struct UsageOAuthResponse {
179    /// Authentication method
180    pub authentication_method: AuthenticationMethod,
181    /// Response message
182    pub message: String,
183}
184
185/// Usage response (can be either API key or OAuth)
186#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
187#[serde(untagged)]
188pub enum UsageResponse {
189    /// API key authentication response
190    ApiKey(UsageApiKeyResponse),
191    /// OAuth authentication response
192    OAuth(UsageOAuthResponse),
193}