Skip to content
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

Update to handlebars v5 #1971

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
54 changes: 32 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ edition = "2018"
[dependencies]
lazy_static = "1.2.0"
fluent = "0.16"
fluent-bundle = "0.11.0"
fluent-bundle = "0.15.0"
fluent-syntax = "0.11.0"
fluent-locale = "0.10.1"
handlebars-fluent = "0.3.1"
handlebars-fluent = "0.4.0"
rand = "0.8"
regex = "1"
rocket = "0.5.0"
rocket_dyn_templates = { version = "0.1.0", features = ["handlebars"] }
rocket = "0.5.1"
rocket_dyn_templates = { version = "0.2.0", features = ["handlebars"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8.17"
sass-rs = "0.2.1"
reqwest = { version = "0.11.4", features = ["json"] }
toml = "0.5"
serde_json = "1.0"
rust_team_data = { git = "https://github.com/rust-lang/team" }
handlebars = "4.3.0"
handlebars = "5.1.0"
siphasher = "0.3.6"
percent-encoding = "2.1.0"
45 changes: 27 additions & 18 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use handlebars::{
Context, Handlebars, Helper, HelperDef, HelperResult, Output, RenderContext, RenderError,
Context, Handlebars, Helper, HelperDef, HelperResult, Output, RenderContext, RenderErrorReason,
};

use rocket::request::FromParam;
Expand Down Expand Up @@ -149,55 +149,64 @@ impl TeamHelperParam {
impl HelperDef for TeamHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
_: &'reg Handlebars,
context: &'rc Context,
rcx: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> HelperResult {
let Some(name) = h.param(0) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} must have at least two parameters",
));
0,
)
Comment on lines -159 to +162
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wasn't a hard error, only a deprecation warning. I hope the variants I chose make some sense. It's a bit late.

.into());
};
let Some(name) = name.relative_path() else {
return Err(RenderError::new(
return Err(RenderErrorReason::InvalidParamType(
"{{team-text}} takes only identifier parameters",
));
)
.into());
};

let Some(id) = h.param(1) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} must have at least two parameters",
));
1,
)
.into());
};
let Some(id) = id.relative_path() else {
return Err(RenderError::new(
return Err(RenderErrorReason::InvalidParamType(
"{{team-text}} takes only identifier parameters",
));
)
.into());
};

let param = match id.as_str() {
"name" => TeamHelperParam::Name,
"description" => TeamHelperParam::Description,
"role" => {
let Some(role_id) = h.param(2) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} requires a third parameter for the role id",
));
2,
)
.into());
};
TeamHelperParam::Role(role_id.value().as_str().unwrap().to_owned())
}
unrecognized => {
return Err(RenderError::new(format!(
return Err(RenderErrorReason::Other(format!(
"unrecognized {{{{team-text}}}} param {unrecognized:?}",
)));
))
.into());
}
};

let team = rcx
.evaluate(context, name)
.map_err(|e| RenderError::from_error(&format!("Cannot find team {}", name), e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
let lang = context
.data()
.get("lang")
Expand All @@ -210,18 +219,18 @@ impl HelperDef for TeamHelper {
if lang == "en-US" {
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render English team data", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
} else if let Some(value) = self.i18n.lookup_no_default_fallback(
&lang.parse().expect("language must be valid"),
&param.fluent_id(team_name),
None,
) {
out.write(&value)
.map_err(|e| RenderError::from_error("failed to render translated team data", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
} else {
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
}
Ok(())
}
Expand Down
19 changes: 13 additions & 6 deletions src/teams.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
use handlebars::{
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderErrorReason,
};
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use rust_team_data::v1::{Team, TeamKind, Teams, BASE_URL};
use std::cmp::Reverse;
Expand Down Expand Up @@ -212,16 +214,21 @@ pub fn encode_zulip_stream(
let zulip_stream = if let Some(p) = h.param(0) {
p.value()
} else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{encode-zulip-stream takes 1 parameter}}",
));
0,
)
.into());
};
let zulip_stream = if let Some(s) = zulip_stream.as_str() {
s
} else {
return Err(RenderError::new(
"{{encode-zulip-stream takes a string parameter}}",
));
return Err(RenderErrorReason::ParamTypeMismatchForName(
"encode-zulip-stream",
"0".into(),
"string".into(),
)
.into());
};

// https://github.com/zulip/zulip/blob/159641bab8c248f5b72a4e736462fb0b48e7fa24/static/js/hash_util.js#L20-L25
Expand Down
1 change: 0 additions & 1 deletion templates/learn/get-started.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,5 @@ fn main() {
src="/static/images/ferris.gif" />
</div>
</section>
{{> components/tools/install-script }}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file was removed in a commit from 2019. It seems that until now, handlebars silently ignored the missing component. I believe the new error was introduced in this PR The result was that the server would crash on the get-started page and the user would see a 404.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I clicked trough a bunch of the pages on the website looking for other occurrances, but couldn't find any. I could've missed something though.

{{/inline}}
{{~> (lookup this "parent")~}}
Loading