sonic_channel/commands/
start.rs

1use super::StreamCommand;
2use crate::channels::ChannelMode;
3use crate::protocol;
4use crate::result::*;
5
6#[derive(Debug)]
7pub struct StartCommand {
8    pub(crate) mode: ChannelMode,
9    pub(crate) password: String,
10}
11
12#[derive(Debug)]
13pub struct StartCommandResponse {
14    pub protocol_version: protocol::Version,
15    pub max_buffer_size: usize,
16    pub mode: ChannelMode,
17}
18
19impl StreamCommand for StartCommand {
20    type Response = StartCommandResponse;
21
22    fn request(&self) -> protocol::Request {
23        protocol::Request::Start {
24            mode: self.mode,
25            password: self.password.to_string(),
26        }
27    }
28
29    fn receive(&self, res: protocol::Response) -> Result<Self::Response> {
30        if let protocol::Response::Started(payload) = res {
31            Ok(StartCommandResponse {
32                protocol_version: payload
33                    .protocol_version
34                    .try_into()
35                    // TODO: better error
36                    .map_err(|_| Error::SwitchMode)?,
37                max_buffer_size: payload.max_buffer_size,
38                mode: self.mode,
39            })
40        } else {
41            Err(Error::SwitchMode)
42        }
43    }
44}