freedom_api/api/post/
user.rs1use reqwest::Response;
2use serde::Serialize;
3
4use crate::{api::Api, error::Error};
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct User {
9 #[serde(skip_serializing)]
10 account_id: i32,
11 first_name: String,
12 last_name: String,
13 email: String,
14 machine_service: bool,
15 roles: Vec<String>,
16}
17
18pub struct UserBuilder<'a, C, S> {
19 client: &'a C,
20 state: S,
21}
22
23pub fn new<C>(client: &C) -> UserBuilder<'_, C, NoAccount> {
24 UserBuilder {
25 client,
26 state: NoAccount,
27 }
28}
29
30pub struct NoAccount;
31
32impl<'a, C> UserBuilder<'a, C, NoAccount> {
33 pub fn account_id(self, account_id: impl Into<i32>) -> UserBuilder<'a, C, NoFirstName> {
34 UserBuilder {
35 client: self.client,
36 state: NoFirstName {
37 account_id: account_id.into(),
38 },
39 }
40 }
41}
42
43pub struct NoFirstName {
44 account_id: i32,
45}
46
47impl<'a, C> UserBuilder<'a, C, NoFirstName> {
48 pub fn first_name(self, first_name: impl Into<String>) -> UserBuilder<'a, C, NoLastName> {
49 UserBuilder {
50 client: self.client,
51 state: NoLastName {
52 account_id: self.state.account_id,
53 first_name: first_name.into(),
54 },
55 }
56 }
57}
58
59pub struct NoLastName {
60 account_id: i32,
61 first_name: String,
62}
63
64impl<'a, C> UserBuilder<'a, C, NoLastName> {
65 pub fn last_name(self, last_name: impl Into<String>) -> UserBuilder<'a, C, NoEmail> {
66 UserBuilder {
67 client: self.client,
68 state: NoEmail {
69 account_id: self.state.account_id,
70 first_name: self.state.first_name,
71 last_name: last_name.into(),
72 },
73 }
74 }
75}
76
77pub struct NoEmail {
78 account_id: i32,
79 first_name: String,
80 last_name: String,
81}
82
83impl<'a, C> UserBuilder<'a, C, NoEmail> {
84 pub fn email(self, email: impl Into<String>) -> UserBuilder<'a, C, User> {
85 let state = User {
86 account_id: self.state.account_id,
87 first_name: self.state.first_name,
88 last_name: self.state.last_name,
89 email: email.into(),
90 machine_service: false,
91 roles: Vec::new(),
92 };
93
94 UserBuilder {
95 client: self.client,
96 state,
97 }
98 }
99}
100
101impl<C> UserBuilder<'_, C, User> {
102 pub fn add_role(mut self, role: impl Into<String>) -> Self {
103 self.state.roles.push(role.into());
104
105 self
106 }
107
108 pub fn add_roles<I, T>(mut self, roles: I) -> Self
109 where
110 I: IntoIterator<Item = T>,
111 T: Into<String>,
112 {
113 for role in roles {
114 self = self.add_role(role);
115 }
116
117 self
118 }
119}
120
121impl<C> UserBuilder<'_, C, User>
122where
123 C: Api,
124{
125 pub async fn send(self) -> Result<Response, Error> {
126 let client = self.client;
127
128 let url = client.path_to_url(format!("accounts/{}/newuser", self.state.account_id));
129 client.post(url, self.state).await
130 }
131}