Skip to content

Update to new rinja version (askama) #139321

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 3 commits into from
Apr 8, 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
59 changes: 54 additions & 5 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,48 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"

[[package]]
name = "askama"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a4e46abb203e00ef226442d452769233142bbfdd79c3941e84c8e61c4112543"
dependencies = [
"askama_derive",
"itoa",
"percent-encoding",
"serde",
"serde_json",
]

[[package]]
name = "askama_derive"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54398906821fd32c728135f7b351f0c7494ab95ae421d41b6f5a020e158f28a6"
dependencies = [
"askama_parser",
"basic-toml",
"memchr",
"proc-macro2",
"quote",
"rustc-hash 2.1.1",
"serde",
"serde_derive",
"syn 2.0.100",
]

[[package]]
name = "askama_parser"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf315ce6524c857bb129ff794935cf6d42c82a6cff60526fe2a63593de4d0d4f"
dependencies = [
"memchr",
"serde",
"serde_derive",
"winnow 0.7.4",
]

[[package]]
name = "autocfg"
version = "1.4.0"
Expand Down Expand Up @@ -1345,8 +1387,8 @@ name = "generate-copyright"
version = "0.1.0"
dependencies = [
"anyhow",
"askama",
"cargo_metadata 0.18.1",
"rinja",
"serde",
"serde_json",
"thiserror 1.0.69",
Expand Down Expand Up @@ -3069,9 +3111,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dc4940d00595430b3d7d5a01f6222b5e5b51395d1120bdb28d854bb8abb17a5"
dependencies = [
"humansize",
"itoa",
"percent-encoding",
"rinja_derive",
]

Expand Down Expand Up @@ -4619,6 +4659,7 @@ name = "rustdoc"
version = "0.0.0"
dependencies = [
"arrayvec",
"askama",
"base64",
"expect-test",
"indexmap",
Expand All @@ -4627,7 +4668,6 @@ dependencies = [
"pulldown-cmark 0.9.6",
"pulldown-cmark-escape",
"regex",
"rinja",
"rustdoc-json-types",
"serde",
"serde_json",
Expand Down Expand Up @@ -5418,7 +5458,7 @@ dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
"winnow 0.5.40",
]

[[package]]
Expand Down Expand Up @@ -6429,6 +6469,15 @@ dependencies = [
"memchr",
]

[[package]]
name = "winnow"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36"
dependencies = [
"memchr",
]

[[package]]
name = "winsplit"
version = "0.1.0"
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap/src/utils/proc_macro_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub static CRATES: &[&str] = &[
// tidy-alphabetical-start
"annotate-snippets",
"anstyle",
"askama_parser",
"basic-toml",
"block-buffer",
"bumpalo",
Expand Down Expand Up @@ -64,6 +65,7 @@ pub static CRATES: &[&str] = &[
"wasm-bindgen-backend",
"wasm-bindgen-macro-support",
"wasm-bindgen-shared",
"winnow",
"yoke",
"zerofrom",
"zerovec",
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ path = "lib.rs"

[dependencies]
arrayvec = { version = "0.7", default-features = false }
rinja = { version = "0.3", default-features = false, features = ["config"] }
askama = { version = "0.13", default-features = false, features = ["alloc", "config", "derive"] }
base64 = "0.21.7"
itertools = "0.12"
indexmap = "2"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Display};
use std::path::PathBuf;

use rinja::Template;
use askama::Template;
use rustc_data_structures::fx::FxIndexMap;

use super::static_files::{STATIC_FILES, StaticFiles};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, channel};

use rinja::Template;
use askama::Template;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE};
use rustc_middle::ty::TyCtxt;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::iter::Peekable;
use std::path::PathBuf;
use std::{fs, str};

use rinja::Template;
use askama::Template;
use rustc_attr_parsing::{
ConstStability, DeprecatedSince, Deprecation, RustcVersion, StabilityLevel, StableSince,
};
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::Ordering;
use std::fmt::{self, Display, Write as _};
use std::iter;

use rinja::Template;
use askama::Template;
use rustc_abi::VariantIdx;
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_hir as hir;
Expand Down Expand Up @@ -37,7 +37,7 @@ use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::render::{document_full, document_item_info};
use crate::html::url_parts_builder::UrlPartsBuilder;

/// Generates a Rinja template struct for rendering items with common methods.
/// Generates an Askama template struct for rendering items with common methods.
///
/// Usage:
/// ```ignore (illustrative)
Expand Down Expand Up @@ -301,7 +301,7 @@ fn toggle_close(mut w: impl fmt::Write) {
w.write_str("</details>").unwrap();
}

trait ItemTemplate<'a, 'cx: 'a>: rinja::Template + Display {
trait ItemTemplate<'a, 'cx: 'a>: askama::Template + Display {
fn item_and_cx(&self) -> (&'a clean::Item, &'a Context<'cx>);
}

Expand Down Expand Up @@ -1867,7 +1867,7 @@ fn item_proc_macro(cx: &Context<'_>, it: &clean::Item, m: &clean::ProcMacro) ->
}
}
}
Ok(())
fmt::Result::Ok(())
})?;
write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
})
Expand Down Expand Up @@ -1944,7 +1944,7 @@ fn item_constant(
}
}
}
Ok(())
Ok::<(), fmt::Error>(())
})?;

