dust_devil_core/sandstorm/
users.rs

1use std::io::{Error, ErrorKind};
2
3use tokio::io::{AsyncRead, AsyncWrite};
4
5use crate::{
6    serialize::{ByteRead, ByteWrite, SmallReadString, SmallWriteString},
7    users::UserRole,
8};
9
10use super::{SandstormCommandType, U8ReprEnum};
11
12pub struct ListUsersRequest;
13pub struct ListUsersResponse(pub Vec<(String, UserRole)>);
14pub struct ListUsersResponseRef<'a>(pub &'a [(String, UserRole)]);
15
16impl ListUsersResponse {
17    pub fn as_ref(&self) -> ListUsersResponseRef {
18        ListUsersResponseRef(self.0.as_slice())
19    }
20}
21
22impl ByteRead for ListUsersRequest {
23    async fn read<R: AsyncRead + Unpin + ?Sized>(_reader: &mut R) -> Result<Self, Error> {
24        Ok(Self)
25    }
26}
27
28impl ByteWrite for ListUsersRequest {
29    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
30        SandstormCommandType::ListUsers.write(writer).await
31    }
32}
33
34impl ByteRead for ListUsersResponse {
35    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
36        Ok(Self(<Vec<(String, UserRole)> as ByteRead>::read(reader).await?))
37    }
38}
39
40impl ByteWrite for ListUsersResponse {
41    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
42        self.as_ref().write(writer).await
43    }
44}
45
46impl<'a> ByteWrite for ListUsersResponseRef<'a> {
47    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
48        (SandstormCommandType::ListUsers, self.0).write(writer).await
49    }
50}
51
52pub struct AddUserRequest(pub String, pub String, pub UserRole);
53pub struct AddUserRequestRef<'a>(pub &'a str, pub &'a str, pub UserRole);
54
55#[repr(u8)]
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum AddUserResponse {
58    Ok = 0x00,
59    AlreadyExists = 0x01,
60    InvalidValues = 0x02,
61}
62
63impl AddUserRequest {
64    pub fn as_ref(&self) -> AddUserRequestRef {
65        AddUserRequestRef(&self.0, &self.1, self.2)
66    }
67}
68
69impl U8ReprEnum for AddUserResponse {
70    fn from_u8(value: u8) -> Option<Self> {
71        match value {
72            0x00 => Some(AddUserResponse::Ok),
73            0x01 => Some(AddUserResponse::AlreadyExists),
74            0x02 => Some(AddUserResponse::InvalidValues),
75            _ => None,
76        }
77    }
78
79    fn into_u8(self) -> u8 {
80        self as u8
81    }
82}
83
84impl ByteRead for AddUserRequest {
85    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
86        Ok(Self(
87            SmallReadString::read(reader).await?.0,
88            SmallReadString::read(reader).await?.0,
89            UserRole::read(reader).await?,
90        ))
91    }
92}
93
94impl ByteWrite for AddUserRequest {
95    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
96        self.as_ref().write(writer).await
97    }
98}
99
100impl<'a> ByteWrite for AddUserRequestRef<'a> {
101    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
102        let tuple = (
103            SandstormCommandType::AddUser,
104            SmallWriteString(self.0),
105            SmallWriteString(self.1),
106            self.2,
107        );
108        tuple.write(writer).await
109    }
110}
111
112impl ByteRead for AddUserResponse {
113    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
114        match Self::from_u8(u8::read(reader).await?) {
115            Some(value) => Ok(value),
116            None => Err(Error::new(ErrorKind::InvalidData, "Invalid AddUserResponse type byte")),
117        }
118    }
119}
120
121impl ByteWrite for AddUserResponse {
122    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
123        (SandstormCommandType::AddUser, self.into_u8()).write(writer).await
124    }
125}
126
127pub struct UpdateUserRequest(pub String, pub Option<String>, pub Option<UserRole>);
128pub struct UpdateUserRequestRef<'a>(pub &'a str, pub Option<&'a str>, pub Option<UserRole>);
129
130#[repr(u8)]
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum UpdateUserResponse {
133    Ok = 0x00,
134    UserNotFound = 0x01,
135    CannotDeleteOnlyAdmin = 0x02,
136    NothingWasRequested = 0x03,
137}
138
139impl UpdateUserRequest {
140    pub fn as_ref(&self) -> UpdateUserRequestRef {
141        UpdateUserRequestRef(&self.0, self.1.as_deref(), self.2)
142    }
143}
144
145impl U8ReprEnum for UpdateUserResponse {
146    fn from_u8(value: u8) -> Option<Self> {
147        match value {
148            0x00 => Some(Self::Ok),
149            0x01 => Some(Self::UserNotFound),
150            0x02 => Some(Self::CannotDeleteOnlyAdmin),
151            0x03 => Some(Self::NothingWasRequested),
152            _ => None,
153        }
154    }
155
156    fn into_u8(self) -> u8 {
157        self as u8
158    }
159}
160
161impl ByteRead for UpdateUserRequest {
162    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
163        Ok(Self(
164            SmallReadString::read(reader).await?.0,
165            <Option<SmallReadString> as ByteRead>::read(reader).await?.map(|s| s.0),
166            <Option<UserRole> as ByteRead>::read(reader).await?,
167        ))
168    }
169}
170
171impl ByteWrite for UpdateUserRequest {
172    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
173        let tuple = (
174            SandstormCommandType::UpdateUser,
175            SmallWriteString(&self.0),
176            self.1.as_ref().map(|s| SmallWriteString(s)),
177            self.2,
178        );
179        tuple.write(writer).await
180    }
181}
182
183impl<'a> ByteWrite for UpdateUserRequestRef<'a> {
184    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
185        let tuple = (
186            SandstormCommandType::UpdateUser,
187            SmallWriteString(self.0),
188            self.1.map(SmallWriteString),
189            self.2,
190        );
191        tuple.write(writer).await
192    }
193}
194
195impl ByteRead for UpdateUserResponse {
196    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
197        match Self::from_u8(u8::read(reader).await?) {
198            Some(value) => Ok(value),
199            None => Err(Error::new(ErrorKind::InvalidData, "Invalid UpdateUserResponse type byte")),
200        }
201    }
202}
203
204impl ByteWrite for UpdateUserResponse {
205    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
206        (SandstormCommandType::UpdateUser, self.into_u8()).write(writer).await
207    }
208}
209
210pub struct DeleteUserRequest(pub String);
211pub struct DeleteUserRequestRef<'a>(pub &'a str);
212
213#[repr(u8)]
214#[derive(Debug, Clone, Copy, PartialEq, Eq)]
215pub enum DeleteUserResponse {
216    Ok = 0x00,
217    UserNotFound = 0x01,
218    CannotDeleteOnlyAdmin = 0x02,
219}
220
221impl DeleteUserRequest {
222    pub fn as_ref(&self) -> DeleteUserRequestRef {
223        DeleteUserRequestRef(&self.0)
224    }
225}
226
227impl U8ReprEnum for DeleteUserResponse {
228    fn from_u8(value: u8) -> Option<Self> {
229        match value {
230            0x00 => Some(Self::Ok),
231            0x01 => Some(Self::UserNotFound),
232            0x02 => Some(Self::CannotDeleteOnlyAdmin),
233            _ => None,
234        }
235    }
236
237    fn into_u8(self) -> u8 {
238        self as u8
239    }
240}
241
242impl ByteRead for DeleteUserRequest {
243    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
244        Ok(DeleteUserRequest(SmallReadString::read(reader).await?.0))
245    }
246}
247
248impl ByteWrite for DeleteUserRequest {
249    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
250        self.as_ref().write(writer).await
251    }
252}
253
254impl<'a> ByteWrite for DeleteUserRequestRef<'a> {
255    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
256        (SandstormCommandType::DeleteUser, SmallWriteString(self.0)).write(writer).await
257    }
258}
259
260impl ByteRead for DeleteUserResponse {
261    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
262        match Self::from_u8(u8::read(reader).await?) {
263            Some(value) => Ok(value),
264            None => Err(Error::new(ErrorKind::InvalidData, "Invalid UpdateUserResponse type byte")),
265        }
266    }
267}
268
269impl ByteWrite for DeleteUserResponse {
270    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
271        (SandstormCommandType::DeleteUser, self.into_u8()).write(writer).await
272    }
273}