freedom_api/api/post/
user.rs

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
use reqwest::Response;
use serde::Serialize;

use crate::{api::Api, error::Error};

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
    #[serde(skip_serializing)]
    account_id: i32,
    first_name: String,
    last_name: String,
    email: String,
    machine_service: bool,
    roles: Vec<String>,
}

pub struct UserBuilder<'a, C, S> {
    client: &'a C,
    state: S,
}

pub fn new<C>(client: &C) -> UserBuilder<'_, C, NoAccount> {
    UserBuilder {
        client,
        state: NoAccount,
    }
}

pub struct NoAccount;

impl<'a, C> UserBuilder<'a, C, NoAccount> {
    pub fn account_id(self, account_id: impl Into<i32>) -> UserBuilder<'a, C, NoFirstName> {
        UserBuilder {
            client: self.client,
            state: NoFirstName {
                account_id: account_id.into(),
            },
        }
    }
}

pub struct NoFirstName {
    account_id: i32,
}

impl<'a, C> UserBuilder<'a, C, NoFirstName> {
    pub fn first_name(self, first_name: impl Into<String>) -> UserBuilder<'a, C, NoLastName> {
        UserBuilder {
            client: self.client,
            state: NoLastName {
                account_id: self.state.account_id,
                first_name: first_name.into(),
            },
        }
    }
}

pub struct NoLastName {
    account_id: i32,
    first_name: String,
}

impl<'a, C> UserBuilder<'a, C, NoLastName> {
    pub fn last_name(self, last_name: impl Into<String>) -> UserBuilder<'a, C, NoEmail> {
        UserBuilder {
            client: self.client,
            state: NoEmail {
                account_id: self.state.account_id,
                first_name: self.state.first_name,
                last_name: last_name.into(),
            },
        }
    }
}

pub struct NoEmail {
    account_id: i32,
    first_name: String,
    last_name: String,
}

impl<'a, C> UserBuilder<'a, C, NoEmail> {
    pub fn email(self, email: impl Into<String>) -> UserBuilder<'a, C, User> {
        let state = User {
            account_id: self.state.account_id,
            first_name: self.state.first_name,
            last_name: self.state.last_name,
            email: email.into(),
            machine_service: false,
            roles: Vec::new(),
        };

        UserBuilder {
            client: self.client,
            state,
        }
    }
}

impl<'a, C> UserBuilder<'a, C, User> {
    pub fn add_role(mut self, role: impl Into<String>) -> Self {
        self.state.roles.push(role.into());

        self
    }

    pub fn add_roles<I, T>(mut self, roles: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        for role in roles {
            self = self.add_role(role);
        }

        self
    }
}

impl<'a, C> UserBuilder<'a, C, User>
where
    C: Api,
{
    pub async fn send(self) -> Result<Response, Error> {
        let client = self.client;

        let url = client.path_to_url(format!("accounts/{}/newuser", self.state.account_id));
        client.post(url, self.state).await
    }
}