mobot/api/
user.rs

1use std::collections::hash_map::DefaultHasher;
2
3use mobot_derive::BotRequest;
4use serde::{Deserialize, Serialize};
5
6use std::hash::{Hash, Hasher};
7
8use super::API;
9
10#[derive(Default, Debug, Clone, Deserialize, Serialize)]
11pub struct User {
12    /// Unique identifier for this user or bot
13    pub id: i64,
14
15    /// User‘s or bot’s first name
16    pub first_name: String,
17
18    /// User‘s or bot’s last name
19    pub last_name: Option<String>,
20
21    /// User‘s or bot’s username
22    pub username: Option<String>,
23
24    /// IETF language tag of the user's language
25    pub language_code: Option<String>,
26}
27
28fn hash<T: Hash>(t: &T) -> u64 {
29    let mut s = DefaultHasher::new();
30    t.hash(&mut s);
31    s.finish()
32}
33
34impl<T: Into<String>> From<T> for User {
35    fn from(s: T) -> Self {
36        let from = s.into();
37        Self {
38            id: hash(&from.clone()) as i64,
39            first_name: from.clone(),
40            last_name: None,
41            username: Some(from),
42            language_code: None,
43        }
44    }
45}
46
47#[derive(Debug, Clone, Serialize, BotRequest)]
48pub struct GetMeRequest {}
49
50impl API {
51    pub async fn get_me(&self) -> anyhow::Result<User> {
52        let req = GetMeRequest {};
53        self.client.post("getMe", &req).await
54    }
55}