snapcast_control/protocol/
mod.rs

1//! # protocol
2//!
3//! This module contains all code for communicating with the Snapcast server.
4//!
5//! The module is split into submodules for each type of request and response for server,
6//! as well as a `super` scoped `de` module for deserialization.
7//!
8//! Most users will only need to interact with the following modules:
9//! - [client] for interacting with clients
10//! - [group] for interacting with groups
11//! - [server] for interacting with the server
12//! - [stream] for interacting with streams
13//!
14//! The [errors] module contains all error types that can be returned from the server. This is
15//! reexported higher up in the crate.
16
17use serde::Serialize;
18
19/// module for interacting with client devices connected to the Snapcast server
20pub mod client;
21/// module for interacting with groups of clients
22pub mod group;
23/// module for interacting with the Snapcast server itself
24pub mod server;
25/// module for interacting with audio streams
26pub mod stream;
27
28/// module for all error types that can be returned from the server
29pub mod errors;
30
31mod de;
32mod notification;
33mod request;
34mod result;
35
36pub use de::DeserializationError;
37pub(super) use de::SentRequests;
38pub(super) use request::{Request, RequestMethod};
39
40pub use notification::Notification;
41pub use request::Method;
42pub use result::SnapcastResult;
43
44/// A message received from the Snapcast server
45#[derive(Debug, Clone, Serialize, PartialEq)]
46#[serde(untagged)]
47pub enum Message {
48  /// A message that is in response to a request
49  Result {
50    /// The id of the request
51    id: uuid::Uuid,
52    /// The jsonrpc version (2.0)
53    jsonrpc: String,
54    /// The result of the request
55    result: Box<SnapcastResult>,
56  },
57  /// An error from the server
58  Error {
59    /// The id of the request
60    id: uuid::Uuid,
61    /// The jsonrpc version (2.0)
62    jsonrpc: String,
63    /// The error
64    error: errors::SnapcastError,
65  },
66  /// A notification from the server
67  Notification {
68    /// The jsonrpc version (2.0)
69    jsonrpc: String,
70    /// The notification data itself as a tagged enum
71    #[serde(flatten)]
72    method: Box<Notification>,
73  },
74}
75
76/// A message received from the Snapcast server that is not an error
77#[derive(Debug, Clone, Serialize, PartialEq)]
78#[serde(untagged)]
79pub enum ValidMessage {
80  /// A message that is in response to a request
81  Result {
82    /// The id of the request
83    id: uuid::Uuid,
84    /// The jsonrpc version (2.0)
85    jsonrpc: String,
86    /// The result of the request
87    result: Box<SnapcastResult>,
88  },
89  /// A notification from the server
90  Notification {
91    /// The jsonrpc version (2.0)
92    jsonrpc: String,
93    /// The notification data itself as a tagged enum
94    #[serde(flatten)]
95    method: Box<Notification>,
96  },
97}
98
99impl TryFrom<Message> for ValidMessage {
100  type Error = errors::SnapcastError;
101
102  fn try_from(value: Message) -> Result<Self, Self::Error> {
103    match value {
104      Message::Result { id, jsonrpc, result } => Ok(ValidMessage::Result { id, jsonrpc, result }),
105      Message::Error { error, .. } => Err(error),
106      Message::Notification { jsonrpc, method } => Ok(ValidMessage::Notification { jsonrpc, method }),
107    }
108  }
109}