@@ -119,18 +119,19 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
119
119
}
120
120
}
121
121
122
- if id >= elements. len ( ) {
123
- return Err ( format ! ( "{} is a bad index" , id+1 ) ) ;
124
- }
125
-
126
- for i in 0 ..elements. len ( ) {
127
- if i == id {
128
- elements. remove ( i) ;
129
- break ;
130
- }
131
- }
132
- overwrite_database ( elements) ;
133
- Ok ( ( ) )
122
+ match check_elements ( id, & elements) {
123
+ Ok ( ( ) ) => {
124
+ for i in 0 ..elements. len ( ) {
125
+ if i == id {
126
+ elements. remove ( i) ;
127
+ break ;
128
+ }
129
+ }
130
+ overwrite_database ( elements) ;
131
+ Ok ( ( ) )
132
+ } ,
133
+ Err ( e) => Err ( e)
134
+ }
134
135
}
135
136
136
137
pub fn edit_element ( mut id : usize , secret : & str , issuer : & str , label : & str ) -> Result < ( ) , String > {
@@ -144,28 +145,28 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str) -> Res
144
145
Ok ( result) => elements = result,
145
146
Err ( _e) => return Err ( String :: from ( "Cannot decrypt existing database" ) )
146
147
}
147
-
148
148
149
- if id >= elements. len ( ) {
150
- return Err ( String :: from ( "Invalid element" ) ) ;
151
- }
152
-
153
- for i in 0 ..elements. len ( ) {
154
- if i == id{
155
- if secret != "." {
156
- elements[ i] . set_secret ( secret. to_string ( ) ) ;
157
- }
158
- if issuer != "." {
159
- elements[ i] . set_issuer ( issuer. to_string ( ) ) ;
160
- }
161
- if label != "." {
162
- elements[ i] . set_label ( label. to_string ( ) ) ;
149
+ match check_elements ( id, & elements) {
150
+ Ok ( ( ) ) => {
151
+ for i in 0 ..elements. len ( ) {
152
+ if i == id{
153
+ if secret != "." {
154
+ elements[ i] . set_secret ( secret. to_string ( ) ) ;
155
+ }
156
+ if issuer != "." {
157
+ elements[ i] . set_issuer ( issuer. to_string ( ) ) ;
158
+ }
159
+ if label != "." {
160
+ elements[ i] . set_label ( label. to_string ( ) ) ;
161
+ }
162
+ break ;
163
+ }
163
164
}
164
- break ;
165
- }
165
+ overwrite_database ( elements) ;
166
+ Ok ( ( ) )
167
+ } ,
168
+ Err ( e) => Err ( e)
166
169
}
167
- overwrite_database ( elements) ;
168
- Ok ( ( ) )
169
170
}
170
171
171
172
pub fn export_database ( ) -> Result < String , String > {
@@ -176,6 +177,9 @@ pub fn export_database() -> Result<String, String> {
176
177
let contents = cryptograpy:: decrypt_string ( & encrypted_contents, & cryptograpy:: prompt_for_passwords ( "Password: " ) ) ;
177
178
match contents {
178
179
Ok ( contents) => {
180
+ if contents == "[]" {
181
+ return Err ( String :: from ( "there are no elements in your database, type \" cotp -h\" to get help" ) ) ;
182
+ }
179
183
file. write_all ( contents. as_bytes ( ) ) . expect ( "Failed to write contents" ) ;
180
184
return Ok ( exported_path) ;
181
185
} ,
@@ -195,3 +199,15 @@ pub fn overwrite_database_json(json: &str){
195
199
utils:: write_to_file ( & encrypted, & mut File :: create ( utils:: get_db_path ( ) ) . expect ( "Failed to open file" ) ) ;
196
200
}
197
201
202
+ fn check_elements ( id : usize , elements : & Vec < OTPElement > ) -> Result < ( ) , String > {
203
+ if elements. len ( ) == 0 {
204
+ return Err ( String :: from ( "there are no elements in your database. Type \" cotp -h\" to get help." ) ) ;
205
+ }
206
+
207
+ if id >= elements. len ( ) {
208
+ return Err ( format ! ( "{} is a bad index" , id+1 ) ) ;
209
+ }
210
+
211
+ Ok ( ( ) )
212
+ }
213
+
0 commit comments