Improve api of discovery crate's builder

This commit is contained in:
johannesd3 2021-04-02 16:15:37 +02:00
parent a7f9e0a20b
commit 16de6a7f68
No known key found for this signature in database
GPG key ID: 8C2739E91D410F75
2 changed files with 7 additions and 6 deletions

View file

@ -10,7 +10,7 @@ async fn main() {
.init() .init()
.unwrap(); .unwrap();
let name = "Librespot".to_string(); let name = "Librespot";
let device_id = hex::encode(Sha1::digest(name.as_bytes())); let device_id = hex::encode(Sha1::digest(name.as_bytes()));
let mut server = librespot_discovery::Discovery::builder(device_id) let mut server = librespot_discovery::Discovery::builder(device_id)

View file

@ -11,6 +11,7 @@
mod server; mod server;
use std::borrow::Cow;
use std::io; use std::io;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
@ -60,19 +61,19 @@ pub enum 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: impl Into<String>) -> Self {
Self { Self {
server_config: server::Config { server_config: server::Config {
name: "Librespot".into(), name: "Librespot".into(),
device_type: DeviceType::default(), device_type: DeviceType::default(),
device_id, device_id: device_id.into(),
}, },
port: 0, port: 0,
} }
} }
/// Sets the name to be displayed. Default is `"Librespot"`. /// Sets the name to be displayed. Default is `"Librespot"`.
pub fn name(mut self, name: String) -> Self { pub fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
self.server_config.name = name.into(); self.server_config.name = name.into();
self self
} }
@ -130,12 +131,12 @@ impl Builder {
impl Discovery { impl Discovery {
/// Starts a [`Builder`] with the provided device id. /// Starts a [`Builder`] with the provided device id.
pub fn builder(device_id: String) -> Builder { pub fn builder(device_id: impl Into<String>) -> Builder {
Builder::new(device_id) Builder::new(device_id)
} }
/// Create a new instance with the specified device id and default paramaters. /// Create a new instance with the specified device id and default paramaters.
pub fn new(device_id: String) -> Result<Self, Error> { pub fn new(device_id: impl Into<String>) -> Result<Self, Error> {
Self::builder(device_id).launch() Self::builder(device_id).launch()
} }
} }