@@ -69,11 +69,6 @@ pub enum SubAccounts {
6969}
7070
7171impl SubAccounts {
72- pub fn vec ( self ) -> Vec < Account > {
73- let SubAccounts :: SubAccounts ( prev) = self ;
74- prev. vec ( |SubAccounts :: SubAccounts ( next) | next)
75- }
76-
7772 pub fn hash_map ( self ) -> HashMap < String , Account > {
7873 let SubAccounts :: SubAccounts ( prev) = self ;
7974 prev. hash_map ( |SubAccounts :: SubAccounts ( next) | next)
@@ -109,21 +104,41 @@ where
109104 #[ arg( long) ]
110105 logs : Option < bool > ,
111106 #[ command( flatten) ]
112- main_account : Option < Account > ,
107+ main_account : Option < CliAccount > ,
113108 #[ command( subcommand) ]
114109 sub_accounts : Option < T > ,
115110}
116111
112+ #[ derive(
113+ Eq ,
114+ PartialEq ,
115+ Ord ,
116+ PartialOrd ,
117+ Debug ,
118+ Clone ,
119+ Serialize ,
120+ Deserialize ,
121+ Args ,
122+ ) ]
123+ pub struct CliAccount {
124+ #[ arg( long) ]
125+ alias : Option < String > ,
126+ #[ arg( long) ]
127+ balance : Option < i32 > ,
128+ #[ arg( long) ]
129+ tags : Option < Vec < String > > ,
130+ }
131+
117132impl Cli < IdClap < HashMap < String , Account > > > {
118133 pub fn def ( ) -> Self {
119134 Cli {
120135 toml : None ,
121136 host : Some ( "127.0.0.1" . into ( ) ) ,
122137 port : Some ( 8080 ) ,
123138 logs : Some ( false ) ,
124- main_account : Some ( Account {
125- alias : "foo ". into ( ) ,
126- balance : 42 ,
139+ main_account : Some ( CliAccount {
140+ alias : Some ( "Functora ". into ( ) ) ,
141+ balance : Some ( 42 ) ,
127142 tags : None ,
128143 } ) ,
129144 sub_accounts : Some ( IdClap ( HashMap :: new ( ) ) ) ,
@@ -134,25 +149,13 @@ impl Cli<IdClap<HashMap<String, Account>>> {
134149#[ test]
135150#[ serial]
136151fn defaults_only ( ) {
137- let cli = Cli :: parse_from ( [ "test" ] ) ;
138- let lhs = Cfg :: new ( cli) ;
139- // let lhs = Cfg::new(Cli {
140- // toml: None,
141- // host: None,
142- // port: None,
143- // logs: None,
144- // main_account: None,
145- // sub_accounts: SubAccounts::SubAccounts(ReClap {
146- // prev: None,
147- // next: None,
148- // }),
149- // });
152+ let lhs = Cfg :: new ( Cli :: parse_from ( [ "functora" ] ) ) ;
150153 let rhs = Cfg {
151154 host : "127.0.0.1" . into ( ) ,
152155 port : 8080 ,
153156 logs : false ,
154157 main_account : Account {
155- alias : "foo " . into ( ) ,
158+ alias : "Functora " . into ( ) ,
156159 balance : 42 ,
157160 tags : None ,
158161 } ,
@@ -163,127 +166,133 @@ fn defaults_only() {
163166
164167#[ test]
165168#[ serial]
166- fn with_file_override ( ) {
169+ fn file_override ( ) {
167170 let path = std:: env:: temp_dir ( ) . join ( "fun.toml" ) ;
168171 let file = r#"
169172 host = "192.168.1.100"
170- port = 9090
171173 logs = true
172- tags = ["a", "b"]
173174
174175 [sub_accounts.alice]
175- alias = "hello"
176- balance = 123
176+ alias = "Alice in Wonderland"
177+ balance = 101
178+ tags = ["retro", "story"]
177179 "# ;
178180 std:: fs:: write ( & path, file) . unwrap ( ) ;
179- let cli = Cli {
180- toml : Some ( path . to_string_lossy ( ) . into_owned ( ) ) ,
181- host : None ,
182- port : None ,
183- logs : None ,
184- main_account : None ,
185- sub_accounts : None ,
186- } ;
187- let cfg : Cfg = Cfg :: new ( cli ) ;
188- assert_eq ! ( cfg . host , "192.168.1.100" ) ;
189- assert_eq ! ( cfg . port , 9090 ) ;
190- assert_eq ! ( cfg . logs , true ) ;
191- assert_eq ! (
192- cfg . sub_accounts ,
193- HashMap :: from( [ (
181+ let lhs = Cfg :: new ( Cli :: parse_from ( [
182+ "functora" ,
183+ "--toml" ,
184+ & path . to_string_lossy ( ) . into_owned ( ) ,
185+ ] ) ) ;
186+ let rhs = Cfg {
187+ host : "192.168.1.100" . into ( ) ,
188+ port : 8080 ,
189+ logs : true ,
190+ main_account : Account {
191+ alias : "Functora" . into ( ) ,
192+ balance : 42 ,
193+ tags : None ,
194+ } ,
195+ sub_accounts : HashMap :: from ( [ (
194196 "alice" . into ( ) ,
195197 Account {
196- alias: "hello" . into( ) ,
197- balance: 123 ,
198- tags: None ,
199- }
200- ) ] )
201- ) ;
198+ alias : "Alice in Wonderland" . into ( ) ,
199+ balance : 101 ,
200+ tags : Some ( vec ! [
201+ "retro" . into( ) ,
202+ "story" . into( ) ,
203+ ] ) ,
204+ } ,
205+ ) ] ) ,
206+ } ;
207+ assert_eq ! ( lhs, rhs) ;
202208}
203209
204210#[ test]
205211#[ serial]
206212fn env_override ( ) {
207- let cli = Cli {
208- toml : None ,
209- host : None ,
210- port : None ,
211- logs : None ,
212- main_account : None ,
213- sub_accounts : None ,
214- } ;
215213 with_vars (
216214 vec ! [
217215 ( "FUNCTORA__HOST" , Some ( "10.0.0.1" ) ) ,
218216 ( "FUNCTORA__PORT" , Some ( "7070" ) ) ,
219217 ( "FUNCTORA__LOGS" , Some ( "true" ) ) ,
220- ( "FUNCTORA__MAIN_ACCOUNT__ALIAS" , Some ( "bar" ) ) ,
218+ (
219+ "FUNCTORA__MAIN_ACCOUNT__ALIAS" ,
220+ Some ( "Rich Functora" ) ,
221+ ) ,
221222 (
222223 "FUNCTORA__SUB_ACCOUNTS__BOB__ALIAS" ,
223- Some ( "buz " ) ,
224+ Some ( "Poor Bob " ) ,
224225 ) ,
225226 (
226227 "FUNCTORA__SUB_ACCOUNTS__BOB__BALANCE" ,
227228 Some ( "1" ) ,
228229 ) ,
229230 ] ,
230231 || {
231- let cfg : Cfg = Cfg :: new ( cli ) ;
232- assert_eq ! ( cfg . host , "10.0.0.1" ) ;
233- assert_eq ! ( cfg . port , 7070 ) ;
234- assert_eq ! ( cfg . logs , true ) ;
235- assert_eq ! (
236- cfg . main_account ,
237- Account {
238- alias: "bar " . into( ) ,
232+ let lhs =
233+ Cfg :: new ( Cli :: parse_from ( [ "functora" ] ) ) ;
234+ let rhs = Cfg {
235+ host : "10.0.0.1" . into ( ) ,
236+ port : 7070 ,
237+ logs : true ,
238+ main_account : Account {
239+ alias : "Rich Functora " . into ( ) ,
239240 balance : 42 ,
240241 tags : None ,
241- }
242- ) ;
243- assert_eq ! (
244- cfg. sub_accounts,
245- HashMap :: from( [ (
242+ } ,
243+ sub_accounts : HashMap :: from ( [ (
246244 "bob" . into ( ) ,
247245 Account {
248- alias: "buz " . into( ) ,
246+ alias : "Poor Bob " . into ( ) ,
249247 balance : 1 ,
250248 tags : None ,
251- }
252- ) ] )
253- ) ;
249+ } ,
250+ ) ] ) ,
251+ } ;
252+ assert_eq ! ( lhs, rhs) ;
254253 } ,
255254 ) ;
256255}
257256
258257#[ test]
259258#[ serial]
260259fn cli_override ( ) {
261- let cli = Cli {
262- toml : None ,
263- host : Some ( "cli.host" . into ( ) ) ,
264- port : Some ( 6060 ) ,
265- logs : Some ( true ) ,
266- main_account : None ,
267- sub_accounts : None ,
268- } ;
269- let cfg: Cfg = Cfg :: new ( cli) ;
270- assert_eq ! ( cfg. host, "cli.host" ) ;
271- assert_eq ! ( cfg. port, 6060 ) ;
272- assert_eq ! ( cfg. logs, true ) ;
273- }
274-
275- #[ test]
276- #[ serial]
277- fn nested_struct ( ) {
278- let cli = Cli {
279- toml : None ,
280- host : None ,
281- port : None ,
282- logs : None ,
283- main_account : None ,
284- sub_accounts : None ,
260+ let lhs = Cfg :: new ( Cli :: parse_from ( [
261+ "functora" ,
262+ "--port" ,
263+ "6060" ,
264+ "--logs" ,
265+ "true" ,
266+ "sub-accounts" ,
267+ "--alias" ,
268+ "Cli Carol" ,
269+ "--balance" ,
270+ "200" ,
271+ "--tags" ,
272+ "pure" ,
273+ "--tags" ,
274+ "geek" ,
275+ ] ) ) ;
276+ let rhs = Cfg {
277+ host : "127.0.0.1" . into ( ) ,
278+ port : 6060 ,
279+ logs : true ,
280+ main_account : Account {
281+ alias : "Functora" . into ( ) ,
282+ balance : 42 ,
283+ tags : None ,
284+ } ,
285+ sub_accounts : HashMap :: from ( [ (
286+ "0" . into ( ) ,
287+ Account {
288+ alias : "Cli Carol" . into ( ) ,
289+ balance : 200 ,
290+ tags : Some ( vec ! [
291+ "pure" . into( ) ,
292+ "geek" . into( ) ,
293+ ] ) ,
294+ } ,
295+ ) ] ) ,
285296 } ;
286- let cfg: Cfg = Cfg :: new ( cli) ;
287- assert_eq ! ( cfg. main_account. alias, "foo" ) ;
288- assert_eq ! ( cfg. main_account. balance, 42 ) ;
297+ assert_eq ! ( lhs, rhs) ;
289298}
0 commit comments