11use crate :: args:: { AddArgs , EditArgs , ExportArgs , ImportArgs } ;
22use crate :: exporters:: do_export;
3+ use crate :: exporters:: otp_uri:: OtpUriList ;
34use crate :: importers:: aegis:: AegisJson ;
45use crate :: importers:: aegis_encrypted:: AegisEncryptedDatabase ;
56use crate :: importers:: authy_remote_debug:: AuthyExportedList ;
67use crate :: importers:: converted:: ConvertedJsonList ;
78use crate :: importers:: freeotp_plus:: FreeOTPPlusJson ;
9+ use crate :: importers:: importer:: import_from_path;
810use crate :: otp:: from_otp_uri:: FromOtpUri ;
911use crate :: otp:: otp_element:: { OTPDatabase , OTPElement } ;
10- use crate :: { importers, utils} ;
12+ use crate :: utils;
13+ use color_eyre:: eyre:: { eyre, ErrReport } ;
1114use zeroize:: Zeroize ;
1215
13- pub fn import ( matches : ImportArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
16+ pub fn import ( matches : ImportArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
1417 let path = matches. path ;
1518
1619 let backup_type = matches. backup_type ;
1720
1821 let result = if backup_type. cotp {
19- importers :: importer :: import_from_path :: < OTPDatabase > ( path)
22+ import_from_path :: < OTPDatabase > ( path)
2023 } else if backup_type. andotp {
21- importers :: importer :: import_from_path :: < Vec < OTPElement > > ( path)
24+ import_from_path :: < Vec < OTPElement > > ( path)
2225 } else if backup_type. aegis {
23- importers :: importer :: import_from_path :: < AegisJson > ( path)
26+ import_from_path :: < AegisJson > ( path)
2427 } else if backup_type. aegis_encrypted {
25- importers :: importer :: import_from_path :: < AegisEncryptedDatabase > ( path)
28+ import_from_path :: < AegisEncryptedDatabase > ( path)
2629 } else if backup_type. freeotp_plus {
27- importers :: importer :: import_from_path :: < FreeOTPPlusJson > ( path)
30+ import_from_path :: < FreeOTPPlusJson > ( path)
2831 } else if backup_type. authy_exported {
29- importers :: importer :: import_from_path :: < AuthyExportedList > ( path)
32+ import_from_path :: < AuthyExportedList > ( path)
3033 } else if backup_type. google_authenticator
3134 || backup_type. authy
3235 || backup_type. microsoft_authenticator
3336 || backup_type. freeotp
3437 {
35- importers:: importer:: import_from_path :: < ConvertedJsonList > ( path)
38+ import_from_path :: < ConvertedJsonList > ( path)
39+ } else if backup_type. otp_uri {
40+ import_from_path :: < OtpUriList > ( path)
3641 } else {
37- return Err ( String :: from ( "Invalid arguments provided" ) ) ;
42+ return Err ( eyre ! ( "Invalid arguments provided" ) ) ;
3843 } ;
3944
40- let elements = result. map_err ( |e| format ! ( "An error occurred: {e}" ) ) ?;
45+ let elements = result. map_err ( |e| eyre ! ( "{e}" ) ) ?;
4146
4247 database. add_all ( elements) ;
4348 Ok ( database)
4449}
4550
46- pub fn add ( matches : AddArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
51+ pub fn add ( matches : AddArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
4752 let otp_element = if matches. otp_uri {
4853 let mut otp_uri = rpassword:: prompt_password ( "Insert the otp uri: " ) . unwrap ( ) ;
4954 let result = OTPElement :: from_otp_uri ( otp_uri. as_str ( ) ) ;
@@ -53,24 +58,23 @@ pub fn add(matches: AddArgs, mut database: OTPDatabase) -> Result<OTPDatabase, S
5358 get_from_args ( matches) ?
5459 } ;
5560 if !otp_element. valid_secret ( ) {
56- return Err ( String :: from ( "Invalid secret." ) ) ;
61+ return Err ( ErrReport :: msg ( "Invalid secret." ) ) ;
5762 }
5863
5964 database. add_element ( otp_element) ;
6065 Ok ( database)
6166}
6267
63- fn get_from_args ( matches : AddArgs ) -> Result < OTPElement , String > {
64- let secret = rpassword:: prompt_password ( "Insert the secret: " )
65- . map_err ( |e| format ! ( "Error during password insertion: {:?}" , e) ) ?;
68+ fn get_from_args ( matches : AddArgs ) -> color_eyre:: Result < OTPElement > {
69+ let secret = rpassword:: prompt_password ( "Insert the secret: " ) . map_err ( ErrReport :: from) ?;
6670 Ok ( map_args_to_code ( secret, matches) )
6771}
6872
6973fn map_args_to_code ( secret : String , matches : AddArgs ) -> OTPElement {
7074 OTPElement {
7175 secret,
72- issuer : matches. issuer . unwrap ( ) ,
73- label : matches. label ,
76+ issuer : matches. issuer ,
77+ label : matches. label . unwrap ( ) ,
7478 digits : matches. digits ,
7579 type_ : matches. otp_type ,
7680 algorithm : matches. algorithm ,
@@ -80,7 +84,7 @@ fn map_args_to_code(secret: String, matches: AddArgs) -> OTPElement {
8084 }
8185}
8286
83- pub fn edit ( matches : EditArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
87+ pub fn edit ( matches : EditArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
8488 let secret = matches
8589 . change_secret
8690 . then ( || rpassword:: prompt_password ( "Insert the secret: " ) . unwrap ( ) ) ;
@@ -90,7 +94,7 @@ pub fn edit(matches: EditArgs, mut database: OTPDatabase) -> Result<OTPDatabase,
9094
9195 if let Some ( real_index) = index. checked_sub ( 1 ) {
9296 if real_index >= database. elements_ref ( ) . len ( ) {
93- return Err ( format ! ( "{index} is an invalid index" ) ) ;
97+ return Err ( eyre ! ( "{index} is an invalid index" ) ) ;
9498 }
9599
96100 match database. mut_element ( real_index) {
@@ -121,15 +125,15 @@ pub fn edit(matches: EditArgs, mut database: OTPDatabase) -> Result<OTPDatabase,
121125 }
122126 database. mark_modified ( ) ;
123127 }
124- None => return Err ( format ! ( "No element found at index {index}" ) ) ,
128+ None => return Err ( eyre ! ( "No element found at index {index}" ) ) ,
125129 }
126130 Ok ( database)
127131 } else {
128- Err ( format ! { "{index} is an invalid index" } )
132+ Err ( eyre ! ( "{index} is an invalid index" ) )
129133 }
130134}
131135
132- pub fn export ( matches : ExportArgs , database : OTPDatabase ) -> Result < OTPDatabase , String > {
136+ pub fn export ( matches : ExportArgs , database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
133137 let export_format = matches. format . unwrap_or_default ( ) ;
134138 let exported_path = if matches. path . is_dir ( ) {
135139 matches. path . join ( "exported.cotp" )
@@ -142,6 +146,9 @@ pub fn export(matches: ExportArgs, database: OTPDatabase) -> Result<OTPDatabase,
142146 } else if export_format. andotp {
143147 let andotp: & Vec < OTPElement > = ( & database) . into ( ) ;
144148 do_export ( & andotp, exported_path)
149+ } else if export_format. otp_uri {
150+ let otp_uri_list: OtpUriList = ( & database) . into ( ) ;
151+ do_export ( & otp_uri_list, exported_path)
145152 } else {
146153 unreachable ! ( "Unreachable code" ) ;
147154 }
@@ -152,14 +159,14 @@ pub fn export(matches: ExportArgs, database: OTPDatabase) -> Result<OTPDatabase,
152159 ) ;
153160 database
154161 } )
155- . map_err ( |e| format ! ( "An error occurred while exporting database: {e}" ) )
162+ . map_err ( |e| eyre ! ( "An error occurred while exporting database: {e}" ) )
156163}
157164
158- pub fn change_password ( mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
165+ pub fn change_password ( mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
159166 let mut new_password = utils:: verified_password ( "New password: " , 8 ) ;
160167 database
161168 . save_with_pw ( & new_password)
162- . map_err ( |e| format ! ( "An error has occurred: {e}" ) ) ?;
169+ . map_err ( ErrReport :: from ) ?;
163170 new_password. zeroize ( ) ;
164171 Ok ( database)
165172}
0 commit comments