Skip to content

Commit 26569e1

Browse files
committedDec 11, 2018
Return Errors instead of process:exit
1 parent 9ec4df2 commit 26569e1

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed
 

‎src/main.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ extern crate tokei;
55

66
use colored::Color;
77
use colored::*;
8-
use git2::{Error, Repository};
8+
use git2::Repository;
99
use license::License;
1010
use std::{
1111
convert::From,
12+
error,
1213
ffi::OsStr,
1314
fmt,
1415
fmt::Write,
1516
fs,
16-
process::{exit, Command, Stdio},
17+
process::{Command, Stdio},
18+
result,
1719
str::FromStr,
1820
};
1921

22+
macro_rules! err {
23+
($($tt:tt)*) => { Err(Box::<error::Error>::from(format!($($tt)*))) }
24+
}
25+
26+
type Result<T> = result::Result<T, Box<error::Error>>;
27+
2028
struct Info {
2129
project_name: String,
2230
language: Language,
@@ -51,12 +59,7 @@ impl fmt::Display for Info {
5159
"Author: "
5260
};
5361

54-
writeln!(
55-
buffer,
56-
"{}{}",
57-
title.color(color).bold(),
58-
self.authors[0]
59-
)?;
62+
writeln!(buffer, "{}{}", title.color(color).bold(), self.authors[0])?;
6063

6164
let title = " ".repeat(title.len());
6265

@@ -144,40 +147,43 @@ impl fmt::Display for Language {
144147
}
145148
}
146149

147-
fn main() {
150+
fn main() -> Result<()> {
148151
let tokei_langs = project_languages();
149152
let language = match get_dominant_language(&tokei_langs) {
150153
Some(language) => language,
151154
None => {
152-
eprintln!("Error: Could not find any source code in this directory.");
153-
exit(1);
155+
return err!("Could not find any source code in this directory.");
154156
}
155157
};
156158

157159
if !is_git_installed() {
158-
eprintln!("Error: Could not execute git for project information.");
159-
exit(1);
160+
return err!("Could not execute git for project information.");
160161
}
161162

162163
let authors = get_authors(3);
163164
let config: Configuration = match get_configuration() {
164165
Ok(config) => config,
165166
Err(_) => {
166-
eprintln!("Error: Could not retrieve git configuration data");
167-
exit(1);
167+
return err!("Could not retrieve git configuration data");
168168
}
169169
};
170170

171+
let license = match project_license() {
172+
Ok(l) => l,
173+
Err(_) => return err!("Could read directory ./"),
174+
};
175+
171176
let info = Info {
172177
project_name: config.repository_name,
173178
language,
174179
authors,
175180
repo: config.repository_url,
176181
number_of_lines: get_total_loc(&tokei_langs),
177-
license: project_license(),
182+
license,
178183
};
179184

180185
println!("{}", info);
186+
Ok(())
181187
}
182188

183189
fn project_languages() -> tokei::Languages {
@@ -187,10 +193,9 @@ fn project_languages() -> tokei::Languages {
187193
languages
188194
}
189195

190-
fn project_license() -> String {
191-
let output = fs::read_dir(".")
192-
.unwrap()
193-
.filter_map(Result::ok)
196+
fn project_license() -> Result<String> {
197+
let output = fs::read_dir(".")?
198+
.filter_map(result::Result::ok)
194199
.map(|entry| entry.path())
195200
.filter(
196201
|entry| {
@@ -203,15 +208,15 @@ fn project_license() -> String {
203208
}, // TODO: multiple prefixes, like COPYING?
204209
)
205210
.map(|entry| license::Kind::from_str(&fs::read_to_string(entry).unwrap_or("".into())))
206-
.filter_map(Result::ok)
211+
.filter_map(result::Result::ok)
207212
.map(|license| license.name().to_string())
208213
.collect::<Vec<_>>()
209214
.join(", ");
210215

211216
if output == "" {
212-
"Unknown".into()
217+
Ok("Unknown".into())
213218
} else {
214-
output
219+
Ok(output)
215220
}
216221
}
217222

@@ -229,7 +234,7 @@ struct Configuration {
229234
pub repository_url: String,
230235
}
231236

232-
fn get_configuration() -> Result<Configuration, Error> {
237+
fn get_configuration() -> Result<Configuration> {
233238
let repo = Repository::open("./")?;
234239
let config = repo.config()?;
235240
let mut remote_url = String::new();

0 commit comments

Comments
 (0)
Please sign in to comment.