1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
mod vk_date_format;
mod vk_date_format_opt;

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
pub use vk_method::{Method, Params};
use async_trait::async_trait;

pub type UserId = u32;

#[async_trait]
pub trait API {
    type Error: std::error::Error;

    async fn method<T>(&self, method: Method) -> Result<T, Self::Error>
    where for<'de>
        T: serde::Deserialize<'de>;

    fn users_get(&self) -> UsersGetBuilder<Self> where Self: Sized {
        UsersGetBuilder::new(self)
    }

    fn friends_get(&self) -> FriendsGetBuilder<Self> where Self: Sized {
        FriendsGetBuilder::new(self)
    }
}

pub struct UsersGetBuilder<'a, A: API> {
    pub user_ids: Option<Vec<String>>,
    pub user_id: Option<String>,
    pub fields: Option<Vec<UsersFields>>,
    api: &'a A
}

impl<'a, A: API> UsersGetBuilder<'a, A> {
    fn new(api: &'a A) -> Self {
        UsersGetBuilder {
            user_ids: None,
            user_id: None,
            fields: None,
            api
        }
    }

    pub fn user_id(mut self, id: impl ToString) -> Self {
        self.user_id = Some(id.to_string());
        self
    }

    pub fn user_ids(mut self, mut ids: Vec<impl ToString>) -> Self {
        let mut ids = ids.iter_mut().map(|id| id.to_string()).collect();

        match self.user_ids {
            Some(ref mut users) => {
                users.append(&mut ids)
            },
            None => {
                self.user_ids = Some(ids);
            }
        }
        self
    }
    
    pub fn fields(mut self, fields: Vec<UsersFields>) -> Self {
        self.fields = Some(fields);
        self
    }

    pub async fn send(self) -> Result<Vec<User>, A::Error> {
        let mut params = Params::new();

        if let Some(value) = self.user_id {
            params.insert("user_id", value);
        }

        if let Some(value) = self.user_ids {
            params.insert("user_id", value);
        }

        if let Some(value) = self.fields {
            params.insert("fields", value.into_iter().map(|field| field.to_string()).collect::<Vec<String>>());
        }
        
        println!("{:?}", params);

        self.api.method(
            Method::new("users.get", params)
        ).await
    }
}

pub struct FriendsGetBuilder<'a, A: API> {
    pub user_id: Option<UserId>,
    pub count: Option<u16>,
    api: &'a A
}

impl<'a, A: API> FriendsGetBuilder<'a, A> {
    fn new(api: &'a A) -> Self {
        FriendsGetBuilder {
            user_id: None,
            count: None,
            api
        }
    }

    pub fn user_id(mut self, id: UserId) -> Self {
        self.user_id = Some(id);
        self
    }

    pub fn count(mut self, count: u16) -> Self {
        self.count = Some(count);
        self
    }

    pub async fn send(self) -> Result<FriendsGetResponse, A::Error> {
        let mut params = Params::new();

        if let Some(value) = self.user_id {
            params.insert("user_id", value);
        }

        if let Some(value) = self.count {
            params.insert("count", value);
        }

        self.api.method(
            Method::new("friends.get", params)
        ).await
    }
}

#[derive(Serialize, Deserialize)]
pub struct FriendsGetResponse {
    pub count: u16,
    pub items: Vec<UserId>
}

#[allow(dead_code)]
#[derive(Deserialize, Debug, PartialEq)]
pub struct User {
    pub id: UserId,
    pub first_name: String,
    #[serde(default)]
    #[serde(with = "vk_date_format_opt")]
    pub bdate: Option<NaiveDate>
}

#[derive(strum::Display)]
#[derive(Serialize)]
pub enum UsersFields {
    bdate
}

#[cfg(test)]
mod tests;