Add an error type to librespot_discovery

This commit is contained in:
johannesd3 2021-03-17 18:47:27 +01:00
parent 1ec5dd21fa
commit a7f9e0a20b
No known key found for this signature in database
GPG key ID: 8C2739E91D410F75
3 changed files with 35 additions and 18 deletions

1
Cargo.lock generated
View file

@ -1259,6 +1259,7 @@ dependencies = [
"serde_json", "serde_json",
"sha-1", "sha-1",
"simple_logger", "simple_logger",
"thiserror",
"tokio", "tokio",
] ]

View file

@ -20,6 +20,7 @@ log = "0.4"
rand = "0.8" rand = "0.8"
serde_json = "1.0.25" serde_json = "1.0.25"
sha-1 = "0.9" sha-1 = "0.9"
thiserror = "1.0"
tokio = { version = "1.0", features = ["sync", "rt"] } tokio = { version = "1.0", features = ["sync", "rt"] }
dns-sd = { version = "0.1.3", optional = true } dns-sd = { version = "0.1.3", optional = true }

View file

@ -3,6 +3,9 @@
//! This device will show up in the list of "available devices". //! This device will show up in the list of "available devices".
//! Once it is selected from the list, [`Credentials`] are received. //! Once it is selected from the list, [`Credentials`] are received.
//! Those can be used to establish a new Session with [`librespot_core`]. //! Those can be used to establish a new Session with [`librespot_core`].
//!
//! This library uses mDNS and DNS-SD so that other devices can find it,
//! and spawns an http server to answer requests of Spotify clients.
#![warn(clippy::all, missing_docs, rust_2018_idioms)] #![warn(clippy::all, missing_docs, rust_2018_idioms)]
@ -15,6 +18,7 @@ use std::task::{Context, Poll};
use cfg_if::cfg_if; use cfg_if::cfg_if;
use futures_core::Stream; use futures_core::Stream;
use librespot_core as core; use librespot_core as core;
use thiserror::Error;
use self::server::DiscoveryServer; use self::server::DiscoveryServer;
@ -43,6 +47,17 @@ pub struct Builder {
port: u16, port: u16,
} }
/// Errors that can occur while setting up a [`Discovery`] instance.
#[derive(Debug, Error)]
pub enum Error {
/// Setting up service discovery via DNS-SD failed.
#[error("Setting up dns-sd failed: {0}")]
DnsSdError(#[from] io::Error),
/// Setting up the http server failed.
#[error("Setting up the http server failed: {0}")]
HttpServerError(#[from] hyper::Error),
}
impl Builder { impl Builder {
/// Starts a new builder using the provided device id. /// Starts a new builder using the provided device id.
pub fn new(device_id: String) -> Self { pub fn new(device_id: String) -> Self {
@ -79,22 +94,10 @@ impl Builder {
/// ///
/// # Errors /// # Errors
/// If setting up the mdns service or creating the server fails, this function returns an error. /// If setting up the mdns service or creating the server fails, this function returns an error.
pub fn launch(self) -> io::Result<Discovery> { pub fn launch(self) -> Result<Discovery, Error> {
Discovery::new(self) let mut port = self.port;
} let name = self.server_config.name.clone().into_owned();
} let server = DiscoveryServer::new(self.server_config, &mut port)?;
impl Discovery {
/// Starts a [`Builder`] with the provided device id.
pub fn builder(device_id: String) -> Builder {
Builder::new(device_id)
}
fn new(builder: Builder) -> io::Result<Self> {
let name = builder.server_config.name.clone();
let mut port = builder.port;
let server = DiscoveryServer::new(builder.server_config, &mut port)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let svc; let svc;
@ -114,14 +117,26 @@ impl Discovery {
let responder = libmdns::Responder::spawn(&tokio::runtime::Handle::current())?; let responder = libmdns::Responder::spawn(&tokio::runtime::Handle::current())?;
svc = responder.register( svc = responder.register(
"_spotify-connect._tcp".to_owned(), "_spotify-connect._tcp".to_owned(),
name.into_owned(), name,
port, port,
&["VERSION=1.0", "CPath=/"], &["VERSION=1.0", "CPath=/"],
) )
} }
}; };
Ok(Self { server, _svc: svc }) Ok(Discovery { server, _svc: svc })
}
}
impl Discovery {
/// Starts a [`Builder`] with the provided device id.
pub fn builder(device_id: String) -> Builder {
Builder::new(device_id)
}
/// Create a new instance with the specified device id and default paramaters.
pub fn new(device_id: String) -> Result<Self, Error> {
Self::builder(device_id).launch()
} }
} }