@@ -8,53 +8,65 @@ use postcodes::*;
88use response:: * ;
99
1010extern crate clap;
11- use clap:: { Arg , App , crate_version} ;
11+ use clap:: { ArgAction , crate_version, Parser } ;
12+
13+ #[ derive( Parser ) ]
14+ #[ command(
15+ name = "rohe" ,
16+ author = "Jake·W.·Ireland.·<jakewilliami@icloud.com>" ,
17+ version = crate_version!( ) ,
18+ about = "A command line interface for NZP's locator API." ,
19+ long_about = "A command line interface for NZP's locator API. The name 'rohe' is the Māori word for 'areas'." ,
20+ ) ]
21+ struct Cli {
22+ /// Takes address as input. Default return value is the address' associated postcode
23+ #[ arg(
24+ short = 'a' ,
25+ long = "address" ,
26+ action = ArgAction :: Set ,
27+ num_args = 0 ..=1 ,
28+ value_name = "address" ,
29+ ) ]
30+ addr : Option < String > ,
31+
32+ /// Takes postcode as input. Default return value is the postcode's associated region
33+ #[ arg(
34+ short = 'p' ,
35+ long = "postcode" ,
36+ action = ArgAction :: Set ,
37+ num_args = 0 ..=1 ,
38+ value_name = "postcode"
39+ ) ]
40+ postcode : Option < String > ,
41+
42+ /// Return address as (latitude, longitude)
43+ #[ arg(
44+ short = 'c' ,
45+ long = "coordinates" ,
46+ action = ArgAction :: Set ,
47+ num_args = 0 ..=1 ,
48+ value_name = "address" ,
49+ ) ]
50+ coords : Option < String > ,
51+ }
1252
1353#[ tokio:: main]
1454async fn main ( ) {
15- let matches = App :: new ( "rohe" )
16- . version ( crate_version ! ( ) )
17- . author ( "Jake W. Ireland. <jakewilliami@icloud.com>" )
18- . about ( "A command line interface for NZP's locator API. The name 'rohe' is the Māori word for 'areas'." )
19- . arg ( Arg :: with_name ( "ADDR" )
20- . short ( "a" )
21- . long ( "address" )
22- . help ( "Takes address as input. Default return value is the address' associated postcode." )
23- . takes_value ( true )
24- . required ( false )
25- . multiple ( false )
26- )
27- . arg ( Arg :: with_name ( "POSTCODE" )
28- . short ( "p" )
29- . long ( "postcode" )
30- . help ( "Takes postcode as input. Default return value is the postcode's associated region." )
31- . takes_value ( true )
32- . required ( false )
33- . multiple ( false )
34- )
35- . arg ( Arg :: with_name ( "ADDR_FOR_COORDS" )
36- . short ( "c" )
37- . long ( "coordinates" )
38- . help ( "Return addresses as (latitude, longitude)." )
39- . takes_value ( true )
40- . required ( false )
41- . multiple ( false )
42- )
43- . get_matches ( ) ;
55+ let cli = Cli :: parse ( ) ;
4456
4557 // Find postcode information
46- if matches . is_present ( "POSTCODE" ) {
58+ if let Some ( postcode_str ) = cli . postcode {
4759 let bad_response: & str = "There was no postcode in the database that matched your input." ;
4860
4961 // get value of postcode
50- let postcode = matches . value_of ( "POSTCODE" ) . unwrap ( ) . parse_postcode ( ) ;
62+ let postcode = postcode_str . parse_postcode ( ) ;
5163
5264 // request postcodes from the API
5365 let matched_postcodes: Option < Vec < EachPostcode > > = request:: get_suggested_postcodes ( postcode) . await ;
5466
5567 // initialise the response string
5668 let mut resp = String :: new ( ) ;
57- if matched_postcodes. as_ref ( ) . is_none ( ) || matched_postcodes. as_ref ( ) . unwrap ( ) . len ( ) == 0 {
69+ if matched_postcodes. as_ref ( ) . is_none ( ) || matched_postcodes. as_ref ( ) . unwrap ( ) . is_empty ( ) {
5870 resp. push_str ( bad_response) ;
5971 } else {
6072 let postcodes = & matched_postcodes. unwrap ( ) ;
@@ -69,15 +81,15 @@ async fn main() {
6981 let details: Option < serde_json:: Map < String , serde_json:: Value > > = request:: get_postcode_details ( * unique_id) . await ;
7082
7183 // construct the response string
72- if details. is_none ( ) {
73- resp. push_str ( bad_response) ;
74- } else {
84+ if let Some ( details) = details {
7585 resp. push_str ( full_partial) ;
7686 resp. push_str ( " ∈ " ) ;
77- resp. push_str ( details. unwrap ( ) [ "CityTown" ] . as_str ( ) . unwrap ( ) ) ;
87+ resp. push_str ( details[ "CityTown" ] . as_str ( ) . unwrap ( ) ) ;
7888 if i != ( postcodes. len ( ) - 1 ) {
79- resp. push_str ( " \n " )
89+ resp. push ( '\n' ) ;
8090 }
91+ } else {
92+ resp. push_str ( bad_response) ;
8193 }
8294 }
8395 }
@@ -86,10 +98,14 @@ async fn main() {
8698 }
8799
88100 // Find address information
89- if matches . is_present ( "ADDR" ) {
101+ if let Some ( addr ) = cli . addr {
90102 // get value of address
91- let addr = matches. value_of ( "ADDR" ) . unwrap ( ) ;
92- let resp: Option < Vec < EachAddress > > = request:: get_suggested_addresses ( addr. to_string ( ) ) . await ;
103+ let resp: Option < Vec < EachAddress > > = request:: get_suggested_addresses ( addr) . await ;
93104 println ! ( "{:?}" , resp) ;
94105 }
106+
107+ // Get address coordinated
108+ if let Some ( _addr_for_coords) = cli. coords {
109+ todo ! ( ) ;
110+ }
95111}
0 commit comments