h1. Macros for including file content
Macros like @include_textile!("../README.textile", "example")@ allow you to include incomplete code from Textile code blocks.
Though Rust doc tests let you hide setup code from being rendered, you cannot do the same when rendering Textile.
You can demonstrate just the code you want in Textile while maintaining the benefit of compiling it in tests.
h2. Examples
The @include_textile!()@ macro resolves a file path relative to the directory containing the crate @Cargo.toml@ manifest file.
Consider a crate @README.textile@ with the following content:
bc.. The @example()@ function returns a model that implements @Debug@ so you can easily print it:
bc(#example)[rust]. let m = example()?;
assert_eq!(format!("{m:?}"), r#"Model { name: "example" }"#);
p. We didn't define the @example()@ function nor the type of @m@.
In Rust doc tests you could do so with lines prefaced with @#@, but those lines would render in a textile file.
Instead, we could use @include_textile!("../README.textile", "example")@ to include the example content from @README.textile@ above in a test to make sure it compiles and even runs.
bc[rust].. #[derive(Debug)]
struct Model {
name: String,
}
fn example() -> Result<Model, Box<dyn std::error::Error>> {
Ok(Model {
name: "example".into(),
})
}
#[test]
fn test_example() -> Result<(), Box<dyn std::error::Error>> {
include_textile!("../README.textile", "example");
Ok(())
}
h2. License
Licensed under the "MIT":../LICENSE.txt license.