Skip to content

Commit f593b5f

Browse files
committed
refactor(docs.rs): Utilize a more rust/railway programming style
1 parent e932aea commit f593b5f

File tree

2 files changed

+77
-72
lines changed

2 files changed

+77
-72
lines changed

src/docs.rs

+75-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,80 @@ use std::{
1919
process::exit,
2020
};
2121

22+
/// "Main" of bashdoc
23+
pub mod runners {
24+
use super::*;
25+
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
26+
use std::{sync::mpsc::channel, time::Duration};
27+
28+
/// Given the arguments received via CLI from clap, setup and run with requested delimiters, file or directory, etc.
29+
pub fn generate<'a>(matches: &'a ArgMatches<'a>) {
30+
let delims = match matches.subcommand() {
31+
("override", Some(sub_m)) => Delimiters::override_delims(sub_m),
32+
_ => Delimiters::get_delims(),
33+
};
34+
let all_em = start(
35+
matches.value_of("INPUT").expect("directory glob not found"),
36+
delims,
37+
)
38+
.unwrap();
39+
if matches.is_present("json") {
40+
write_json(&all_em, matches.value_of("json").unwrap());
41+
} else if matches.is_present("location") {
42+
to_html(
43+
&all_em,
44+
matches.value_of("location"),
45+
matches.value_of("template"),
46+
);
47+
} else {
48+
for doc in &all_em {
49+
if matches.is_present("color") {
50+
printer(doc, true);
51+
} else {
52+
printer(doc, false);
53+
}
54+
}
55+
}
56+
}
57+
58+
/// Given a request to watch files, Call `generate` on file write.
59+
pub fn watcher<'a>(matches: &'a ArgMatches<'a>) {
60+
generate(matches);
61+
let (tx, rx) = channel();
62+
let mut watcher: RecommendedWatcher = match Watcher::new(tx, Duration::from_secs(2)) {
63+
Ok(d) => d,
64+
Err(_) => {
65+
println!("Provided path is invalid");
66+
exit(1);
67+
}
68+
};
69+
let path: String = if cfg!(windows) {
70+
String::from(matches.value_of("INPUT").unwrap())
71+
} else {
72+
matches
73+
.value_of("INPUT")
74+
.unwrap()
75+
.replace("~", home_dir().unwrap().to_str().unwrap())
76+
};
77+
watcher.watch(&path, RecursiveMode::Recursive).unwrap();
78+
println!("Watching for changes in {}...", path);
79+
loop {
80+
match rx.recv() {
81+
Ok(event) => {
82+
generate(&matches);
83+
if let DebouncedEvent::Write(e) = event {
84+
println!(
85+
"Bashdoc updated to match changes to {}.",
86+
e.as_path().file_name().unwrap().to_str().unwrap()
87+
);
88+
}
89+
}
90+
Err(e) => println!("watch error: {:?}", e),
91+
}
92+
}
93+
}
94+
}
95+
2296
/// Functions and declarations for general Key,Value Pair
2397
mod kv {
2498
use super::*;
@@ -420,7 +494,7 @@ mod outputs {
420494
}
421495

422496
/// Functions and declarations for generating/overriding delimiters
423-
pub mod delims {
497+
mod delims {
424498
use super::*;
425499
use std::io::prelude::*;
426500
/// Represents the necessary delimiters for a `bashdoc`

src/main.rs

+2-71
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@
107107
//! - v0.4.11 - support for overriding global `.bashdocrc` within a directory.
108108
//! - v0.4.12 - descriptors can be split on ':' or whitespace
109109
mod docs;
110-
use crate::docs::delims::override_delims;
111-
use clap::{load_yaml, App, ArgMatches};
112-
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
113-
use std::{sync::mpsc::channel, time::Duration};
110+
use crate::docs::runners::*;
111+
use clap::{load_yaml, App};
114112

115113
fn main() {
116114
let yaml = load_yaml!("../cli.yml");
@@ -121,70 +119,3 @@ fn main() {
121119
generate(&matches);
122120
}
123121
}
124-
125-
/// Given the arguments received via CLI from clap, setup and run with requested delimiters, file or directory, etc.
126-
pub fn generate<'a>(matches: &'a ArgMatches<'a>) {
127-
let delims = match matches.subcommand() {
128-
("override", Some(sub_m)) => override_delims(sub_m),
129-
_ => Delimiters::get_delims(),
130-
};
131-
let all_em = start(
132-
matches.value_of("INPUT").expect("directory glob not found"),
133-
delims,
134-
)
135-
.unwrap();
136-
if matches.is_present("json") {
137-
write_json(&all_em, matches.value_of("json").unwrap());
138-
} else if matches.is_present("location") {
139-
to_html(
140-
&all_em,
141-
matches.value_of("location"),
142-
matches.value_of("template"),
143-
);
144-
} else {
145-
for doc in &all_em {
146-
if matches.is_present("color") {
147-
printer(doc, true);
148-
} else {
149-
printer(doc, false);
150-
}
151-
}
152-
}
153-
}
154-
155-
/// Given a request to watch files, Call `generate` on file write.
156-
pub fn watcher<'a>(matches: &'a ArgMatches<'a>) {
157-
generate(matches);
158-
let (tx, rx) = channel();
159-
let mut watcher: RecommendedWatcher = match Watcher::new(tx, Duration::from_secs(2)) {
160-
Ok(d) => d,
161-
Err(_) => {
162-
println!("Provided path is invalid");
163-
exit(1);
164-
}
165-
};
166-
let path: String = if cfg!(windows) {
167-
String::from(matches.value_of("INPUT").unwrap())
168-
} else {
169-
matches
170-
.value_of("INPUT")
171-
.unwrap()
172-
.replace("~", home_dir().unwrap().to_str().unwrap())
173-
};
174-
watcher.watch(&path, RecursiveMode::Recursive).unwrap();
175-
println!("Watching for changes in {}...", path);
176-
loop {
177-
match rx.recv() {
178-
Ok(event) => {
179-
generate(&matches);
180-
if let DebouncedEvent::Write(e) = event {
181-
println!(
182-
"Bashdoc updated to match changes to {}.",
183-
e.as_path().file_name().unwrap().to_str().unwrap()
184-
);
185-
}
186-
}
187-
Err(e) => println!("watch error: {:?}", e),
188-
}
189-
}
190-
}

0 commit comments

Comments
 (0)