diff --git a/Cargo.lock b/Cargo.lock index 67d44569..6f8cafa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -534,7 +534,7 @@ dependencies = [ "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "staticfile 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.3 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "ws 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1209,7 +1209,7 @@ dependencies = [ [[package]] name = "toml" -version = "0.4.6" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "serde 1.0.72 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1224,7 +1224,7 @@ dependencies = [ "is-match 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1558,7 +1558,7 @@ dependencies = [ "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" -"checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9" +"checksum toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4a2ecc31b0351ea18b3fe11274b8db6e4d82bce861bbb22e6dbed40417902c65" "checksum toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab234a943a2363ad774020e2f9474a38a85bc4396bace01a96380144aef17db3" "checksum traitobject 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" "checksum typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1410f6f91f21d1612654e7cc69193b0334f909dcf2c790c4826254fbb86f8887" diff --git a/Cargo.toml b/Cargo.toml index d47f682e..1e0d1447 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ pulldown-cmark = "0.1.2" lazy_static = "1.0" log = "0.4" env_logger = "0.5" -toml = "0.4" +toml = "0.4.8" memchr = "2.0" open = "1.1" regex = "1.0.0" diff --git a/src/preprocess/cmd.rs b/src/preprocess/cmd.rs index 4398eba6..59b0cdd3 100644 --- a/src/preprocess/cmd.rs +++ b/src/preprocess/cmd.rs @@ -65,9 +65,13 @@ impl CmdPreprocessor { } } - fn write_input(&self, writer: W, book: &Book, ctx: &PreprocessorContext) -> Result<()> { - serde_json::to_writer(writer, &(ctx, book)) - .map_err(Into::into) + fn write_input( + &self, + writer: W, + book: &Book, + ctx: &PreprocessorContext, + ) -> Result<()> { + serde_json::to_writer(writer, &(ctx, book)).map_err(Into::into) } /// The command this `Preprocessor` will invoke. @@ -105,7 +109,12 @@ impl Preprocessor for CmdPreprocessor { .stdout(Stdio::piped()) .stderr(Stdio::inherit()) .spawn() - .chain_err(|| format!("Unable to start the \"{}\" preprocessor. Is it installed?", self.name()))?; + .chain_err(|| { + format!( + "Unable to start the \"{}\" preprocessor. Is it installed?", + self.name() + ) + })?; self.write_input_to_child(&mut child, &book, ctx); @@ -114,13 +123,21 @@ impl Preprocessor for CmdPreprocessor { .chain_err(|| "Error waiting for the preprocessor to complete")?; trace!("{} exited with output: {:?}", self.cmd, output); - ensure!(output.status.success(), "The preprocessor exited unsuccessfully"); + ensure!( + output.status.success(), + "The preprocessor exited unsuccessfully" + ); - serde_json::from_slice(&output.stdout).chain_err(|| "Unable to parse the preprocessed book") + serde_json::from_slice(&output.stdout) + .chain_err(|| "Unable to parse the preprocessed book") } fn supports_renderer(&self, renderer: &str) -> bool { - debug!("Checking if the \"{}\" preprocessor supports \"{}\"", self.name(), renderer); + debug!( + "Checking if the \"{}\" preprocessor supports \"{}\"", + self.name(), + renderer + ); let mut cmd = match self.command() { Ok(c) => c, @@ -152,3 +169,36 @@ impl Preprocessor for CmdPreprocessor { outcome.unwrap_or(false) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::Path; + use MDBook; + + fn book_example() -> MDBook { + let example = + Path::new(env!("CARGO_MANIFEST_DIR")).join("book-example"); + MDBook::load(example).unwrap() + } + + #[test] + fn round_trip_write_and_parse_input() { + let cmd = CmdPreprocessor::new("test".to_string(), "test".to_string()); + let md = book_example(); + let ctx = PreprocessorContext::new( + md.root.clone(), + md.config.clone(), + "some-renderer".to_string(), + ); + + let mut buffer = Vec::new(); + cmd.write_input(&mut buffer, &md.book, &ctx).unwrap(); + + let (got_ctx, got_book) = + CmdPreprocessor::parse_input(buffer.as_slice()).unwrap(); + + assert_eq!(got_book, md.book); + assert_eq!(got_ctx, ctx); + } +}