Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions crates/templates/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ impl TemplateContent {
raw: Vec<u8>,
parser: &liquid::Parser,
) -> anyhow::Result<TemplateContent> {
match string_from_bytes(&raw) {
None => Ok(TemplateContent::Binary(raw)),
Some(s) => {
match String::from_utf8(raw) {
// `into_bytes` hands back the original bytes that failed UTF-8 validation
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // TODO: try other encodings!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would benefit from a comment: my first reading was "wait is this rendering the error" and I had to look at the docs to understand "oh the error has methods to retrieve the blob that got consumed."

Suggested change
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // TODO: try other encodings!
Err(e) => Ok(TemplateContent::Binary(e.into_bytes())), // retrieves the original blob // TODO: try other encodings!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @itowlson, thanks for the feedback, and this is indeed very confusing. I rephrased it a little bit and pushed in 57b22da
Is that better? Let me know if you would suggest a further change.

Ok(s) => {
match parser.parse(&s) {
Ok(t) => Ok(TemplateContent::Template(t)),
Err(e) => match understand_liquid_error(e) {
TemplateParseFailure::Other(_e) => {
// TODO: emit a warning?
Ok(TemplateContent::Binary(raw))
Ok(TemplateContent::Binary(s.into_bytes()))
}
TemplateParseFailure::UnknownFilter(id) => {
Err(anyhow!("internal error in template: unknown filter '{id}'"))
Expand All @@ -112,22 +113,13 @@ impl TemplateContent {
match self {
Self::Template(t) => {
let text = t.render(globals)?;
Ok(text.bytes().collect())
Ok(text.into_bytes())
}
Self::Binary(v) => Ok(v),
}
}
}

// TODO: this doesn't truly belong in a module that claims to be about
// rendering but the only thing that uses it is the TemplateContent ctor
fn string_from_bytes(bytes: &[u8]) -> Option<String> {
match std::str::from_utf8(bytes) {
Ok(s) => Some(s.to_owned()),
Err(_) => None, // TODO: try other encodings!
}
}

enum TemplateParseFailure {
UnknownFilter(String),
Other(liquid::Error),
Expand Down
Loading