use std::str::FromStr;
use quote::{ToTokens, Tokens};
use syn;
use syn::synom::Parser;
#[derive(Debug, Clone)]
pub struct Block(Vec<syn::Stmt>);
impl Default for Block {
fn default() -> Self {
"".parse().unwrap()
}
}
impl ToTokens for Block {
fn to_tokens(&self, tokens: &mut Tokens) {
let inner = &self.0;
tokens.append_all(quote!(
{ #( #inner )* }
));
}
}
impl FromStr for Block {
type Err = String;
fn from_str(expr: &str) -> Result<Self, Self::Err> {
named!(block_contents -> Vec<syn::Stmt>, call!(syn::Block::parse_within));
Ok(Block(
block_contents
.parse_str(expr)
.map_err(|e| format!("{}", e))?,
))
}
}
#[cfg(test)]
mod test {
#[allow(unused_imports)]
use super::*;
#[test]
#[should_panic(
expected = r#"called `Result::unwrap()` on an `Err` value: "error while lexing input string""#
)]
fn block_invalid_token_trees() {
Block::from_str("let x = 2; { x+1").unwrap();
}
#[test]
fn block_delimited_token_tree() {
let expr = Block::from_str("let x = 2; { x+1 }").unwrap();
assert_eq!(
quote!(#expr),
quote!({
let x = 2;
{
x + 1
}
})
);
}
#[test]
fn block_single_token_tree() {
let expr = Block::from_str("42").unwrap();
assert_eq!(quote!(#expr), quote!({ 42 }));
}
}