write!(w, "{}", document(cx, it, None, HeadingOffset::H2))
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::cmp::Ordering;

use rinja::Template;
use askama::Template;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::CtorKind;
use rustc_hir::def_id::{DefIdMap, DefIdSet};
Expand Down Expand Up @@ -123,10 +123,10 @@ impl<'a> Link<'a> {
pub(crate) mod filters {
use std::fmt::{self, Display};

use rinja::filters::Safe;
use askama::filters::Safe;

use crate::html::escape::EscapeBodyTextWithWbr;
pub(crate) fn wrapped<T>(v: T) -> rinja::Result<Safe<impl Display>>
pub(crate) fn wrapped<T>(v: T) -> askama::Result<Safe<impl Display>>
where
T: Display,
{
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/type_layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use rinja::Template;
use askama::Template;
use rustc_abi::{Primitive, TagEncoding, Variants};
use rustc_hir::def_id::DefId;
use rustc_middle::span_bug;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ffi::OsStr;
use std::path::{Component, Path, PathBuf};
use std::{fmt, fs};

use rinja::Template;
use askama::Template;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::ty::TyCtxt;
Expand Down
16 changes: 8 additions & 8 deletions src/librustdoc/html/templates/STYLE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Style for Templates

This directory has templates in the [Rinja templating language][rinjadoc], which is very
This directory has templates in the [Askama templating language][askamadoc], which is very
similar to [Jinja2][jinjadoc].

[jinjadoc]: https://jinja.palletsprojects.com/en/3.1.x/templates/
[rinjadoc]: https://docs.rs/rinja/latest/rinja/
[askamadoc]: https://docs.rs/askama/latest/askama/

We want our rendered output to have as little unnecessary whitespace as
possible, so that pages load quickly. To achieve that we use Rinja's
possible, so that pages load quickly. To achieve that we use Askama's
[whitespace control] features. By default, whitespace characters are removed
around jinja tags (`{% %}` for example). At the end of most lines, we put an
empty comment tag: `{# #}`. This causes all whitespace between the end of the
Expand All @@ -18,19 +18,19 @@ remove following whitespace but not preceding. We also use the whitespace
control characters in most instances of tags with control flow, for example
`{% if foo %}`.

[whitespace control]: https://rinja.readthedocs.io/en/stable/configuration.html#whitespace-control
[whitespace control]: https://askama.readthedocs.io/en/stable/configuration.html#whitespace-control

We want our templates to be readable, so we use indentation and newlines
liberally. We indent by four spaces after opening an HTML tag _or_ a Jinja
tag. In most cases an HTML tag should be followed by a newline, but if the
tag has simple contents and fits with its close tag on a single line, the
contents don't necessarily need a new line.

Rinja templates support quite sophisticated control flow. To keep our templates
Askama templates support quite sophisticated control flow. To keep our templates
simple and understandable, we use only a subset: `if` and `for`. In particular
we avoid [assignments in the template logic][assignments] and [Rinja
we avoid [assignments in the template logic][assignments] and [Askama
macros][macros]. This also may make things easier if we switch to a different
Jinja-style template system in the future.

[assignments]: https://rinja.readthedocs.io/en/stable/template_syntax.html#assignments
[macros]: https://rinja.readthedocs.io/en/stable/template_syntax.html#macros
[assignments]: https://askama.readthedocs.io/en/stable/template_syntax.html#assignments
[macros]: https://askama.readthedocs.io/en/stable/template_syntax.html#macros
2 changes: 1 addition & 1 deletion src/tools/generate-copyright/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ description = "Produces a manifest of all the copyrighted materials in the Rust

[dependencies]
anyhow = "1.0.65"
askama = "0.13.0"
cargo_metadata = "0.18.1"
rinja = "0.3.0"
serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.85"
thiserror = "1"
4 changes: 2 additions & 2 deletions src/tools/generate-copyright/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use anyhow::Error;
use rinja::Template;
use askama::Template;

mod cargo_metadata;

Expand Down Expand Up @@ -117,7 +117,7 @@ struct Metadata {
}

/// Describes one node in our metadata tree
#[derive(serde::Deserialize, rinja::Template, Clone, Debug, PartialEq, Eq)]
#[derive(serde::Deserialize, Template, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case", tag = "type")]
#[template(path = "Node.html")]
pub(crate) enum Node {
Expand Down
Loading