Skip to content

Commit

Permalink
Add a rustfmt config and run rustfmt on the code base
Browse files Browse the repository at this point in the history
  • Loading branch information
azerupi committed Mar 17, 2016
1 parent 6bac41c commit ad0794a
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 381 deletions.
15 changes: 15 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
write_mode = "Overwrite"

max_width = 120
ideal_width = 120
fn_call_width = 100

fn_args_density = "Compressed"

enum_trailing_comma = true
match_block_trailing_comma = true
struct_trailing_comma = "Always"
wrap_comments = true

report_todo = "Always"
report_fixme = "Always"
134 changes: 68 additions & 66 deletions src/bin/mdbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ fn main() {

// Check which subcomamnd the user ran...
let res = match matches.subcommand() {
("init", Some(sub_matches)) => init(sub_matches),
("init", Some(sub_matches)) => init(sub_matches),
("build", Some(sub_matches)) => build(sub_matches),
#[cfg(feature = "watch")]
("watch", Some(sub_matches)) => watch(sub_matches),
("test", Some(sub_matches)) => test(sub_matches),
(_, _) => unreachable!()
(_, _) => unreachable!(),
};

if let Err(e) = res {
Expand All @@ -77,7 +77,7 @@ fn confirm() -> bool {
io::stdin().read_line(&mut s).ok();
match &*s.trim() {
"Y" | "y" | "yes" | "Yes" => true,
_ => false
_ => false,
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
if confirm() {
book.create_gitignore();
println!("\n.gitignore created.");
}
}
}

println!("\nAll done, no errors...");
Expand All @@ -151,66 +151,68 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
let book = MDBook::new(&book_dir).read_config();

// Create a channel to receive the events.
let (tx, rx) = channel();

let w: Result<notify::RecommendedWatcher, notify::Error> = notify::Watcher::new(tx);

match w {
Ok(mut watcher) => {

// Add the source directory to the watcher
if let Err(e) = watcher.watch(book.get_src()) {
println!("Error while watching {:?}:\n {:?}", book.get_src(), e);
::std::process::exit(0);
};

// Add the book.json file to the watcher if it exists, because it's not
// located in the source directory
if let Err(_) = watcher.watch(book_dir.join("book.json")) {
// do nothing if book.json is not found
}

let previous_time = time::get_time().sec;

crossbeam::scope(|scope| {
loop {
match rx.recv() {
Ok(event) => {

// Skip the event if an event has already been issued in the last second
if time::get_time().sec - previous_time < 1 { continue }

if let Some(path) = event.path {
// Trigger the build process in a new thread (to keep receiving events)
scope.spawn(move || {
println!("File changed: {:?}\nBuilding book...\n", path);
match build(args) {
Err(e) => println!("Error while building: {:?}", e),
_ => {}
}
println!("");
});

} else {
continue;
}
},
Err(e) => {
println!("An error occured: {:?}", e);
}
}
}
});

},
Err(e) => {
println!("Error while trying to watch the files:\n\n\t{:?}", e);
::std::process::exit(0);
}
}

Ok(())
}
let (tx, rx) = channel();

let w: Result<notify::RecommendedWatcher, notify::Error> = notify::Watcher::new(tx);

match w {
Ok(mut watcher) => {

// Add the source directory to the watcher
if let Err(e) = watcher.watch(book.get_src()) {
println!("Error while watching {:?}:\n {:?}", book.get_src(), e);
::std::process::exit(0);
};

// Add the book.json file to the watcher if it exists, because it's not
// located in the source directory
if let Err(_) = watcher.watch(book_dir.join("book.json")) {
// do nothing if book.json is not found
}

let previous_time = time::get_time().sec;

crossbeam::scope(|scope| {
loop {
match rx.recv() {
Ok(event) => {

// Skip the event if an event has already been issued in the last second
if time::get_time().sec - previous_time < 1 {
continue;
}

if let Some(path) = event.path {
// Trigger the build process in a new thread (to keep receiving events)
scope.spawn(move || {
println!("File changed: {:?}\nBuilding book...\n", path);
match build(args) {
Err(e) => println!("Error while building: {:?}", e),
_ => {},
}
println!("");
});

} else {
continue;
}
},
Err(e) => {
println!("An error occured: {:?}", e);
},
}
}
});

},
Err(e) => {
println!("Error while trying to watch the files:\n\n\t{:?}", e);
::std::process::exit(0);
},
}

Ok(())
}



