-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Every code example is now fully runnable and testable. As a result, all examples are now tested and include imports. Relevant imports are shown by default. Code examples can be expanded to show all imports. Fixes #432.
- Loading branch information
1 parent
ee1a990
commit 74113c3
Showing
17 changed files
with
780 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::path::Path; | ||
use std::error::Error; | ||
|
||
use proc_macro::TokenStream; | ||
use devise::{syn::{self, Ident, LitStr}, Result}; | ||
|
||
use crate::syn_ext::syn_to_diag; | ||
use crate::proc_macro2::TokenStream as TokenStream2; | ||
|
||
pub fn _macro(input: TokenStream) -> Result<TokenStream> { | ||
let root = syn::parse::<LitStr>(input.into()).map_err(syn_to_diag)?; | ||
let modules = entry_to_modules(&root) | ||
.map_err(|e| root.span().unstable().error(format!("failed to read: {}", e)))?; | ||
|
||
Ok(quote_spanned!(root.span() => | ||
#[allow(dead_code)] | ||
#[allow(non_camel_case_types)] | ||
mod test_site_guide { #(#modules)* } | ||
).into()) | ||
} | ||
|
||
fn entry_to_modules(pat: &LitStr) -> std::result::Result<Vec<TokenStream2>, Box<dyn Error>> { | ||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("MANIFEST_DIR"); | ||
let full_pat = Path::new(&manifest_dir).join(&pat.value()).display().to_string(); | ||
|
||
let mut modules = vec![]; | ||
for path in glob::glob(&full_pat).map_err(|e| Box::new(e))? { | ||
let path = path.map_err(|e| Box::new(e))?; | ||
let name = path.file_name() | ||
.and_then(|f| f.to_str()) | ||
.map(|name| name.trim_matches(|c| char::is_numeric(c) || c == '-') | ||
.replace('-', "_") | ||
.replace('.', "_")) | ||
.ok_or_else(|| "invalid file name".to_string())?; | ||
|
||
let ident = Ident::new(&name, pat.span()); | ||
let full_path = Path::new(&manifest_dir).join(&path).display().to_string(); | ||
modules.push(quote_spanned!(pat.span() => | ||
#[doc(include = #full_path)] | ||
struct #ident; | ||
)) | ||
} | ||
|
||
Ok(modules) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![feature(proc_macro_hygiene)] | ||
#![feature(external_doc)] | ||
|
||
#[allow(dead_code)] | ||
mod test_guide { | ||
#[doc(include = "../../../site/guide/2-getting-started.md")] | ||
pub struct GuideGettingStart; | ||
|
||
/// ```rust | ||
/// assert_eq!(0, 1); | ||
/// ``` | ||
struct Foo; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.