6
6
7
7
#![ warn( clippy:: all, missing_docs, rust_2018_idioms) ]
8
8
9
+ mod request;
10
+ mod response;
9
11
mod server;
10
12
13
+ mod util {
14
+ use hyper:: { Body , Response , StatusCode } ;
15
+
16
+ #[ inline]
17
+ pub fn new_response ( status : StatusCode , body : impl Into < Body > ) -> Response < Body > {
18
+ let mut response = Response :: new ( body. into ( ) ) ;
19
+ * response. status_mut ( ) = status;
20
+ response
21
+ }
22
+ }
23
+
11
24
use std:: io;
12
25
use std:: pin:: Pin ;
13
26
use std:: task:: { Context , Poll } ;
@@ -39,32 +52,32 @@ pub struct Discovery {
39
52
40
53
/// A builder for [`Discovery`].
41
54
pub struct Builder {
42
- server_config : server:: Config ,
55
+ name : String ,
56
+ device_id : String ,
57
+ device_type : DeviceType ,
43
58
port : u16 ,
44
59
}
45
60
46
61
impl Builder {
47
62
/// Starts a new builder using the provided device id.
48
63
pub fn new ( device_id : String ) -> Self {
49
64
Self {
50
- server_config : server:: Config {
51
- name : "Librespot" . into ( ) ,
52
- device_type : DeviceType :: default ( ) ,
53
- device_id,
54
- } ,
65
+ name : "Librespot" . into ( ) ,
66
+ device_type : DeviceType :: default ( ) ,
67
+ device_id,
55
68
port : 0 ,
56
69
}
57
70
}
58
71
59
72
/// Sets the name to be displayed. Default is `"Librespot"`.
60
73
pub fn name ( mut self , name : String ) -> Self {
61
- self . server_config . name = name. into ( ) ;
74
+ self . name = name;
62
75
self
63
76
}
64
77
65
78
/// Sets the device type which is visible as icon in other Spotify clients. Default is `Speaker`.
66
79
pub fn device_type ( mut self , device_type : DeviceType ) -> Self {
67
- self . server_config . device_type = device_type;
80
+ self . device_type = device_type;
68
81
self
69
82
}
70
83
@@ -80,28 +93,17 @@ impl Builder {
80
93
/// # Errors
81
94
/// If setting up the mdns service or creating the server fails, this function returns an error.
82
95
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)
96
+ let name = self . name . clone ( ) ;
97
+ let mut port = self . port ;
98
+ let server = DiscoveryServer :: new ( self . name , self . device_type , self . device_id , & mut port)
97
99
. map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e) ) ?;
98
100
99
101
let svc;
100
102
101
103
cfg_if ! {
102
104
if #[ cfg( feature = "with-dns-sd" ) ] {
103
105
svc = dns_sd:: DNSService :: register(
104
- Some ( name. as_ref ( ) ) ,
106
+ Some ( & name) ,
105
107
"_spotify-connect._tcp" ,
106
108
None ,
107
109
None ,
@@ -114,14 +116,26 @@ impl Discovery {
114
116
let responder = libmdns:: Responder :: spawn( & tokio:: runtime:: Handle :: current( ) ) ?;
115
117
svc = responder. register(
116
118
"_spotify-connect._tcp" . to_owned( ) ,
117
- name. into_owned ( ) ,
119
+ name,
118
120
port,
119
121
& [ "VERSION=1.0" , "CPath=/" ] ,
120
122
)
121
123
}
122
124
} ;
123
125
124
- Ok ( Self { server, _svc : svc } )
126
+ Ok ( Discovery { server, _svc : svc } )
127
+ }
128
+ }
129
+
130
+ impl Discovery {
131
+ /// Starts a [`Builder`] with the provided device id.
132
+ pub fn builder ( device_id : String ) -> Builder {
133
+ Builder :: new ( device_id)
134
+ }
135
+
136
+ /// Create a new instance with the specified device id and default paramaters.
137
+ pub fn new ( device_id : String ) -> io:: Result < Self > {
138
+ Self :: builder ( device_id) . launch ( )
125
139
}
126
140
}
127
141
0 commit comments