dust_devil_core/sandstorm/
auth_methods.rs

1use std::io::Error;
2
3use tokio::io::{AsyncRead, AsyncWrite};
4
5use crate::{
6    serialize::{ByteRead, ByteWrite, SmallReadList, SmallWriteList},
7    socks5::AuthMethod,
8};
9
10use super::SandstormCommandType;
11
12/// A Sandstorm list auth methods request.
13pub struct ListAuthMethodsRequest;
14
15/// A Sandstorm list auth methods response.
16pub struct ListAuthMethodsResponse(
17    /// The list of authentication methods returned by the server.
18    pub Vec<(AuthMethod, bool)>,
19);
20
21/// A borrowed version of [`ListAuthMethodsResponse`].
22pub struct ListAuthMethodsResponseRef<'a>(
23    /// The list of authentication methods returned by the server.
24    &'a [(AuthMethod, bool)],
25);
26
27impl ListAuthMethodsResponse {
28    pub fn as_ref(&self) -> ListAuthMethodsResponseRef {
29        ListAuthMethodsResponseRef(&self.0)
30    }
31}
32
33impl ByteRead for ListAuthMethodsRequest {
34    async fn read<R: AsyncRead + Unpin + ?Sized>(_reader: &mut R) -> Result<Self, Error> {
35        Ok(Self)
36    }
37}
38
39impl ByteWrite for ListAuthMethodsRequest {
40    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
41        SandstormCommandType::ListAuthMethods.write(writer).await
42    }
43}
44
45impl ByteRead for ListAuthMethodsResponse {
46    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
47        Ok(Self(<SmallReadList<(AuthMethod, bool)> as ByteRead>::read(reader).await?.0))
48    }
49}
50
51impl ByteWrite for ListAuthMethodsResponse {
52    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
53        self.as_ref().write(writer).await
54    }
55}
56
57impl<'a> ByteWrite for ListAuthMethodsResponseRef<'a> {
58    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
59        (SandstormCommandType::ListAuthMethods, SmallWriteList(self.0)).write(writer).await
60    }
61}
62
63/// A Sandstorm toggle auth method request.
64pub struct ToggleAuthMethodRequest(
65    /// The authentication method to alter.
66    pub AuthMethod,
67    /// The desired state for the authentication method (`true` = enabled, `false` = disabled).
68    pub bool,
69);
70
71// A Sandstorm toggle auth method response.
72pub struct ToggleAuthMethodResponse(
73    /// Whether the operation succeeded.
74    pub bool,
75);
76
77impl ByteRead for ToggleAuthMethodRequest {
78    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
79        Ok(Self(AuthMethod::read(reader).await?, bool::read(reader).await?))
80    }
81}
82
83impl ByteWrite for ToggleAuthMethodRequest {
84    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
85        (SandstormCommandType::ToggleAuthMethod, self.0, self.1).write(writer).await
86    }
87}
88
89impl ByteRead for ToggleAuthMethodResponse {
90    async fn read<R: AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
91        Ok(Self(bool::read(reader).await?))
92    }
93}
94
95impl ByteWrite for ToggleAuthMethodResponse {
96    async fn write<W: AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
97        (SandstormCommandType::ToggleAuthMethod, self.0).write(writer).await
98    }
99}