]> git.proxmox.com Git - rustc.git/blame - src/tools/rustbook/src/main.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / tools / rustbook / src / main.rs
CommitLineData
e1599b0c 1use clap::crate_version;
8bb4bdeb
XL
2
3use std::env;
8bb4bdeb
XL
4use std::path::{Path, PathBuf};
5
e1599b0c 6use clap::{App, AppSettings, ArgMatches, SubCommand};
8bb4bdeb 7
e1599b0c 8use mdbook::errors::Result as Result3;
dc9dc135 9use mdbook::MDBook;
416331ca 10
8bb4bdeb
XL
11fn main() {
12 let d_message = "-d, --dest-dir=[dest-dir]
13'The output directory for your book{n}(Defaults to ./book when omitted)'";
14 let dir_message = "[dir]
15'A directory for your book{n}(Defaults to Current Directory when omitted)'";
16
17 let matches = App::new("rustbook")
e1599b0c
XL
18 .about("Build a book with mdBook")
19 .author("Steve Klabnik <steve@steveklabnik.com>")
20 .version(&*format!("v{}", crate_version!()))
21 .setting(AppSettings::SubcommandRequired)
22 .subcommand(
23 SubCommand::with_name("build")
24 .about("Build the book from the markdown files")
25 .arg_from_usage(d_message)
26 .arg_from_usage(dir_message),
27 )
74b04a01
XL
28 .subcommand(
29 SubCommand::with_name("test")
30 .about("Tests that a book's Rust code samples compile")
31 .arg_from_usage(dir_message),
32 )
e1599b0c 33 .get_matches();
8bb4bdeb
XL
34
35 // Check which subcomamnd the user ran...
9fa01778
XL
36 match matches.subcommand() {
37 ("build", Some(sub_matches)) => {
416331ca 38 if let Err(e) = build(sub_matches) {
74b04a01
XL
39 handle_error(e);
40 }
41 }
42 ("test", Some(sub_matches)) => {
43 if let Err(e) = test(sub_matches) {
44 handle_error(e);
416331ca 45 }
e1599b0c 46 }
8bb4bdeb
XL
47 (_, _) => unreachable!(),
48 };
9fa01778 49}
8bb4bdeb 50
8bb4bdeb 51// Build command implementation
416331ca 52pub fn build(args: &ArgMatches<'_>) -> Result3<()> {
ea8adc8c 53 let book_dir = get_book_dir(args);
f035d41b 54 let mut book = load_book(&book_dir)?;
8bb4bdeb 55
2c00a5a8
XL
56 // Set this to allow us to catch bugs in advance.
57 book.config.build.create_missing = false;
58
59 if let Some(dest_dir) = args.value_of("dest-dir") {
60 book.config.build.build_dir = PathBuf::from(dest_dir);
61 }
8bb4bdeb 62
ea8adc8c 63 book.build()?;
8bb4bdeb
XL
64
65 Ok(())
66}
67
74b04a01
XL
68fn test(args: &ArgMatches<'_>) -> Result3<()> {
69 let book_dir = get_book_dir(args);
f035d41b 70 let mut book = load_book(&book_dir)?;
74b04a01
XL
71 book.test(vec![])
72}
73
532ac7d7 74fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
8bb4bdeb
XL
75 if let Some(dir) = args.value_of("dir") {
76 // Check if path is relative from current dir, or absolute...
77 let p = Path::new(dir);
e74abb32 78 if p.is_relative() { env::current_dir().unwrap().join(dir) } else { p.to_path_buf() }
8bb4bdeb
XL
79 } else {
80 env::current_dir().unwrap()
81 }
82}
74b04a01 83
f035d41b
XL
84fn load_book(book_dir: &Path) -> Result3<MDBook> {
85 let mut book = MDBook::load(book_dir)?;
86 book.config.set("output.html.input-404", "").unwrap();
87 Ok(book)
88}
89
74b04a01
XL
90fn handle_error(error: mdbook::errors::Error) -> ! {
91 eprintln!("Error: {}", error);
92
f035d41b 93 for cause in error.chain().skip(1) {
74b04a01
XL
94 eprintln!("\tCaused By: {}", cause);
95 }
96
97 ::std::process::exit(101);
98}