@@ -45,37 +45,36 @@ pub enum Error {
45
45
46
46
/// Failed to write to the accounts file
47
47
#[ error( "Failed to write to the accounts file: {0}" ) ]
48
- FailedWrite ( WritingError ) ,
48
+ FailedWrite ( # [ from ] WritingError ) ,
49
49
50
50
/// Failed to read the accounts file
51
51
#[ error( "Failed to read the accounts file: {0}" ) ]
52
- FailedRead ( ReadingError ) ,
52
+ FailedRead ( # [ from ] ReadingError ) ,
53
53
}
54
54
55
-
56
55
/// Possible errors when writing to the accounts file.
57
56
#[ derive( Debug , ThisError ) ]
58
57
pub enum WritingError {
59
- #[ error( "{0}" ) ]
58
+ #[ error( transparent ) ]
60
59
IO ( IOError ) ,
61
60
62
- #[ error( "{0}" ) ]
61
+ #[ error( transparent ) ]
63
62
Serialization ( serde_json:: Error ) ,
64
63
}
65
64
66
65
/// Possible errors when reading the accounts file.
67
66
#[ derive( Debug , ThisError ) ]
68
67
pub enum ReadingError {
69
- #[ error( "{0}" ) ]
68
+ #[ error( transparent ) ]
70
69
IO ( IOError ) ,
71
70
72
- #[ error( "{0}" ) ]
71
+ #[ error( transparent ) ]
73
72
Deserialization ( serde_json:: Error ) ,
74
73
}
75
74
76
75
/// Add an account to the storage.
77
76
///
78
- /// Fails if an account with the given `name` already exists_ .
77
+ /// Fails if an account with the given `name` already exists .
79
78
/// It can also fail from IO and Serde Json errors.
80
79
pub fn add ( name : String , data : AccountData ) -> Result < ( ) , Error > {
81
80
let mut accounts = list ( ) ?;
@@ -87,25 +86,22 @@ pub fn add(name: String, data: AccountData) -> Result<(), Error> {
87
86
update ( accounts)
88
87
}
89
88
90
- /// List all the stored accountsr
89
+ /// List all the stored accounts.
91
90
///
92
91
/// It can fail from IO and Serde Json errors.
93
92
pub fn list ( ) -> Result < HashMap < String , AccountData > , Error > {
94
93
let path_buf = get_or_create_path ( ) ?;
95
- let file = File :: open ( path_buf. as_path ( ) )
96
- . map_err ( |e| Error :: FailedRead ( ReadingError :: IO ( e) ) ) ?;
94
+ let file = File :: open ( path_buf. as_path ( ) ) . map_err ( ReadingError :: IO ) ?;
97
95
let accounts: HashMap < String , AccountData > =
98
- serde_json:: from_reader ( & file)
99
- . map_err ( |e| Error :: FailedRead ( ReadingError :: Deserialization ( e) ) ) ?;
96
+ serde_json:: from_reader ( & file) . map_err ( ReadingError :: Deserialization ) ?;
100
97
Ok ( accounts)
101
98
}
102
99
103
100
fn update ( accounts : HashMap < String , AccountData > ) -> Result < ( ) , Error > {
104
101
let path_buf = get_or_create_path ( ) ?;
105
- let new_content = serde_json:: to_string ( & accounts)
106
- . map_err ( |e| Error :: FailedWrite ( WritingError :: Serialization ( e) ) ) ?;
107
- std:: fs:: write ( path_buf. as_path ( ) , new_content. as_bytes ( ) )
108
- . map_err ( |e| Error :: FailedWrite ( WritingError :: IO ( e) ) )
102
+ let new_content = serde_json:: to_string ( & accounts) . map_err ( WritingError :: Serialization ) ?;
103
+ std:: fs:: write ( path_buf. as_path ( ) , new_content. as_bytes ( ) ) . map_err ( WritingError :: IO ) ?;
104
+ Ok ( ( ) )
109
105
}
110
106
111
107
const FILE : & str = "accounts.json" ;
@@ -121,8 +117,7 @@ fn get_or_create_path() -> Result<PathBuf, Error> {
121
117
let path = path_buf. as_path ( ) ;
122
118
123
119
if !path. exists ( ) {
124
- std:: fs:: write ( path, b"{}" )
125
- . map_err ( |e| Error :: FailedWrite ( WritingError :: IO ( e) ) ) ?;
120
+ std:: fs:: write ( path, b"{}" ) . map_err ( WritingError :: IO ) ?;
126
121
}
127
122
128
123
Ok ( path_buf)
@@ -133,6 +128,6 @@ fn dir() -> Result<PathBuf, Error> {
133
128
. unwrap ( )
134
129
. data_dir ( )
135
130
. join ( "radicle-registry-cli" ) ;
136
- std:: fs:: create_dir_all ( & dir) . map_err ( |e| Error :: FailedRead ( ReadingError :: IO ( e ) ) ) ?;
131
+ std:: fs:: create_dir_all ( & dir) . map_err ( ReadingError :: IO ) ?;
137
132
Ok ( dir)
138
133
}
0 commit comments