ridewithgps_client/
users.rs

1//! User-related types and methods
2
3use crate::{Result, RideWithGpsClient};
4use serde::{Deserialize, Serialize};
5
6/// User information
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct User {
9    /// User ID
10    pub id: u64,
11
12    /// User name (full name or display name)
13    pub name: Option<String>,
14
15    /// First name
16    pub first_name: Option<String>,
17
18    /// Last name
19    pub last_name: Option<String>,
20
21    /// Display name
22    pub display_name: Option<String>,
23
24    /// User email
25    pub email: Option<String>,
26
27    /// User location
28    pub location: Option<String>,
29
30    /// User description/bio
31    pub description: Option<String>,
32
33    /// Avatar URL
34    pub avatar_url: Option<String>,
35
36    /// Whether the user has a premium account
37    pub premium: Option<bool>,
38
39    /// Account creation timestamp
40    pub created_at: Option<String>,
41
42    /// Account updated timestamp
43    pub updated_at: Option<String>,
44}
45
46impl RideWithGpsClient {
47    /// Get the current authenticated user's information
48    ///
49    /// Requires an auth token to be set.
50    ///
51    /// # Example
52    ///
53    /// ```rust,no_run
54    /// use ridewithgps_client::RideWithGpsClient;
55    ///
56    /// let client = RideWithGpsClient::new(
57    ///     "https://ridewithgps.com",
58    ///     "your-api-key",
59    ///     Some("your-auth-token")
60    /// );
61    ///
62    /// let user = client.get_current_user().unwrap();
63    /// println!("User: {:?}", user);
64    /// ```
65    pub fn get_current_user(&self) -> Result<User> {
66        #[derive(Deserialize)]
67        struct UserWrapper {
68            user: User,
69        }
70
71        let wrapper: UserWrapper = self.get("/api/v1/users/current.json")?;
72        Ok(wrapper.user)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_user_deserialization() {
82        let json = r#"{
83            "id": 123,
84            "name": "Test User",
85            "email": "test@example.com",
86            "premium": true
87        }"#;
88
89        let user: User = serde_json::from_str(json).unwrap();
90        assert_eq!(user.id, 123);
91        assert_eq!(user.name.as_deref(), Some("Test User"));
92        assert_eq!(user.email.as_deref(), Some("test@example.com"));
93        assert_eq!(user.premium, Some(true));
94    }
95
96    #[test]
97    fn test_user_wrapper_deserialization() {
98        let json = r#"{
99            "user": {
100                "id": 456,
101                "name": "Wrapped User",
102                "first_name": "Wrapped",
103                "last_name": "User",
104                "email": "wrapped@example.com"
105            }
106        }"#;
107
108        #[derive(Deserialize)]
109        struct UserWrapper {
110            user: User,
111        }
112
113        let wrapper: UserWrapper = serde_json::from_str(json).unwrap();
114        assert_eq!(wrapper.user.id, 456);
115        assert_eq!(wrapper.user.name.as_deref(), Some("Wrapped User"));
116        assert_eq!(wrapper.user.first_name.as_deref(), Some("Wrapped"));
117        assert_eq!(wrapper.user.last_name.as_deref(), Some("User"));
118    }
119
120    #[test]
121    fn test_user_with_all_fields() {
122        let json = r#"{
123            "id": 789,
124            "name": "Full User",
125            "first_name": "Full",
126            "last_name": "User",
127            "display_name": "fulluser",
128            "email": "full@example.com",
129            "location": "San Francisco, CA",
130            "description": "Avid cyclist",
131            "avatar_url": "https://example.com/avatar.jpg",
132            "premium": true,
133            "created_at": "2020-01-01T00:00:00Z",
134            "updated_at": "2025-01-01T00:00:00Z"
135        }"#;
136
137        let user: User = serde_json::from_str(json).unwrap();
138        assert_eq!(user.id, 789);
139        assert_eq!(user.display_name.as_deref(), Some("fulluser"));
140        assert_eq!(user.location.as_deref(), Some("San Francisco, CA"));
141        assert_eq!(user.description.as_deref(), Some("Avid cyclist"));
142        assert_eq!(user.premium, Some(true));
143    }
144}