]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/rustbook/src/main.rs
Ignore unused variable for non-linux builds
[rust.git] / src / tools / rustbook / src / main.rs
index 5a6246347cc030d6d28ff047b3596dcdb5ce8f82..d9b16780107be150448232868a47d4f049f9ba73 100644 (file)
@@ -1,4 +1,5 @@
-//
+#![deny(rust_2018_idioms)]
+
 use clap::{crate_version};
 
 use std::env;
@@ -9,8 +10,18 @@
 use mdbook_1::{MDBook as MDBook1};
 use mdbook_1::errors::{Result as Result1};
 
-use mdbook_2::{MDBook as MDBook2};
-use mdbook_2::errors::{Result as Result2};
+use mdbook::MDBook;
+use mdbook::errors::{Result as Result3};
+
+#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
+use mdbook::renderer::RenderContext;
+
+#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
+use mdbook_linkcheck::{self, errors::BrokenLinks};
+use failure::Error;
+
+#[cfg(not(all(target_arch = "x86_64", target_os = "linux")))]
+use failure::bail;
 
 fn main() {
     let d_message = "-d, --dest-dir=[dest-dir]
@@ -30,6 +41,9 @@ fn main() {
                         .arg_from_usage(d_message)
                         .arg_from_usage(dir_message)
                         .arg_from_usage(vers_message))
+                    .subcommand(SubCommand::with_name("linkcheck")
+                        .about("Run linkcheck with mdBook 3")
+                        .arg_from_usage(dir_message))
                     .get_matches();
 
     // Check which subcomamnd the user ran...
@@ -47,8 +61,8 @@ fn main() {
                         ::std::process::exit(101);
                     }
                 }
-                Some("2") => {
-                    if let Err(e) = build_2(sub_matches) {
+                Some("2") | Some("3") => {
+                    if let Err(e) = build(sub_matches) {
                         eprintln!("Error: {}", e);
 
                         for cause in e.iter().skip(1) {
@@ -59,16 +73,47 @@ fn main() {
                     }
                 }
                 _ => {
-                    panic!("Invalid mdBook version! Select '1' or '2'");
+                    panic!("Invalid mdBook version! Select '1' or '2' or '3'");
                 }
             };
         },
+        ("linkcheck", Some(sub_matches)) => {
+            if let Err(err) = linkcheck(sub_matches) {
+                eprintln!("Error: {}", err);
+
+                #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
+                {
+                    if let Ok(broken_links) = err.downcast::<BrokenLinks>() {
+                        for cause in broken_links.links().iter() {
+                            eprintln!("\tCaused By: {}", cause);
+                        }
+                    }
+                }
+
+                ::std::process::exit(101);
+            }
+        },
         (_, _) => unreachable!(),
     };
 }
 
+#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
+pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> {
+    let book_dir = get_book_dir(args);
+    let book = MDBook::load(&book_dir).unwrap();
+    let cfg = book.config;
+    let render_ctx = RenderContext::new(&book_dir, book.book, cfg, &book_dir);
+
+    mdbook_linkcheck::check_links(&render_ctx)
+}
+
+#[cfg(not(all(target_arch = "x86_64", target_os = "linux")))]
+pub fn linkcheck(_args: &ArgMatches<'_>) -> Result<(), Error> {
+    bail!("mdbook-linkcheck only works on x86_64 linux targets.");
+}
+
 // Build command implementation
-pub fn build_1(args: &ArgMatches) -> Result1<()> {
+pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
     let book_dir = get_book_dir(args);
     let mut book = MDBook1::load(&book_dir)?;
 
@@ -85,9 +130,9 @@ pub fn build_1(args: &ArgMatches) -> Result1<()> {
 }
 
 // Build command implementation
-pub fn build_2(args: &ArgMatches) -> Result2<()> {
+pub fn build(args: &ArgMatches<'_>) -> Result3<()> {
     let book_dir = get_book_dir(args);
-    let mut book = MDBook2::load(&book_dir)?;
+    let mut book = MDBook::load(&book_dir)?;
 
     // Set this to allow us to catch bugs in advance.
     book.config.build.create_missing = false;
@@ -101,7 +146,7 @@ pub fn build_2(args: &ArgMatches) -> Result2<()> {
     Ok(())
 }
 
-fn get_book_dir(args: &ArgMatches) -> PathBuf {
+fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
     if let Some(dir) = args.value_of("dir") {
         // Check if path is relative from current dir, or absolute...
         let p = Path::new(dir);