Skip to content

Update to new rinja version (askama 0.13) #2789

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

Merged
merged 2 commits into from
Apr 7, 2025
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
94 changes: 43 additions & 51 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ tempfile = "3.1.0"
fn-error-context = "0.2.0"

# Templating
rinja = "0.3.4"
askama = "0.13"
walkdir = "2"

# Date and Time utilities
Expand Down
4 changes: 2 additions & 2 deletions src/utils/html.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::web::page::templates::{Body, Head, Vendored};
use crate::web::rustdoc::RustdocPage;
use askama::Template;
use lol_html::element;
use lol_html::errors::RewritingError;
use rinja::Template;

/// Rewrite a rustdoc page to have the docs.rs topbar
///
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(crate) fn rewrite_lol(
rustdoc_body_class.set_attribute("tabindex", "-1")?;
// Change the `body` to a `div`
rustdoc_body_class.set_tag_name("div")?;
// Prepend the rinja content
// Prepend the askama content
rustdoc_body_class.prepend(&body_html, ContentType::Html);
// Wrap the transformed body and topbar into a <body> element
rustdoc_body_class.before(r#"<body class="rustdoc-page">"#, ContentType::Html);
Expand Down
2 changes: 1 addition & 1 deletion src/web/build_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::{
},
};
use anyhow::Context as _;
use askama::Template;
use axum::{extract::Extension, response::IntoResponse};
use chrono::{DateTime, Utc};
use futures_util::TryStreamExt;
use rinja::Template;
use semver::Version;
use serde::Deserialize;
use std::sync::Arc;
Expand Down
4 changes: 2 additions & 2 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
},
};
use anyhow::{Result, anyhow};
use askama::Template;
use axum::{
Json, extract::Extension, http::header::ACCESS_CONTROL_ALLOW_ORIGIN, response::IntoResponse,
};
Expand All @@ -27,7 +28,6 @@ use axum_extra::{
use chrono::{DateTime, Utc};
use constant_time_eq::constant_time_eq;
use http::StatusCode;
use rinja::Template;
use semver::Version;
use std::sync::Arc;

Expand Down Expand Up @@ -670,7 +670,7 @@ mod tests {
dbg!(&values);
assert!(values.contains(&"6.44 GB"));
assert!(values.contains(&"2 hours"));
assert!(values.contains(&"102.40 kB"));
assert!(values.contains(&"102.4 kB"));
assert!(values.contains(&"blocked"));
assert!(values.contains(&"1"));

Expand Down
2 changes: 1 addition & 1 deletion src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ use crate::{
},
};
use anyhow::{Context, Result, anyhow};
use askama::Template;
use axum::{
extract::Extension,
response::{IntoResponse, Response as AxumResponse},
};
use chrono::{DateTime, Utc};
use futures_util::stream::TryStreamExt;
use log::warn;
use rinja::Template;
use semver::Version;
use serde::Deserialize;
use serde_json::Value;
Expand Down
2 changes: 1 addition & 1 deletion src/web/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
},
};
use anyhow::anyhow;
use askama::Template;
use axum::response::IntoResponse;
use rinja::Template;
use serde_json::Value;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};

Expand Down
2 changes: 1 addition & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::utils::get_correct_docsrs_style_file;
use crate::utils::report_error;
use crate::web::page::templates::{RenderSolid, filters};
use anyhow::{Context as _, Result, anyhow, bail};
use askama::Template;
use axum_extra::middleware::option_layer;
use rinja::Template;
use serde_json::Value;
use tracing::{info, instrument};

Expand Down
38 changes: 19 additions & 19 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::Result;
use crate::web::rustdoc::RustdocPage;
use anyhow::Context;
use rinja::Template;
use askama::Template;
use std::sync::Arc;
use tracing::trace;

Expand Down Expand Up @@ -88,12 +88,12 @@ impl TemplateData {
}

