Skip to content

Commit 93fb2c2

Browse files
committed
refactor(Path Handling): Improved error handling through Path propagation with matches
1 parent 3811ba0 commit 93fb2c2

File tree

5 files changed

+42
-51
lines changed

5 files changed

+42
-51
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bashdoc"
3-
version = "0.4.8"
3+
version = "0.4.9"
44
authors = ["Dustin Knopoff <dustinknopoff@gmail.com>"]
55
description = """
66
A tool for generating documentation/help menu for user defined bash functions.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ See the [changelog](https://github.com/dustinknopoff/bashdoc/blob/master/CHANGEL
106106
- v.0.4.6 - Improved Error handling, `--html` argument removed replaced with `--location`, `--template` argument added for supplying custom `.hbs`
107107
- v0.4.7 - Fix required location for all inputs and not exclusive to `--location`
108108
- v0.4.8 - Clearer README, link to docs.rs documentation
109+
- v0.4.9 - Improved error path handling
109110

110111
License: MIT

cli.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
about: 'Creates a "javadoc" like structure for bash. See github repo github.com/dustinknopoff/bashdoc for information on formatting.'
22
author: "Dustin Knopoff <dustinknopoff@gmail.com>"
33
name: bashdoc
4-
version: "0.4.8"
4+
version: "0.4.9"
55
args:
66
- color:
77
help: "toggles color"
88
long: color
99
short: c
1010
- INPUT:
11-
help: "Sets the input file to use"
11+
help: "Sets the input file or glob pattern to use"
1212
index: 1
1313
required: true
14-
- directory:
15-
help: "pass a glob pattern to run on."
16-
long: directory
17-
short: d
1814
- json:
1915
help: "print result as JSON"
2016
long: json

src/docs.rs

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,10 @@ pub mod runners {
2525
("override", Some(sub_m)) => Delimiters::override_delims(sub_m),
2626
_ => Delimiters::get_delims(),
2727
};
28-
let all_em = if matches.is_present("directory") {
29-
start(
30-
matches.value_of("INPUT").expect("directory glob not found"),
31-
true,
32-
delims,
33-
)
34-
} else {
35-
start(
36-
matches.value_of("INPUT").expect("no file found."),
37-
false,
38-
delims,
39-
)
40-
};
28+
let all_em = start(
29+
&Path::new(matches.value_of("INPUT").expect("directory glob not found")),
30+
delims,
31+
);
4132
if matches.is_present("json") {
4233
write_json(&all_em, matches.value_of("json").unwrap());
4334
} else if matches.is_present("location") {
@@ -218,12 +209,22 @@ mod doc {
218209
impl Doc {
219210
/// Build a `Doc` from an array of strings
220211
/// Parse `Doc` fields.
221-
pub fn make_doc(vector: &Extracted, delims: Delimiters) -> Doc {
212+
pub fn make_doc(
213+
vector: &Extracted,
214+
delims: Delimiters,
215+
fname: &str,
216+
) -> Result<Doc, nom::ErrorKind> {
222217
// println!("{:#?}", vector);
223218
let parsed = parse_doc(&vector.content, delims);
224-
let mut result = parsed.expect("Parsing error.").1;
219+
let mut result = match parsed {
220+
Ok(e) => e.1,
221+
Err(_) => {
222+
println!("{} did not contain any docstrings.", fname);
223+
exit(1);
224+
}
225+
};
225226
result.position = vector.position.line + 1;
226-
result
227+
Ok(result)
227228
}
228229
}
229230
}
@@ -284,7 +285,13 @@ mod docfile {
284285
///
285286
/// A final `Vec` of the collected comment strings is returned.
286287
pub fn get_strings_from_file<'a>(p: &Path, delims: Delimiters) -> Vec<Extracted<'a>> {
287-
let mut f = File::open(&p).expect("file not found.");
288+
let mut f = match File::open(&p) {
289+
Ok(m) => m,
290+
Err(_) => {
291+
println!("Provided path is invalid");
292+
exit(1);
293+
}
294+
};
288295
let mut buffer = String::new();
289296
f.read_to_string(&mut buffer).unwrap();
290297
let used = Box::leak(buffer.into_boxed_str());
@@ -303,53 +310,39 @@ mod docfile {
303310
/// Given a `Vec<str>` make a `DocFile`
304311
pub fn generate_doc_file(
305312
docs: &[Extracted<'static>],
306-
fname: String,
313+
fname: &Path,
307314
delims: Delimiters,
308315
) -> DocFile {
309316
let mut all_docs: DocFile = Default::default();
310-
all_docs.filename = fname;
317+
all_docs.filename = String::from(fname.file_stem().unwrap().to_str().unwrap());
311318
let collected: Vec<Doc> = docs
312319
.par_iter()
313320
.filter(|x| !x.content.is_empty())
314-
.map(|x| Doc::make_doc(x, delims))
321+
.map(|x| Doc::make_doc(x, delims, &all_docs.filename).unwrap())
315322
.collect();
316323
all_docs.thedocs = collected;
317324
all_docs
318325
}
319326

320327
/// Given a file path and delimiters, generate a DocFile for all files requested.
321-
pub fn start(p: &str, is_directory: bool, delims: Delimiters) -> Vec<DocFile> {
322-
let dir = if cfg!(windows) {
323-
String::from(p)
324-
} else {
325-
p.replace("~", home_dir().unwrap().to_str().unwrap())
326-
};
327-
if is_directory {
328-
let files: Vec<_> = glob(&dir).unwrap().filter_map(|x| x.ok()).collect();
328+
pub fn start(p: &Path, delims: Delimiters) -> Vec<DocFile> {
329+
if p.is_dir() || p.to_str().unwrap().contains("*") {
330+
let pth = home_dir().unwrap().join(p.strip_prefix("~").unwrap());
331+
let files: Vec<_> = glob(pth.to_str().unwrap())
332+
.unwrap()
333+
.filter_map(|x| x.ok())
334+
.collect();
329335
let every_doc: Vec<DocFile> = files
330336
.par_iter()
331337
.map(|entry| {
332338
let docs = get_strings_from_file(&entry, delims);
333-
generate_doc_file(
334-
&docs,
335-
entry.file_name().unwrap().to_str().unwrap().to_string(),
336-
delims,
337-
)
339+
generate_doc_file(&docs, &entry, delims)
338340
})
339341
.collect();
340342
every_doc
341343
} else {
342-
let docs = get_strings_from_file(&Path::new(&p), delims);
343-
let all_docs = generate_doc_file(
344-
&docs,
345-
Path::new(&dir)
346-
.file_name()
347-
.unwrap()
348-
.to_str()
349-
.unwrap()
350-
.to_string(),
351-
delims,
352-
);
344+
let docs = get_strings_from_file(&p.canonicalize().unwrap(), delims);
345+
let all_docs = generate_doc_file(&docs, &p, delims);
353346
let result = vec![all_docs];
354347
result
355348
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
//!- v.0.4.6 - Improved Error handling, `--html` argument removed replaced with `--location`, `--template` argument added for supplying custom `.hbs`
105105
//!- v0.4.7 - Fix required location for all inputs and not exclusive to `--location`
106106
//!- v0.4.8 - Clearer README, link to docs.rs documentation
107+
//!- v0.4.9 - Improved error path handling
107108
mod docs;
108109
use crate::docs::runners::*;
109110
use clap::{load_yaml, App};

0 commit comments

Comments
 (0)