-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Join Vector<Html<T>> with separator #85
Comments
Extracting the Html call outside the map and operate on the complete string instead works as long as none of the variables contains data that could be interpreted as html tags
|
Here's a bunch of code that may be useful. I'm thinking about adding something like this use crate::templates::{Html, ToHtml};
struct HtmlJoiner<
Item: ToHtml,
Items: Iterator<Item = Item> + Clone,
Sep: ToHtml,
> {
items: Items,
sep: Sep,
}
impl<Item: ToHtml, Items: Iterator<Item = Item> + Clone, Sep: ToHtml> ToHtml
for HtmlJoiner<Item, Items, Sep>
{
fn to_html(&self, out: &mut dyn Write) -> io::Result<()> {
let mut iter = self.items.clone();
if let Some(first) = iter.next() {
first.to_html(out)?;
} else {
return Ok(());
}
for item in iter {
self.sep.to_html(out)?;
item.to_html(out)?;
}
Ok(())
}
}
fn to_buffer(content: impl ToHtml) -> Result<Vec<u8>, io::Error> {
let mut buf = Vec::new();
content.to_html(&mut buf)?;
Ok(buf)
}
#[test]
fn test_html_joiner() {
assert_eq!(
to_buffer(HtmlJoiner {
items: ["foo", "b<a", "baz"].iter(),
sep: ", ",
})
.unwrap(),
b"foo, b<a, baz"
)
}
#[test]
fn test_html_joiner_empty() {
use std::iter::empty;
assert_eq!(
to_buffer(HtmlJoiner {
items: empty::<&str>(),
sep: ", ",
})
.unwrap(),
b""
)
}
#[test]
fn test_html_joiner_mark() {
assert_eq!(
to_buffer(HtmlJoiner {
items: ["foo", "b<a", "baz"].iter(),
sep: Html("<br/>"),
})
.unwrap(),
b"foo<br/>b<a<br/>baz"
)
} In theory, it should be possible to provide a function users.into_iter()
.map(|u| Html(format!("<a href=\"example.com/{}\">{}</a>", u.name, u.name)))
.join_html(", ")
.to_html(&mut out)?; If the html snippet is written inside a separate users.into_iter()
.join_html(user_template, ", ")
.to_html(&mut out)?; Ideally, the syntax used should also be convenient in a template, where it should be possible to provide user_template inline, though I'm not sure about the parameter passing then. In summary; yes, this is an area where ructe could use some improvement. :-) |
I went ahead and wrote a PR. Could you, @OskarPersson , check if it (the branch |
How can I join a vector of html strings with a custom separator? With for exampe
Vec<String>
I would usejoin
but this isn't available forHtml
. This is what I've tried:The text was updated successfully, but these errors were encountered: