qobuz_api_rust/models/
core.rs

1use {
2    serde::{Deserialize, Deserializer, Serialize, de::Error},
3    serde_json::Value::{self, Null},
4};
5
6use crate::models::User;
7
8/// Custom deserializer for the 'code' field that handles both string and numeric values
9///
10/// This function allows deserializing the 'code' field from either a string or numeric value,
11/// converting numeric values to strings. This is useful when the API may return codes
12/// in different formats.
13///
14/// # Arguments
15///
16/// * `deserializer` - The deserializer to use for reading the value
17///
18/// # Returns
19///
20/// A `Result` containing either an `Option<String>` with the code value or a deserialization error
21pub fn deserialize_code<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
22where
23    D: Deserializer<'de>,
24{
25    let value = Value::deserialize(deserializer)?;
26
27    match value {
28        Value::String(s) => Ok(Some(s)),
29
30        Value::Number(n) => Ok(Some(n.to_string())),
31
32        Null => Ok(None),
33
34        _ => Err(Error::custom("Invalid code format")),
35    }
36}
37
38/// A general Qobuz API status response model
39///
40/// This struct represents the standard response format for Qobuz API calls that return
41/// status information, including an optional code, message, and status string.
42///
43/// # Examples
44///
45/// ```
46/// use qobuz_api_rust::models::QobuzApiStatusResponse;
47///
48/// let response = QobuzApiStatusResponse::new(
49///     Some("success".to_string()),
50///     Some("Operation completed successfully".to_string()),
51///     Some("ok".to_string())
52/// );
53/// ```
54#[derive(Serialize, Deserialize, Debug, Clone, Default)]
55pub struct QobuzApiStatusResponse {
56    /// The status code of the API response, which can be a string or numeric value
57    #[serde(rename = "code", deserialize_with = "deserialize_code")]
58    pub code: Option<String>,
59
60    /// A human-readable message describing the status of the response
61    #[serde(rename = "message")]
62    pub message: Option<String>,
63
64    /// The status string indicating the result of the API call
65    #[serde(rename = "status")]
66    pub status: Option<String>,
67}
68
69impl QobuzApiStatusResponse {
70    /// Creates a new instance of `QobuzApiStatusResponse`
71    ///
72    /// # Arguments
73    ///
74    /// * `code` - Optional status code for the response
75    /// * `message` - Optional human-readable message
76    /// * `status` - Optional status string
77    ///
78    /// # Returns
79    ///
80    /// A new instance of `QobuzApiStatusResponse` with the provided values
81    pub fn new(code: Option<String>, message: Option<String>, status: Option<String>) -> Self {
82        QobuzApiStatusResponse {
83            code,
84            message,
85            status,
86        }
87    }
88}
89
90/// Login response model containing user and authentication token
91///
92/// This struct represents the response from a login operation, containing the
93/// user information and authentication token.
94///
95/// # Examples
96///
97/// ```
98/// use qobuz_api_rust::models::{Login, User};
99///
100/// let login_response = Login {
101///     user: Some(User::default()),
102///     auth_token: Some("auth_token_123".to_string()),
103/// };
104/// ```
105#[derive(Serialize, Deserialize, Debug, Clone)]
106pub struct Login {
107    /// User information returned with the login response
108    #[serde(rename = "user")]
109    pub user: Option<User>,
110
111    /// Authentication token for the logged-in user
112    #[serde(rename = "user_auth_token")]
113    pub auth_token: Option<String>,
114}