Crate snapcast_control

Source
Expand description

§snapcast-control

snapcast-control is a Rust api client for Snapcast. It supports all features of the Snapcast JSON-RPC API as of version 0.28.0 (2024/6/25).

Documentation is available at docs.rs.

§Features

  • native rust types for all api requests and responses
  • tokio-based async client
  • client with helper methods for all api requests
  • automatic socket reconnection via stubborn-io

§Installation

cargo add snapcast-control

§Usage

The best example of this crate’s usage is snapcast-multiroom, the project I designed it for.

A simple example of usage:

use snapcast_control::{SnapcastConnection, ValidMessage};

#[tokio::main]
async fn main() {
  let mut client = SnapcastConnection::open("127.0.0.1:1705".parse().expect("could not parse socket address")).await;

  // client state is updated with each message received
  let state = client.state.clone();

  // state is empty initially, sending the server_get_status request will populate it
  client.server_get_status().await.expect("could not send request");

  loop {
    tokio::select! {
      // as messages are received, they are stored in the client's recv buffer
      Some(message) = client.recv() => {
        if let Ok(response) = message {
          // handle response
          match response {
            ValidMessage::Result { id, jsonrpc, result  } => {},
            ValidMessage::Notification { method, jsonrpc } => {},
          }
        } else if let Err(err) = message {
          // handle error
        }
      },
      _ = tokio::signal::ctrl_c() => {
        break;
      }
    }
  }
}

Modules§

client
module for interacting with client devices connected to the Snapcast server
errors
module for all error types that can be returned from the server
group
module for interacting with groups of clients
server
module for interacting with the Snapcast server itself
stream
module for interacting with audio streams

Structs§

SnapcastConnection
Struct representing a connection to a Snapcast server. Contains the current state of the server and methods to interact with it.
State
The state of the Snapcast server, automatically kept up to date by the client

Enums§

ClientError
Error type for the Snapcast client
DeserializationError
Errors that can occur during deserialization
Message
A message received from the Snapcast server
Method
The method of a request that the client can call
Notification
A notification from the Snapcast server
SnapcastResult
The result of a Snapcast request
ValidMessage
A message received from the Snapcast server that is not an error