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
Change write_to_html
to allow fmt::Write
#870
Change write_to_html
to allow fmt::Write
#870
Conversation
This must be reviewed and analyzed, but yes, it is a breaking change, and yes, it means an increment of the version number and indeed it is planned for the 0.11 milestone (#492). Also, this pull request should be created against the branch 'branch_0.11', that is the branch where all breaking changes for the 0.11 version are being merged. Thanks! |
56fcf0e
to
4a79f63
Compare
4a79f63
to
42b8274
Compare
Interesting approach, it seems the only inconvenience is having to manually create a IoWrapper or a FmtWrapper... And I think it could be solved implementing |
Do you mean
Fixed and rebased. |
42b8274
to
081a4d2
Compare
I see, so this seems OK for me, but I would like considering other options and the opinion from other reviewers before merging it. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API-wise I definitely prefer the idea of just having separate entry functions write_html_io/fmt
. Having to use the name write_html_fmt
for strings (the most common case) is a little obtuse. Maybe we could keep push_html
anyway. Although it is technically redundant, the name is a bit clearer and keeping it would reduce breakage.
Internally using a trait with an associated error type for writing looks good to me.
Co-authored-by: Roope Salmi <rpsalmi@gmail.com>
I agree with @ollpu. Less complex API with less surface area is better. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
It would be even better if we didn't have to expose StrWrite
and the wrappers in pulldown-cmark-escape
. I guess one way to do it would be to have structs that impl Display
, e.g. EscapeHtml
, and always using write!()
or similar. That's something for a later PR though.
Perfect, merge! Adding the The other remaining issue for the 0.11 is #67. |
I'm trying to hide Respect to the My implementation for those structs (example only with pub struct EscapedHtml<'a> {
html: &'a str,
}
impl<'a> fmt::Display for EscapedHtml<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buffer = String::new();
escape_html(&mut buffer, self.html)?;
write!(f, "{}", buffer)
}
} |
On the first point, to be able to hide For the struct Display approach, you can do this with the current
The internal functions could just take |
I would leave as public the trait and the structs in the pulldown_cmark_escape crate, since it is more low-level, instead of duplicating them in pulldown_cmark. But this approach would not avoid the use of But I'm probably missing something... |
Hm, these simple forwarding calls almost certainly should be zero cost and either get inlined to where they're called or inline internal writer call into them (whatever compiler decides based on heuristics). |
@Martin1887 If the escaping works through Display impls, then it can be used with either fmt or io writers by using There's definitely more for the optimizer to chew on, and I'm also vary of having extra layers of And yes, something similar to |
I think it is worthy for an easier interface to escape HTML strings, and we could even using them in the Could you create the pull request? Thanks! |
Previous attempt:
std::fmt::Write
instead of customStrWrite
trait #493In this version I've just added an associated type on
StrWrite
so thatio::Error
isn't lost. Let me know if that makes sense.Other alternatives: Instead of exposing
StrWrite
,IoWriter
andFmtWriter
we can expose two functions:write_html_io
andwrite_html_fmt
that acceptio::Write
andfmt::Write
(push_html
can be removed then).NOTE. This is a breaking change (should this PR bump version?)Changed base to 0.11