-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mark Van de Vyver <[email protected]>
- Loading branch information
1 parent
75c91b0
commit c761e1a
Showing
2 changed files
with
71 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,71 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::{parse_quote, Stmt}; | ||
|
||
use crate::trace::lower::Assertion; | ||
|
||
//pub type Rust = proc_macro2::TokenStream; | ||
|
||
use crate::trace::lower::Quotable; | ||
use crate::trace::lower::Quotables; | ||
|
||
// Generate TokenStream to be returned by the proc-macro | ||
// | ||
// The "Lower" stage is responsible for turning the "Model" data into a | ||
// syn::Item that can be reported here. | ||
pub fn generate(quotes: Quotables<Quotable>) -> proc_macro2::TokenStream { | ||
// Have a logic check earlier to error if there is not **at least one** | ||
// `syn::Item` | ||
#[allow(clippy::collapsible_match)] | ||
#[allow(unreachable_patterns)] | ||
let it = match quotes.get(0).expect("An item") { | ||
Quotable::Item(item) => match item { | ||
syn::Item::Fn(itemf) => Some(itemf), | ||
_ => None, | ||
}, | ||
_ => None, //? | ||
}; | ||
let syn::ItemFn { | ||
attrs, | ||
vis, | ||
sig, | ||
block, | ||
} = it.expect("An item variant"); | ||
|
||
quote!( | ||
#(#attrs)* | ||
#vis #sig { | ||
#block | ||
} | ||
) | ||
} | ||
|
||
impl ToTokens for Assertion { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
let Assertion { expr, message } = self; | ||
let stmt: Stmt = parse_quote!(assert!(#expr, #message);); | ||
stmt.to_tokens(tokens); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn fn_in_fn_out() { | ||
let models = crate::trace::analyze( | ||
parse_quote!(#[trace]), | ||
parse_quote!( | ||
fn f(x: bool) {} | ||
), | ||
); | ||
println!("{:?}", models.clone()); | ||
let ir = crate::trace::lower(models); | ||
println!("{:?}", ir.clone()); | ||
let rust = crate::trace::generate(ir); | ||
println!("{:?}", rust.clone()); | ||
|
||
assert!(syn::parse2::<syn::ItemFn>(rust).is_ok()); | ||
} | ||
} |