Open livingsilver94 opened 1 year ago
What does the relevant template code look like?
Tree:
[Lit(Lit { lws: "", val: "typedef enum\n{", rws: "\n " }), Loop(Loop { ws1: Ws(None, None), var: Name("entry"), iter: Var("entries"), cond: None, body: [Lit(Lit { lws: "\n ", val: "SWM_CANPDU_", rws: "" }), Expr(Ws(Some(Suppress), Some(Suppress)), Var("entry")), Lit(Lit { lws: "", val: ",", rws: "\n " })], ws2: Ws(None, None), else_nodes: [], ws3: Ws(None, None) }), Lit(Lit { lws: "\n ", val: "", rws: "" }), Comment(Comment { ws: Ws(None, None), content: "{#" }), Lit(Lit { lws: "\n ", val: "SWM_CANPDU_", rws: "" }), Expr(Ws(Some(Suppress), Some(Suppress)), Var("count_prefix")), Lit(Lit { lws: "", val: "_NUMBER_ID,\n}\nSWM_CANPDU_", rws: "" }), Expr(Ws(Some(Suppress), Some(Suppress)), Var("object_name")), Lit(Lit { lws: "", val: "List_priv_t;", rws: "" })]
Code:
impl < 'a, E : Iterator < Item = String > > ::askama::Template for Enum< 'a, E > {
fn render_into(&self, writer: &mut (impl ::std::fmt::Write + ?Sized)) -> ::askama::Result<()> {
include_bytes! ("/home/path/lib/templates/enum.c.j2") ;
writer.write_str("typedef enum\n{\n ")?;
{
let mut _did_loop = false;
let _iter = (&self.entries).into_iter();
for (entry, _loop_item) in ::askama::helpers::TemplateLoop::new(_iter) {
_did_loop = true;
::std::write!(
writer,
"\n SWM_CANPDU_{expr0},\n ",
expr0 = &::askama::MarkupDisplay::new_unsafe(&(entry), ::askama::Html),
)?;
}
if !_did_loop {
}
}
::std::write!(
writer,
"\n \n SWM_CANPDU_{expr1}_NUMBER_ID,\n}}\nSWM_CANPDU_{expr2}List_priv_t;",
expr1 = &::askama::MarkupDisplay::new_unsafe(&(self.count_prefix), ::askama::Html),
expr2 = &::askama::MarkupDisplay::new_unsafe(&(self.object_name), ::askama::Html),
)?;
::askama::Result::Ok(())
}
const EXTENSION: ::std::option::Option<&'static ::std::primitive::str> =
Some("c")
;
const SIZE_HINT: ::std::primitive::usize =
126
;
const MIME_TYPE: &'static ::std::primitive::str =
"text/plain; charset=utf-8"
;
}
impl < 'a, E : Iterator < Item = String > > ::std::fmt::Display for Enum< 'a, E > {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::askama::Template::render_into(self, f).map_err(|_| ::std::fmt::Error {})
}
}
I just mean the contents of enum.c.j2
.
Oh sorry about that. Here it is:
typedef enum
{
{% for entry in entries %}
SWM_CANPDU_{{- entry -}},
{% endfor %}
{# The last entry of the enum is the entry count. #}
SWM_CANPDU_{{- count_prefix -}}_NUMBER_ID,
}
SWM_CANPDU_{{- object_name -}}List_priv_t;
I guess I could have {% for entry in entries.clone() %}
in the template, since cloning std::iter::Map
is cheap given its 32-bit size. I'm open to more elegant solutions though, if any.
The notion of an &impl Iterator
itself feels fairly confused, since you need a &mut
reference to actually iterate. One thing that could work is to have the field be more like messages: &[Message<'a>]
and then expose an iter(&self)
method on Enum
which yields the owned iterator, for use in the template.
I need to compute the string values out of a slice of Messages by filtering and mapping. At the end of the chain, I have an
std::iter::Map<...>
which can be seen as animpl Iterator<Item = String>
. At this point I cannot figure out a way to pass this iterator to a Template to loop over it:The compiler returns:
Is there any way to do this, or am I forced to compute the entire list up front?