11use std:: borrow:: ToOwned ;
22use std:: env;
3- use std:: error:: Error ;
4- use std:: fmt;
3+ use anyhow:: Result ;
54use std:: fs;
65use std:: io;
76use std:: io:: { Read , Write } ;
87use std:: path;
9- use std:: process;
108
119use atomicwrites:: { AllowOverwrite , AtomicFile } ;
1210
13- use crate :: app;
1411use crate :: editor;
1512use crate :: utils;
1613use crate :: utils:: CustomPathExt ;
@@ -25,13 +22,13 @@ fn get_envvar(key: &str) -> Option<String> {
2522 match env:: var ( key) {
2623 Ok ( x) => Some ( x) ,
2724 Err ( env:: VarError :: NotPresent ) => None ,
28- Err ( env:: VarError :: NotUnicode ( _) ) => panic ! ( format! ( "{} is not unicode." , key) ) ,
25+ Err ( env:: VarError :: NotUnicode ( _) ) => panic ! ( "{} is not unicode." , key) ,
2926 }
3027}
3128
32- fn build_index ( outfile : & path:: Path , dir : & path:: Path ) -> MainResult < ( ) > {
29+ pub fn build_index ( outfile : & path:: Path , dir : & path:: Path ) -> Result < ( ) > {
3330 if !dir. is_dir ( ) {
34- return Err ( MainError :: new ( "MATES_DIR must be a directory." ) . into ( ) ) ;
31+ return Err ( anyhow ! ( "MATES_DIR must be a directory." ) ) ;
3532 } ;
3633
3734 let af = AtomicFile :: new ( & outfile, AllowOverwrite ) ;
@@ -78,95 +75,23 @@ fn build_index(outfile: &path::Path, dir: &path::Path) -> MainResult<()> {
7875 } ) ?;
7976
8077 if errors {
81- Err ( MainError :: new ( "Several errors happened while generating the index." ) . into ( ) )
78+ Err ( anyhow ! ( "Several errors happened while generating the index." ) )
8279 } else {
8380 Ok ( ( ) )
8481 }
8582}
8683
87- pub fn cli_main ( ) {
88- match cli_main_raw ( ) {
89- Err ( e) => {
90- writeln ! ( & mut io:: stderr( ) , "{}" , e) . unwrap ( ) ;
91- process:: exit ( 1 ) ;
92- }
93- _ => ( ) ,
94- } ;
95- }
96-
97- pub fn cli_main_raw ( ) -> MainResult < ( ) > {
98- let matches = app:: app ( ) . get_matches ( ) ;
99-
100- let command = matches. subcommand_name ( ) . unwrap ( ) ;
101-
102- let config = match Configuration :: new ( ) {
103- Ok ( x) => x,
104- Err ( e) => {
105- return Err ( MainError :: new ( format ! ( "Error while reading configuration: {}" , e) ) . into ( ) ) ;
106- }
107- } ;
108-
109- let submatches = matches
110- . subcommand_matches ( command)
111- . expect ( "Internal error." ) ;
112-
113- match command {
114- "index" => {
115- println ! (
116- "Rebuilding index file \" {}\" ..." ,
117- config. index_path. display( )
118- ) ;
119- build_index ( & config. index_path , & config. vdir_path ) ?;
120- }
121- "mutt-query" => {
122- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
123- mutt_query ( & config, & query[ ..] ) ?;
124- }
125- "file-query" => {
126- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
127- file_query ( & config, & query[ ..] ) ?;
128- }
129- "email-query" => {
130- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
131- email_query ( & config, & query[ ..] ) ?;
132- }
133- "add" => {
134- let stdin = io:: stdin ( ) ;
135- let mut email = String :: new ( ) ;
136- stdin. lock ( ) . read_to_string ( & mut email) ?;
137- let contact = utils:: add_contact_from_email ( & config. vdir_path , & email[ ..] ) ?;
138- println ! ( "{}" , contact. path. display( ) ) ;
139-
140- let mut index_fp = fs:: OpenOptions :: new ( )
141- . append ( true )
142- . write ( true )
143- . open ( & config. index_path ) ?;
144-
145- let index_entry = utils:: index_item_from_contact ( & contact) ?;
146- index_fp. write_all ( index_entry. as_bytes ( ) ) ?;
147- }
148- "edit" => {
149- let query = submatches. value_of ( "file-or-query" ) . unwrap_or ( "" ) ;
150- edit_contact ( & config, & query[ ..] ) ?;
151- }
152- _ => {
153- return Err ( MainError :: new ( format ! ( "Invalid command: {}" , command) ) . into ( ) ) ;
154- }
155- } ;
156- Ok ( ( ) )
157- }
158-
159- fn edit_contact ( config : & Configuration , query : & str ) -> MainResult < ( ) > {
84+ pub fn edit_contact ( config : & Configuration , query : & str ) -> Result < ( ) > {
16085 let results = if get_pwd ( ) . join ( query) . is_file ( ) {
16186 vec ! [ path:: PathBuf :: from( query) ]
16287 } else {
16388 utils:: file_query ( config, query) ?. into_iter ( ) . collect ( )
16489 } ;
16590
16691 if results. len ( ) < 1 {
167- return Err ( MainError :: new ( "No such contact." ) . into ( ) ) ;
92+ return Err ( anyhow ! ( "No such contact." ) ) ;
16893 } else if results. len ( ) > 1 {
169- return Err ( MainError :: new ( "Ambiguous query." ) . into ( ) ) ;
94+ return Err ( anyhow ! ( "Ambiguous query." ) ) ;
17095 }
17196
17297 let fpath = & results[ 0 ] ;
@@ -181,13 +106,13 @@ fn edit_contact(config: &Configuration, query: &str) -> MainResult<()> {
181106
182107 if ( & fcontent[ ..] ) . trim ( ) . len ( ) == 0 {
183108 fs:: remove_file ( fpath) ?;
184- return Err ( MainError :: new ( "Contact emptied, file removed." ) . into ( ) ) ;
109+ return Err ( anyhow ! ( "Contact emptied, file removed." ) ) ;
185110 } ;
186111
187112 Ok ( ( ) )
188113}
189114
190- fn mutt_query < ' a > ( config : & Configuration , query : & str ) -> MainResult < ( ) > {
115+ pub fn mutt_query < ' a > ( config : & Configuration , query : & str ) -> Result < ( ) > {
191116 println ! ( "" ) ; // For some reason mutt requires an empty line
192117 // We need to ignore errors here, otherwise mutt's UI will glitch
193118 if let Ok ( items) = utils:: index_query ( config, query) {
@@ -200,14 +125,14 @@ fn mutt_query<'a>(config: &Configuration, query: &str) -> MainResult<()> {
200125 Ok ( ( ) )
201126}
202127
203- fn file_query < ' a > ( config : & Configuration , query : & str ) -> MainResult < ( ) > {
128+ pub fn file_query < ' a > ( config : & Configuration , query : & str ) -> Result < ( ) > {
204129 for path in utils:: file_query ( config, query) ?. iter ( ) {
205130 println ! ( "{}" , path. display( ) ) ;
206131 }
207132 Ok ( ( ) )
208133}
209134
210- fn email_query < ' a > ( config : & Configuration , query : & str ) -> MainResult < ( ) > {
135+ pub fn email_query < ' a > ( config : & Configuration , query : & str ) -> Result < ( ) > {
211136 for item in utils:: index_query ( config, query) ? {
212137 if item. name . len ( ) > 0 && item. email . len ( ) > 0 {
213138 println ! ( "{} <{}>" , item. name, item. email) ;
@@ -248,32 +173,3 @@ impl Configuration {
248173 } )
249174 }
250175}
251-
252- #[ derive( PartialEq , Eq , Debug ) ]
253- pub struct MainError {
254- desc : String ,
255- }
256-
257- pub type MainResult < T > = Result < T , Box < dyn Error > > ;
258-
259- impl Error for MainError {
260- fn description ( & self ) -> & str {
261- & self . desc [ ..]
262- }
263-
264- fn cause ( & self ) -> Option < & dyn Error > {
265- None
266- }
267- }
268-
269- impl fmt:: Display for MainError {
270- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
271- self . desc . fmt ( f)
272- }
273- }
274-
275- impl MainError {
276- pub fn new < T : Into < String > > ( desc : T ) -> Self {
277- MainError { desc : desc. into ( ) }
278- }
279- }
0 commit comments