11use std:: env;
22use std:: path:: { Path , PathBuf } ;
33
4- use clap:: { ArgMatches , Command , arg, crate_version} ;
5- use mdbook:: MDBook ;
4+ use clap:: { arg, crate_version, ArgMatches , Command } ;
65use mdbook:: errors:: Result as Result3 ;
6+ use mdbook:: MDBook ;
77use mdbook_i18n_helpers:: preprocessors:: Gettext ;
88use mdbook_spec:: Spec ;
99use mdbook_trpl_listing:: TrplListing ;
@@ -31,6 +31,20 @@ fn main() {
3131 (Defaults to the current directory when omitted)")
3232 . value_parser ( clap:: value_parser!( PathBuf ) ) ;
3333
34+ // Note: we don't parse this into a `PathBuf` because it is comma separated
35+ // strings *and* we will ultimately pass it into `MDBook::test()`, which
36+ // accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and
37+ // `-L/--library-path` are so close, this is the same set of arguments we
38+ // would pass when invoking mdbook on the CLI, so making them match when
39+ // invoking rustbook makes for good consistency.
40+ let library_path_arg = arg ! (
41+ -L --"library-path"
42+ "A comma-separated list of directories to add to the crate search\n \
43+ path when building tests"
44+ )
45+ . required ( false )
46+ . value_parser ( parse_library_paths) ;
47+
3448 let matches = Command :: new ( "rustbook" )
3549 . about ( "Build a book with mdBook" )
3650 . author ( "Steve Klabnik <[email protected] >" ) @@ -48,7 +62,8 @@ fn main() {
4862 . subcommand (
4963 Command :: new ( "test" )
5064 . about ( "Tests that a book's Rust code samples compile" )
51- . arg ( dir_arg) ,
65+ . arg ( dir_arg)
66+ . arg ( library_path_arg) ,
5267 )
5368 . get_matches ( ) ;
5469
@@ -113,14 +128,22 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
113128
114129fn test ( args : & ArgMatches ) -> Result3 < ( ) > {
115130 let book_dir = get_book_dir ( args) ;
131+ let library_paths = args
132+ . try_get_one :: < Vec < String > > ( "library_path" ) ?
133+ . map ( |v| v. iter ( ) . map ( |s| s. as_str ( ) ) . collect :: < Vec < & str > > ( ) )
134+ . unwrap_or_default ( ) ;
116135 let mut book = load_book ( & book_dir) ?;
117- book. test ( vec ! [ ] )
136+ book. test ( library_paths )
118137}
119138
120139fn get_book_dir ( args : & ArgMatches ) -> PathBuf {
121140 if let Some ( p) = args. get_one :: < PathBuf > ( "dir" ) {
122141 // Check if path is relative from current dir, or absolute...
123- if p. is_relative ( ) { env:: current_dir ( ) . unwrap ( ) . join ( p) } else { p. to_path_buf ( ) }
142+ if p. is_relative ( ) {
143+ env:: current_dir ( ) . unwrap ( ) . join ( p)
144+ } else {
145+ p. to_path_buf ( )
146+ }
124147 } else {
125148 env:: current_dir ( ) . unwrap ( )
126149 }
@@ -132,12 +155,16 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> {
132155 Ok ( book)
133156}
134157
158+ fn parse_library_paths ( input : & str ) -> Result < Vec < String > , String > {
159+ Ok ( input. split ( "," ) . map ( String :: from) . collect ( ) )
160+ }
161+
135162fn handle_error ( error : mdbook:: errors:: Error ) -> ! {
136163 eprintln ! ( "Error: {}" , error) ;
137164
138165 for cause in error. chain ( ) . skip ( 1 ) {
139166 eprintln ! ( "\t Caused By: {}" , cause) ;
140167 }
141168
142- :: std:: process:: exit ( 101 ) ;
169+ std:: process:: exit ( 101 ) ;
143170}
0 commit comments