diff options
| author | 2017-09-05 19:54:15 +0200 | |
|---|---|---|
| committer | 2017-09-05 19:54:15 +0200 | |
| commit | 41a41579acbdfb3f85455edb65ff82d9ee24bf1b (patch) | |
| tree | 36a27079b947652d3bb96f436662aab66c58eeb3 /askama_shared/src | |
| parent | e850b1e8050a2faa3eb9e84670c466e227aa3bdd (diff) | |
| download | askama-41a41579acbdfb3f85455edb65ff82d9ee24bf1b.tar.gz askama-41a41579acbdfb3f85455edb65ff82d9ee24bf1b.tar.bz2 askama-41a41579acbdfb3f85455edb65ff82d9ee24bf1b.zip  | |
Create better abstraction around TemplateMeta type
Diffstat (limited to '')
| -rw-r--r-- | askama_shared/src/lib.rs | 96 | 
1 files changed, 48 insertions, 48 deletions
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index ac88de0..0444a18 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -31,7 +31,7 @@ use std::path::PathBuf;  /// the parse tree and/or generated source according to the `print` key's  /// value as passed to the `template()` attribute.  pub fn build_template(ast: &syn::DeriveInput) -> String { -    let meta = get_template_meta(ast); +    let meta = TemplateMeta::new(ast);      let (path, src) = match meta.source {          Source::Source(s) => (PathBuf::new(), Cow::Borrowed(s)),          Source::Path(s) => { @@ -51,53 +51,6 @@ pub fn build_template(ast: &syn::DeriveInput) -> String {      code  } -// Returns a `TemplateMeta` based on the `template()` attribute data found -// in the parsed struct or enum. Will panic if it does not find the required -// template path, or if the `print` key has an unexpected value. -fn get_template_meta<'a>(ast: &'a syn::DeriveInput) -> TemplateMeta<'a> { -    let attr = ast.attrs.iter().find(|a| a.name() == "template"); -    if attr.is_none() { -        let msg = format!("'template' attribute not found on struct '{}'", -                          ast.ident.as_ref()); -        panic!(msg); -    } - -    let attr = attr.unwrap(); -    let mut source = None; -    let mut print = Print::None; -    if let syn::MetaItem::List(_, ref inner) = attr.value { -        for nm_item in inner { -            if let syn::NestedMetaItem::MetaItem(ref item) = *nm_item { -                if let syn::MetaItem::NameValue(ref key, ref val) = *item { -                    match key.as_ref() { -                        "path" => if let syn::Lit::Str(ref s, _) = *val { -                            source = Some(Source::Path(s.as_ref())); -                        } else { -                            panic!("template path must be string literal"); -                        }, -                        "source" => if let syn::Lit::Str(ref s, _) = *val { -                            source = Some(Source::Source(s.as_ref())); -                        } else { -                            panic!("template source must be string literal"); -                        }, -                        "print" => if let syn::Lit::Str(ref s, _) = *val { -                            print = s.into(); -                        } else { -                            panic!("print value must be string literal"); -                        }, -                        _ => { panic!("unsupported annotation key found") } -                    } -                } -            } -        } -    } - -    match source { -        Some(s) => TemplateMeta { source: s, print }, -        None => panic!("template path or source not found in struct attributes"), -    } -} -  mod errors {      error_chain! {          foreign_links { @@ -113,6 +66,53 @@ struct TemplateMeta<'a> {      print: Print,  } +impl<'a> TemplateMeta<'a> { +    fn new(ast: &'a syn::DeriveInput) -> TemplateMeta<'a> { +        let attr = ast.attrs.iter().find(|a| a.name() == "template"); +        if attr.is_none() { +            let msg = format!("'template' attribute not found on struct '{}'", +                              ast.ident.as_ref()); +            panic!(msg); +        } + +        let attr = attr.unwrap(); +        let mut source = None; +        let mut print = Print::None; +        if let syn::MetaItem::List(_, ref inner) = attr.value { +            for nm_item in inner { +                if let syn::NestedMetaItem::MetaItem(ref item) = *nm_item { +                    if let syn::MetaItem::NameValue(ref key, ref val) = *item { +                        match key.as_ref() { +                            "path" => if let syn::Lit::Str(ref s, _) = *val { +                                source = Some(Source::Path(s.as_ref())); +                            } else { +                                panic!("template path must be string literal"); +                            }, +                            "source" => if let syn::Lit::Str(ref s, _) = *val { +                                source = Some(Source::Source(s.as_ref())); +                            } else { +                                panic!("template source must be string literal"); +                            }, +                            "print" => if let syn::Lit::Str(ref s, _) = *val { +                                print = s.into(); +                            } else { +                                panic!("print value must be string literal"); +                            }, +                            _ => { panic!("unsupported annotation key found") } +                        } +                    } +                } +            } +        } + +        match source { +            Some(s) => TemplateMeta { source: s, print }, +            None => panic!("template path or source not found in struct attributes"), +        } +    } +} + +  enum Source<'a> {      Path(&'a str),      Source(&'a str),  | 
