Skip to content

Commit a7f9e0a

Browse files
committed
Add an error type to librespot_discovery
1 parent 1ec5dd2 commit a7f9e0a

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

discovery/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ log = "0.4"
2020
rand = "0.8"
2121
serde_json = "1.0.25"
2222
sha-1 = "0.9"
23+
thiserror = "1.0"
2324
tokio = { version = "1.0", features = ["sync", "rt"] }
2425

2526
dns-sd = { version = "0.1.3", optional = true }

discovery/src/lib.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//! This device will show up in the list of "available devices".
44
//! Once it is selected from the list, [`Credentials`] are received.
55
//! Those can be used to establish a new Session with [`librespot_core`].
6+
//!
7+
//! This library uses mDNS and DNS-SD so that other devices can find it,
8+
//! and spawns an http server to answer requests of Spotify clients.
69
710
#![warn(clippy::all, missing_docs, rust_2018_idioms)]
811

@@ -15,6 +18,7 @@ use std::task::{Context, Poll};
1518
use cfg_if::cfg_if;
1619
use futures_core::Stream;
1720
use librespot_core as core;
21+
use thiserror::Error;
1822

1923
use self::server::DiscoveryServer;
2024

@@ -43,6 +47,17 @@ pub struct Builder {
4347
port: u16,
4448
}
4549

50+
/// Errors that can occur while setting up a [`Discovery`] instance.
51+
#[derive(Debug, Error)]
52+
pub enum Error {
53+
/// Setting up service discovery via DNS-SD failed.
54+
#[error("Setting up dns-sd failed: {0}")]
55+
DnsSdError(#[from] io::Error),
56+
/// Setting up the http server failed.
57+
#[error("Setting up the http server failed: {0}")]
58+
HttpServerError(#[from] hyper::Error),
59+
}
60+
4661
impl Builder {
4762
/// Starts a new builder using the provided device id.
4863
pub fn new(device_id: String) -> Self {
@@ -79,22 +94,10 @@ impl Builder {
7994
///
8095
/// # Errors
8196
/// If setting up the mdns service or creating the server fails, this function returns an error.
82-
pub fn launch(self) -> io::Result<Discovery> {
83-
Discovery::new(self)
84-
}
85-
}
86-
87-
impl Discovery {
88-
/// Starts a [`Builder`] with the provided device id.
89-
pub fn builder(device_id: String) -> Builder {
90-
Builder::new(device_id)
91-
}
92-
93-
fn new(builder: Builder) -> io::Result<Self> {
94-
let name = builder.server_config.name.clone();
95-
let mut port = builder.port;
96-
let server = DiscoveryServer::new(builder.server_config, &mut port)
97-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
97+
pub fn launch(self) -> Result<Discovery, Error> {
98+
let mut port = self.port;
99+
let name = self.server_config.name.clone().into_owned();
100+
let server = DiscoveryServer::new(self.server_config, &mut port)?;
98101

99102
let svc;
100103

@@ -114,14 +117,26 @@ impl Discovery {
114117
let responder = libmdns::Responder::spawn(&tokio::runtime::Handle::current())?;
115118
svc = responder.register(
116119
"_spotify-connect._tcp".to_owned(),
117-
name.into_owned(),
120+
name,
118121
port,
119122
&["VERSION=1.0", "CPath=/"],
120123
)
121124
}
122125
};
123126

124-
Ok(Self { server, _svc: svc })
127+
Ok(Discovery { server, _svc: svc })
128+
}
129+
}
130+
131+
impl Discovery {
132+
/// Starts a [`Builder`] with the provided device id.
133+
pub fn builder(device_id: String) -> Builder {
134+
Builder::new(device_id)
135+
}
136+
137+
/// Create a new instance with the specified device id and default paramaters.
138+
pub fn new(device_id: String) -> Result<Self, Error> {
139+
Self::builder(device_id).launch()
125140
}
126141
}
127142

0 commit comments

Comments
 (0)