pub mod filters {
use askama::filters::Safe;
use chrono::{DateTime, Utc};
use rinja::filters::Safe;
use std::borrow::Cow;

// Copied from `tera`.
pub fn escape_html(input: &str) -> rinja::Result<Cow<'_, str>> {
pub fn escape_html(input: &str) -> askama::Result<Cow<'_, str>> {
if !input.chars().any(|c| "&<>\"'/".contains(c)) {
return Ok(Cow::Borrowed(input));
}
Expand All @@ -115,7 +115,7 @@ pub mod filters {
}

// Copied from `tera`.
pub fn escape_xml(input: &str) -> rinja::Result<Cow<'_, str>> {
pub fn escape_xml(input: &str) -> askama::Result<Cow<'_, str>> {
if !input.chars().any(|c| "&<>\"'".contains(c)) {
return Ok(Cow::Borrowed(input));
}
Expand All @@ -135,11 +135,11 @@ pub mod filters {

/// Prettily format a timestamp
// TODO: This can be replaced by chrono
pub fn timeformat(value: &DateTime<Utc>) -> rinja::Result<String> {
pub fn timeformat(value: &DateTime<Utc>) -> askama::Result<String> {
Ok(crate::web::duration_to_str(*value))
}

pub fn format_secs(mut value: f32) -> rinja::Result<String> {
pub fn format_secs(mut value: f32) -> askama::Result<String> {
const TIMES: &[&str] = &["seconds", "minutes", "hours"];

let mut chosen_time = &TIMES[0];
Expand Down Expand Up @@ -167,7 +167,7 @@ pub mod filters {
pub fn dedent<T: std::fmt::Display, I: Into<Option<i32>>>(
value: T,
levels: I,
) -> rinja::Result<String> {
) -> askama::Result<String> {
let string = value.to_string();

let unindented = if let Some(levels) = levels.into() {
Expand Down Expand Up @@ -200,7 +200,7 @@ pub mod filters {
Ok(unindented)
}

pub fn highlight(code: impl std::fmt::Display, lang: &str) -> rinja::Result<Safe<String>> {
pub fn highlight(code: impl std::fmt::Display, lang: &str) -> askama::Result<Safe<String>> {
let highlighted_code =
crate::web::highlight::with_lang(Some(lang), &code.to_string(), None);
Ok(Safe(format!(
Expand All @@ -209,7 +209,7 @@ pub mod filters {
)))
}

pub fn round(value: &f32, precision: u32) -> rinja::Result<String> {
pub fn round(value: &f32, precision: u32) -> askama::Result<String> {
let multiplier = if precision == 0 {
1.0
} else {
Expand All @@ -218,43 +218,43 @@ pub mod filters {
Ok(((multiplier * *value).round() / multiplier).to_string())
}

pub fn split_first<'a>(value: &'a str, pat: &str) -> rinja::Result<Option<&'a str>> {
pub fn split_first<'a>(value: &'a str, pat: &str) -> askama::Result<Option<&'a str>> {
Ok(value.split(pat).next())
}

pub fn json_encode<T: ?Sized + serde::Serialize>(value: &T) -> rinja::Result<Safe<String>> {
pub fn json_encode<T: ?Sized + serde::Serialize>(value: &T) -> askama::Result<Safe<String>> {
Ok(Safe(
serde_json::to_string(value).expect("`encode_json` failed"),
))
}
}

pub trait RenderSolid {
fn render_solid(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String>;
fn render_solid(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String>;
}

impl<T: font_awesome_as_a_crate::Solid> RenderSolid for T {
fn render_solid(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String> {
fn render_solid(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String> {
render("fa-solid", self.icon_name(), fw, spin, extra)
}
}

pub trait RenderRegular {
fn render_regular(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String>;
fn render_regular(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String>;
}

impl<T: font_awesome_as_a_crate::Regular> RenderRegular for T {
fn render_regular(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String> {
fn render_regular(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String> {
render("fa-regular", self.icon_name(), fw, spin, extra)
}
}

pub trait RenderBrands {
fn render_brands(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String>;
fn render_brands(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String>;
}

impl<T: font_awesome_as_a_crate::Brands> RenderBrands for T {
fn render_brands(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String> {
fn render_brands(&self, fw: bool, spin: bool, extra: &str) -> askama::filters::Safe<String> {
render("fa-brands", self.icon_name(), fw, spin, extra)
}
}
Expand All @@ -265,7 +265,7 @@ fn render(
fw: bool,
spin: bool,
extra: &str,
) -> rinja::filters::Safe<String> {
) -> askama::filters::Safe<String> {
let mut classes = Vec::new();
if fw {
classes.push("fa-fw");
Expand All @@ -281,5 +281,5 @@ fn render(
classes = classes.join(" "),
);

rinja::filters::Safe(icon)
askama::filters::Safe(icon)
}
4 changes: 2 additions & 2 deletions src/web/page/web_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use http::header::CONTENT_LENGTH;
use std::sync::Arc;

pub(crate) trait AddCspNonce: IntoResponse {
fn render_with_csp_nonce(&mut self, csp_nonce: String) -> rinja::Result<String>;
fn render_with_csp_nonce(&mut self, csp_nonce: String) -> askama::Result<String>;
}

#[macro_export]
Expand All @@ -25,7 +25,7 @@ macro_rules! impl_axum_webpage {
$(,)?
) => {
impl $crate::web::page::web_page::AddCspNonce for $page {
fn render_with_csp_nonce(&mut self, csp_nonce: String) -> rinja::Result<String> {
fn render_with_csp_nonce(&mut self, csp_nonce: String) -> askama::Result<String> {
self.csp_nonce = csp_nonce;
self.render()
}
Expand Down
Loading
Loading