Skip to content

Commit 5dfd80a

Browse files
committed
reorganise file layout based on 2018 edition
1 parent 816b28b commit 5dfd80a

File tree

11 files changed

+365
-404
lines changed

11 files changed

+365
-404
lines changed

Cargo.lock

Lines changed: 275 additions & 268 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@ repository = "https://github.com/untitaker/mates.rs"
1313

1414
edition = "2018"
1515

16-
[lib]
17-
name = "mates"
18-
path = "src/mates/lib.rs"
19-
20-
[[bin]]
21-
name = "mates"
22-
test = false
23-
doc = false
24-
2516
[dependencies]
2617
uuid = { version = "0.5", features = ["v4"] }
2718
atomicwrites = "0.1"
2819
email = "0.0"
2920
clap = "2.31"
3021
vobject = "0.2"
3122
cursive = "0.14"
23+
anyhow = "1.0.26"
3224

3325
[build-dependencies]
3426
clap = "2.31"

build.rs renamed to build.rs.bak

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
extern crate clap;
2+
extern crate mates;
23

34
use std::env;
45
use std::fs;
56

67
use clap::Shell;
78

8-
#[path = "src/mates/app.rs"]
9-
mod app;
10-
119
fn main() {
1210
let outdir = match env::var_os("OUT_DIR") {
1311
None => return,
1412
Some(outdir) => outdir,
1513
};
1614
fs::create_dir_all(&outdir).unwrap();
1715

18-
let mut app = app::app();
16+
let mut app = mates::app();
1917
app.gen_completions("mates", Shell::Bash, &outdir);
2018
app.gen_completions("mates", Shell::Fish, &outdir);
2119
app.gen_completions("mates", Shell::Zsh, &outdir);
File renamed without changes.

src/bin/mates.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 12 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use std::borrow::ToOwned;
22
use std::env;
3-
use std::error::Error;
4-
use std::fmt;
3+
use anyhow::Result;
54
use std::fs;
65
use std::io;
76
use std::io::{Read, Write};
87
use std::path;
9-
use std::process;
108

119
use atomicwrites::{AllowOverwrite, AtomicFile};
1210

13-
use crate::app;
1411
use crate::editor;
1512
use crate::utils;
1613
use 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-
}
File renamed without changes.
File renamed without changes.

src/mates/lib.rs renamed to src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[macro_use]
2+
extern crate anyhow;
13
extern crate atomicwrites;
24
extern crate clap;
35
extern crate cursive;
@@ -8,4 +10,4 @@ extern crate vobject;
810
pub mod app;
911
pub mod cli;
1012
mod editor;
11-
mod utils;
13+
pub mod utils;

src/main.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#[macro_use]
2+
extern crate anyhow;
3+
use anyhow::Result;
4+
use std::fs;
5+
use std::io;
6+
use std::io::{Read, Write};
7+
8+
use mates::app;
9+
use mates::cli;
10+
use mates::utils;
11+
12+
fn main() -> Result<()> {
13+
let matches = app::app().get_matches();
14+
15+
let command = matches.subcommand_name().unwrap();
16+
17+
let config = match cli::Configuration::new() {
18+
Ok(x) => x,
19+
Err(e) => {
20+
return Err(anyhow!("Error while reading configuration: {}", e));
21+
}
22+
};
23+
24+
let submatches = matches
25+
.subcommand_matches(command)
26+
.expect("Internal error.");
27+
28+
match command {
29+
"index" => {
30+
println!(
31+
"Rebuilding index file \"{}\"...",
32+
config.index_path.display()
33+
);
34+
cli::build_index(&config.index_path, &config.vdir_path)?;
35+
}
36+
"mutt-query" => {
37+
let query = submatches.value_of("query").unwrap_or("");
38+
cli::mutt_query(&config, &query[..])?;
39+
}
40+
"file-query" => {
41+
let query = submatches.value_of("query").unwrap_or("");
42+
cli::file_query(&config, &query[..])?;
43+
}
44+
"email-query" => {
45+
let query = submatches.value_of("query").unwrap_or("");
46+
cli::email_query(&config, &query[..])?;
47+
}
48+
"add" => {
49+
let stdin = io::stdin();
50+
let mut email = String::new();
51+
stdin.lock().read_to_string(&mut email)?;
52+
let contact = utils::add_contact_from_email(&config.vdir_path, &email[..])?;
53+
println!("{}", contact.path.display());
54+
55+
let mut index_fp = fs::OpenOptions::new()
56+
.append(true)
57+
.write(true)
58+
.open(&config.index_path)?;
59+
60+
let index_entry = utils::index_item_from_contact(&contact)?;
61+
index_fp.write_all(index_entry.as_bytes())?;
62+
}
63+
"edit" => {
64+
let query = submatches.value_of("file-or-query").unwrap_or("");
65+
cli::edit_contact(&config, &query[..])?;
66+
}
67+
_ => {
68+
return Err(anyhow!("Invalid command: {}", command));
69+
}
70+
};
71+
Ok(())
72+
}

0 commit comments

Comments
 (0)