@@ -2,110 +2,109 @@ use std::error::Error;
22
33use crate :: { station:: Station , Config } ;
44use inquire:: { error:: InquireError , Text } ;
5- use radiobrowser:: { blocking:: RadioBrowserAPI , ApiCountry , StationOrder } ;
5+ use radiobrowser:: { blocking:: RadioBrowserAPI , ApiCountry , ApiStation , StationOrder } ;
66
7- pub fn get_countries ( ) -> Result < Vec < ApiCountry > , Box < dyn Error > > {
8- let api = match RadioBrowserAPI :: new ( ) {
9- Ok ( r) => r,
10- Err ( _e) => panic ! ( "Failed to create Radio Browser API!" ) ,
11- } ;
12-
13- api. get_countries ( ) . send ( )
7+ pub struct Browser {
8+ api : RadioBrowserAPI ,
9+ config : Config ,
10+ stations : Vec < ApiStation > ,
1411}
1512
16- pub fn get_station ( name : String , country_code : Option < String > ) -> Result < Station , InquireError > {
17- let api = match RadioBrowserAPI :: new ( ) {
18- Ok ( r) => r,
19- Err ( _e) => panic ! ( "Failed to create Radio Browser API!" ) ,
20- } ;
13+ impl Browser {
14+ pub fn new ( config : Config ) -> Result < Browser , Box < dyn Error > > {
15+ let api = match RadioBrowserAPI :: new ( ) {
16+ Ok ( r) => r,
17+ Err ( e) => return Err ( e) ,
18+ } ;
2119
22- if let Some ( code) = country_code {
23- return match api. get_stations ( ) . name ( name) . countrycode ( code) . send ( ) {
24- Ok ( s) => match s. get ( 0 ) {
25- Some ( x) => Ok ( Station {
26- station : x. name . clone ( ) ,
27- url : x. url . clone ( ) ,
28- } ) ,
29- None => Err ( InquireError :: InvalidConfiguration (
30- "Radio station does not exist" . to_string ( ) ,
31- ) ) ,
32- } ,
33- Err ( _e) => Err ( InquireError :: OperationInterrupted ) ,
20+ let stations = if let Some ( code) = & config. country_code {
21+ match api
22+ . get_stations ( )
23+ . countrycode ( code)
24+ . order ( StationOrder :: Clickcount )
25+ . send ( )
26+ {
27+ Ok ( s) => s,
28+ Err ( _e) => Vec :: new ( ) ,
29+ }
30+ } else {
31+ match api. get_stations ( ) . order ( StationOrder :: Clickcount ) . send ( ) {
32+ Ok ( s) => s,
33+ Err ( _e) => Vec :: new ( ) ,
34+ }
3435 } ;
35- } else {
36- return match api . get_stations ( ) . name ( name ) . send ( ) {
37- Ok ( s ) => match s . get ( 0 ) {
38- Some ( x ) => Ok ( Station {
39- station : x . name . clone ( ) ,
40- url : x . url . clone ( ) ,
41- } ) ,
42- None => Err ( InquireError :: InvalidConfiguration (
43- "Radio station does not exist" . to_string ( ) ,
44- ) ) ,
45- } ,
46- Err ( _e ) => Err ( InquireError :: OperationInterrupted ) ,
36+
37+ Ok ( Browser {
38+ api ,
39+ config ,
40+ stations ,
41+ } )
42+ }
43+
44+ pub fn get_countries ( ) -> Result < Vec < ApiCountry > , Box < dyn Error > > {
45+ let api = match RadioBrowserAPI :: new ( ) {
46+ Ok ( r ) => r ,
47+ Err ( e ) => return Err ( e ) ,
4748 } ;
49+
50+ api. get_countries ( ) . send ( )
4851 }
49- }
5052
51- fn search_station (
52- message : & str ,
53- placeholder : & str ,
54- country_code : Option < String > ,
55- ) -> Result < String , InquireError > {
56- let api = match RadioBrowserAPI :: new ( ) {
57- Ok ( r) => r,
58- Err ( _e) => panic ! ( "Failed to create Radio Browser API!" ) ,
59- } ;
53+ pub fn get_station ( & self , name : String ) -> Result < Station , InquireError > {
54+ if let Some ( code) = self . config . country_code . clone ( ) {
55+ return match self . api . get_stations ( ) . name ( name) . countrycode ( code) . send ( ) {
56+ Ok ( s) => match s. get ( 0 ) {
57+ Some ( x) => Ok ( Station {
58+ station : x. name . clone ( ) ,
59+ url : x. url . clone ( ) ,
60+ } ) ,
61+ None => Err ( InquireError :: InvalidConfiguration (
62+ "Radio station does not exist" . to_string ( ) ,
63+ ) ) ,
64+ } ,
65+ Err ( _e) => Err ( InquireError :: OperationInterrupted ) ,
66+ } ;
67+ } else {
68+ return match self . api . get_stations ( ) . name ( name) . send ( ) {
69+ Ok ( s) => match s. get ( 0 ) {
70+ Some ( x) => Ok ( Station {
71+ station : x. name . clone ( ) ,
72+ url : x. url . clone ( ) ,
73+ } ) ,
74+ None => Err ( InquireError :: InvalidConfiguration (
75+ "Radio station does not exist" . to_string ( ) ,
76+ ) ) ,
77+ } ,
78+ Err ( _e) => Err ( InquireError :: OperationInterrupted ) ,
79+ } ;
80+ }
81+ }
6082
61- Text :: new ( message)
62- . with_placeholder ( placeholder)
63- . with_suggester ( & |s : & str | {
64- if s. len ( ) > 3 {
65- if let Some ( code) = & country_code {
66- match api
67- . get_stations ( )
68- . countrycode ( code)
69- . name ( s)
70- . order ( StationOrder :: Clickcount )
71- . send ( )
72- {
73- Ok ( s) => s
74- . iter ( )
75- . map ( |station| String :: from ( & station. name ) )
76- . collect ( ) ,
77- Err ( _e) => Vec :: new ( ) ,
78- }
79- } else {
80- match api
81- . get_stations ( )
82- . name ( s)
83- . order ( StationOrder :: Clickcount )
84- . send ( )
85- {
86- Ok ( s) => s
87- . iter ( )
88- . map ( |station| String :: from ( & station. name ) )
89- . collect ( ) ,
90- Err ( _e) => Vec :: new ( ) ,
91- }
92- }
93- } else {
94- Vec :: new ( )
95- }
96- } )
97- . prompt ( )
98- }
83+ fn search_station ( & self , message : & str , placeholder : & str ) -> Result < String , InquireError > {
84+ let max_lines = match self . config . max_lines {
85+ Some ( x) => x,
86+ None => Text :: DEFAULT_PAGE_SIZE ,
87+ } ;
88+
89+ Text :: new ( message)
90+ . with_placeholder ( placeholder)
91+ . with_suggester ( & |s : & str | {
92+ self . stations
93+ . iter ( )
94+ . filter ( |station| station. name . contains ( s) )
95+ . map ( |station| String :: from ( & station. name ) )
96+ . collect ( )
97+ } )
98+ . with_page_size ( max_lines)
99+ . prompt ( )
100+ }
99101
100- pub fn prompt ( config : Config ) -> Result < Station , InquireError > {
101- let station = search_station (
102- "Search for a station: " ,
103- "Station name" ,
104- config. country_code . clone ( ) ,
105- ) ;
102+ pub fn prompt ( self ) -> Result < Station , InquireError > {
103+ let station = self . search_station ( "Search for a station: " , "Station name" ) ;
106104
107- match station {
108- Ok ( s) => get_station ( s, config. country_code . clone ( ) ) ,
109- Err ( e) => Err ( e) ,
105+ match station {
106+ Ok ( s) => self . get_station ( s. to_string ( ) ) ,
107+ Err ( e) => Err ( e) ,
108+ }
110109 }
111110}
0 commit comments