Expand All @@ -230,9 +232,9 @@ fn get_book_dir(args: &ArgMatches) -> PathBuf {
// Check if path is relative from current dir, or absolute...
let p = Path::new(dir);
if p.is_relative() {
env::current_dir().unwrap().join(dir)
env::current_dir().unwrap().join(dir)
} else {
p.to_path_buf()
p.to_path_buf()
}
} else {
env::current_dir().unwrap()
Expand Down
25 changes: 17 additions & 8 deletions src/book/bookconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl BookConfig {
root: root.to_owned(),
dest: PathBuf::from("book"),
src: PathBuf::from("src"),
indent_spaces: 4, // indentation used for SUMMARY.md
indent_spaces: 4, // indentation used for SUMMARY.md
multilingual: false,
}
}
Expand All @@ -40,7 +40,7 @@ impl BookConfig {
Ok(f) => f,
Err(_) => {
debug!("[*]: Failed to open {:?}", root.join("book.json"));
return self
return self;
},
};

Expand All @@ -49,17 +49,25 @@ impl BookConfig {

// Just return if an error occured.
// I would like to propagate the error, but I have to return `&self`
if let Err(_) = config_file.read_to_string(&mut data) { return self }
if let Err(_) = config_file.read_to_string(&mut data) {
return self;
}

// Convert to JSON
if let Ok(config) = Json::from_str(&data) {
// Extract data

debug!("[*]: Extracting data from config");
// Title, author, description
if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") }
if let Some(a) = config.find_path(&["author"]) { self.author = a.to_string().replace("\"", "") }
if let Some(a) = config.find_path(&["description"]) { self.description = a.to_string().replace("\"", "") }
if let Some(a) = config.find_path(&["title"]) {
self.title = a.to_string().replace("\"", "")
}
if let Some(a) = config.find_path(&["author"]) {
self.author = a.to_string().replace("\"", "")
}
if let Some(a) = config.find_path(&["description"]) {
self.description = a.to_string().replace("\"", "")
}

// Destination
if let Some(a) = config.find_path(&["dest"]) {
Expand All @@ -71,7 +79,9 @@ impl BookConfig {
let dest = self.get_root().join(&dest).to_owned();
self.set_dest(&dest);
},
false => { self.set_dest(&dest); },
false => {
self.set_dest(&dest);
},
}
}
}
Expand Down Expand Up @@ -105,5 +115,4 @@ impl BookConfig {
self.src = src.to_owned();
self
}

}
16 changes: 8 additions & 8 deletions src/book/bookitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct BookItems<'a> {


impl Chapter {

pub fn new(name: String, path: PathBuf) -> Self {

Chapter {
Expand All @@ -40,13 +39,14 @@ impl Chapter {


impl ToJson for Chapter {

fn to_json(&self) -> Json {
let mut m: BTreeMap<String, Json> = BTreeMap::new();
m.insert("name".to_owned(), self.name.to_json());
m.insert("path".to_owned(),self.path.to_str()
.expect("Json conversion failed for path").to_json()
);
m.insert("path".to_owned(),
self.path
.to_str()
.expect("Json conversion failed for path")
.to_json());
m.to_json()
}
}
Expand All @@ -66,7 +66,7 @@ impl<'a> Iterator for BookItems<'a> {
Some((parent_items, parent_idx)) => {
self.items = parent_items;
self.current_index = parent_idx + 1;
}
},
}
} else {
let cur = self.items.get(self.current_index).unwrap();
Expand All @@ -79,10 +79,10 @@ impl<'a> Iterator for BookItems<'a> {
},
BookItem::Spacer => {
self.current_index += 1;
}
},
}

return Some(cur)
return Some(cur);
}
}
}
Expand Down
Loading

0 comments on commit ad0794a

Please sign in to comment.