]> git.lizzy.rs Git - rust.git/blob - src/tools/rustbook/src/main.rs
Rollup merge of #58204 - estebank:impl-trait-semi, r=zackmdavis
[rust.git] / src / tools / rustbook / src / main.rs
1 #![deny(rust_2018_idioms)]
2
3 use clap::{crate_version};
4
5 use std::env;
6 use std::path::{Path, PathBuf};
7
8 use clap::{App, ArgMatches, SubCommand, AppSettings};
9
10 use mdbook_1::{MDBook as MDBook1};
11 use mdbook_1::errors::{Result as Result1};
12
13 use mdbook_2::{MDBook as MDBook2};
14 use mdbook_2::errors::{Result as Result2};
15
16 fn main() {
17     let d_message = "-d, --dest-dir=[dest-dir]
18 'The output directory for your book{n}(Defaults to ./book when omitted)'";
19     let dir_message = "[dir]
20 'A directory for your book{n}(Defaults to Current Directory when omitted)'";
21     let vers_message = "-m, --mdbook-vers=[md-version]
22 'The version of mdbook to use for your book{n}(Defaults to 1 when omitted)'";
23
24     let matches = App::new("rustbook")
25                     .about("Build a book with mdBook")
26                     .author("Steve Klabnik <steve@steveklabnik.com>")
27                     .version(&*format!("v{}", crate_version!()))
28                     .setting(AppSettings::SubcommandRequired)
29                     .subcommand(SubCommand::with_name("build")
30                         .about("Build the book from the markdown files")
31                         .arg_from_usage(d_message)
32                         .arg_from_usage(dir_message)
33                         .arg_from_usage(vers_message))
34                     .get_matches();
35
36     // Check which subcomamnd the user ran...
37     match matches.subcommand() {
38         ("build", Some(sub_matches)) => {
39             match sub_matches.value_of("mdbook-vers") {
40                 None | Some("1") => {
41                     if let Err(e) = build_1(sub_matches) {
42                         eprintln!("Error: {}", e);
43
44                         for cause in e.iter().skip(1) {
45                             eprintln!("\tCaused By: {}", cause);
46                         }
47
48                         ::std::process::exit(101);
49                     }
50                 }
51                 Some("2") => {
52                     if let Err(e) = build_2(sub_matches) {
53                         eprintln!("Error: {}", e);
54
55                         for cause in e.iter().skip(1) {
56                             eprintln!("\tCaused By: {}", cause);
57                         }
58
59                         ::std::process::exit(101);
60                     }
61                 }
62                 _ => {
63                     panic!("Invalid mdBook version! Select '1' or '2'");
64                 }
65             };
66         },
67         (_, _) => unreachable!(),
68     };
69 }
70
71 // Build command implementation
72 pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
73     let book_dir = get_book_dir(args);
74     let mut book = MDBook1::load(&book_dir)?;
75
76     // Set this to allow us to catch bugs in advance.
77     book.config.build.create_missing = false;
78
79     if let Some(dest_dir) = args.value_of("dest-dir") {
80         book.config.build.build_dir = PathBuf::from(dest_dir);
81     }
82
83     book.build()?;
84
85     Ok(())
86 }
87
88 // Build command implementation
89 pub fn build_2(args: &ArgMatches<'_>) -> Result2<()> {
90     let book_dir = get_book_dir(args);
91     let mut book = MDBook2::load(&book_dir)?;
92
93     // Set this to allow us to catch bugs in advance.
94     book.config.build.create_missing = false;
95
96     if let Some(dest_dir) = args.value_of("dest-dir") {
97         book.config.build.build_dir = PathBuf::from(dest_dir);
98     }
99
100     book.build()?;
101
102     Ok(())
103 }
104
105 fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
106     if let Some(dir) = args.value_of("dir") {
107         // Check if path is relative from current dir, or absolute...
108         let p = Path::new(dir);
109         if p.is_relative() {
110             env::current_dir().unwrap().join(dir)
111         } else {
112             p.to_path_buf()
113         }
114     } else {
115         env::current_dir().unwrap()
116     }
117 }