Merge handling of trailing newline into get_template_source()

This commit is contained in:
Dirkjan Ochtman 2017-08-06 14:51:56 +02:00
parent 05f11a6843
commit b9cb5b82e6
3 changed files with 9 additions and 15 deletions

View File

@ -66,10 +66,7 @@ fn get_template_meta(ast: &syn::DeriveInput) -> TemplateMeta {
/// value as passed to the `template()` attribute. /// value as passed to the `template()` attribute.
fn build_template(ast: &syn::DeriveInput) -> String { fn build_template(ast: &syn::DeriveInput) -> String {
let meta = get_template_meta(ast); let meta = get_template_meta(ast);
let mut src = path::get_template_source(&meta.path); let src = path::get_template_source(&meta.path);
if src.ends_with('\n') {
let _ = src.pop();
}
let nodes = parser::parse(&src); let nodes = parser::parse(&src);
if meta.print == "ast" || meta.print == "all" { if meta.print == "ast" || meta.print == "all" {
println!("{:?}", nodes); println!("{:?}", nodes);

View File

@ -323,16 +323,10 @@ named!(block_include<Node>, do_parse!(
ws!(tag_s!("include")) >> ws!(tag_s!("include")) >>
name: ws!(expr_str_lit) >> name: ws!(expr_str_lit) >>
nws: opt!(tag_s!("-")) >> nws: opt!(tag_s!("-")) >>
({ (Node::Include(WS(pws.is_some(), nws.is_some()), match name {
let mut src = match name { Expr::StrLit(s) => path::get_template_source(s),
Expr::StrLit(s) => path::get_template_source(s), _ => panic!("include path must be a string literal"),
_ => panic!("include path must be a string literal"), }))
};
if src.ends_with('\n') {
let _ = src.pop();
}
Node::Include(WS(pws.is_some(), nws.is_some()), src)
})
)); ));
named!(block_node<Node>, do_parse!( named!(block_node<Node>, do_parse!(

View File

@ -46,6 +46,9 @@ pub fn get_template_source(tpl_file: &str) -> String {
}; };
let mut s = String::new(); let mut s = String::new();
f.read_to_string(&mut s).unwrap(); f.read_to_string(&mut s).unwrap();
if s.ends_with('\n') {
let _ = s.pop();
}
s s
} }
@ -56,7 +59,7 @@ mod tests {
#[test] #[test]
fn get_source() { fn get_source() {
assert_eq!(get_template_source("sub/b.html"), "bar\n"); assert_eq!(get_template_source("sub/b.html"), "bar");
} }
#[test] #[test]