Skip to content

Commit 64ee87b

Browse files
Replace tera with rinja
1 parent f30b11d commit 64ee87b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1552
-1353
lines changed

Cargo.lock

Lines changed: 143 additions & 308 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ tempfile = "3.1.0"
100100
fn-error-context = "0.2.0"
101101

102102
# Templating
103-
tera = { version = "1.5.0", features = ["builtins"] }
103+
# tera = { version = "1.5.0", features = ["builtins"] }
104+
rinja = { path = "/home/imperio/rust/rinja/rinja" }
104105
walkdir = "2"
105106

106107
# Date and Time utilities

src/db/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ impl sqlx::postgres::PgHasArrayType for BuildStatus {
4545
}
4646
}
4747

48+
impl<'a> PartialEq<&'a str> for BuildStatus {
49+
fn eq(&self, other: &&str) -> bool {
50+
match self {
51+
Self::Success => *other == "success",
52+
Self::Failure => *other == "failure",
53+
Self::InProgress => *other == "in_progress",
54+
}
55+
}
56+
}
57+
4858
#[cfg(test)]
4959
mod tests {
5060
use super::*;

src/docbuilder/limits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const GB: usize = 1024 * 1024 * 1024;
66

77
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
88
pub(crate) struct Limits {
9-
memory: usize,
10-
targets: usize,
11-
timeout: Duration,
12-
networking: bool,
13-
max_log_size: usize,
9+
pub memory: usize,
10+
pub targets: usize,
11+
pub timeout: Duration,
12+
pub networking: bool,
13+
pub max_log_size: usize,
1414
}
1515

1616
impl Limits {

src/registry_api.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use chrono::{DateTime, Utc};
44
use reqwest::header::{HeaderValue, ACCEPT, USER_AGENT};
55
use semver::Version;
66
use serde::{Deserialize, Serialize};
7+
use std::fmt;
78
use tracing::instrument;
89
use url::Url;
910

@@ -59,6 +60,15 @@ pub enum OwnerKind {
5960
Team,
6061
}
6162

63+
impl fmt::Display for OwnerKind {
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
match self {
66+
Self::User => f.write_str("user"),
67+
Self::Team => f.write_str("team"),
68+
}
69+
}
70+
}
71+
6272
impl RegistryApi {
6373
pub fn new(api_base: Url, max_retries: u32) -> Result<Self> {
6474
let headers = vec![

src/utils/html.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::web::page::TemplateData;
1+
use crate::web::page::templates::{Body, Head, Topbar, Vendored};
2+
use crate::web::rustdoc::RustdocPage;
23
use lol_html::element;
34
use lol_html::errors::RewritingError;
4-
use tera::Context;
5+
use rinja::Template;
56

67
/// Rewrite a rustdoc page to have the docs.rs topbar
78
///
@@ -12,17 +13,15 @@ use tera::Context;
1213
pub(crate) fn rewrite_lol(
1314
html: &[u8],
1415
max_allowed_memory_usage: usize,
15-
ctx: Context,
16-
templates: &TemplateData,
16+
data: &RustdocPage,
1717
) -> Result<Vec<u8>, RewritingError> {
1818
use lol_html::html_content::{ContentType, Element};
1919
use lol_html::{HtmlRewriter, MemorySettings, Settings};
2020

21-
let templates = &templates.templates;
22-
let tera_head = templates.render("rustdoc/head.html", &ctx).unwrap();
23-
let tera_vendored_css = templates.render("rustdoc/vendored.html", &ctx).unwrap();
24-
let tera_body = templates.render("rustdoc/body.html", &ctx).unwrap();
25-
let tera_rustdoc_topbar = templates.render("rustdoc/topbar.html", &ctx).unwrap();
21+
let head_html = Head::new(data).render().unwrap();
22+
let vendored_html = Vendored::new(data).render().unwrap();
23+
let body_html = Body::new(data).render().unwrap();
24+
let topbar_html = Topbar::new(data).render().unwrap();
2625

2726
// Before: <body> ... rustdoc content ... </body>
2827
// After:
@@ -46,12 +45,12 @@ pub(crate) fn rewrite_lol(
4645
rustdoc_body_class.set_attribute("tabindex", "-1")?;
4746
// Change the `body` to a `div`
4847
rustdoc_body_class.set_tag_name("div")?;
49-
// Prepend the tera content
50-
rustdoc_body_class.prepend(&tera_body, ContentType::Html);
48+
// Prepend the rinja content
49+
rustdoc_body_class.prepend(&body_html, ContentType::Html);
5150
// Wrap the transformed body and topbar into a <body> element
5251
rustdoc_body_class.before(r#"<body class="rustdoc-page">"#, ContentType::Html);
5352
// Insert the topbar outside of the rustdoc div
54-
rustdoc_body_class.before(&tera_rustdoc_topbar, ContentType::Html);
53+
rustdoc_body_class.before(&topbar_html, ContentType::Html);
5554
// Finalize body with </body>
5655
rustdoc_body_class.after("</body>", ContentType::Html);
5756

@@ -62,7 +61,7 @@ pub(crate) fn rewrite_lol(
6261
element_content_handlers: vec![
6362
// Append `style.css` stylesheet after all head elements.
6463
element!("head", |head: &mut Element| {
65-
head.append(&tera_head, ContentType::Html);
64+
head.append(&head_html, ContentType::Html);
6665
Ok(())
6766
}),
6867
element!("body", body_handler),
@@ -81,7 +80,7 @@ pub(crate) fn rewrite_lol(
8180
element!(
8281
"link[rel='stylesheet'][href*='rustdoc-']",
8382
|rustdoc_css: &mut Element| {
84-
rustdoc_css.before(&tera_vendored_css, ContentType::Html);
83+
rustdoc_css.before(&vendored_html, ContentType::Html);
8584
Ok(())
8685
}
8786
),

src/web/build_details.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ use crate::{
22
db::types::BuildStatus,
33
impl_axum_webpage,
44
web::{
5+
crate_details::CrateDetails,
56
error::{AxumNope, AxumResult},
67
extractors::{DbConnection, Path},
78
file::File,
8-
MetaData,
9+
filters, MetaData,
910
},
1011
AsyncStorage, Config,
1112
};
1213
use anyhow::Context as _;
1314
use axum::{extract::Extension, response::IntoResponse};
1415
use chrono::{DateTime, Utc};
1516
use futures_util::TryStreamExt;
17+
use rinja::Template;
1618
use semver::Version;
1719
use serde::{Deserialize, Serialize};
1820
use std::sync::Arc;
@@ -28,17 +30,33 @@ pub(crate) struct BuildDetails {
2830
errors: Option<String>,
2931
}
3032

33+
#[derive(Template)]
34+
#[template(path = "crate/build_details.html")]
3135
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3236
struct BuildDetailsPage {
3337
metadata: MetaData,
3438
build_details: BuildDetails,
3539
use_direct_platform_links: bool,
3640
all_log_filenames: Vec<String>,
3741
current_filename: Option<String>,
42+
csp_nonce: String,
3843
}
3944

40-
impl_axum_webpage! {
41-
BuildDetailsPage = "crate/build_details.html",
45+
impl_axum_webpage! { BuildDetailsPage }
46+
47+
// Used for template rendering.
48+
impl BuildDetailsPage {
49+
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
50+
None
51+
}
52+
53+
pub(crate) fn permalink_path(&self) -> &str {
54+
""
55+
}
56+
57+
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
58+
Some(&self.metadata)
59+
}
4260
}
4361

4462
#[derive(Clone, Deserialize, Debug)]
@@ -126,6 +144,7 @@ pub(crate) async fn build_details_handler(
126144
use_direct_platform_links: true,
127145
all_log_filenames,
128146
current_filename,
147+
csp_nonce: String::new(),
129148
}
130149
.into_response())
131150
}

src/web/builds.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::{
44
docbuilder::Limits,
55
impl_axum_webpage,
66
web::{
7+
crate_details::CrateDetails,
78
error::AxumResult,
89
extractors::{DbConnection, Path},
9-
match_version, MetaData, ReqVersion,
10+
filters, match_version, MetaData, ReqVersion,
1011
},
1112
Config,
1213
};
@@ -15,6 +16,7 @@ use axum::{
1516
extract::Extension, http::header::ACCESS_CONTROL_ALLOW_ORIGIN, response::IntoResponse, Json,
1617
};
1718
use chrono::{DateTime, Utc};
19+
use rinja::Template;
1820
use semver::Version;
1921
use serde::Serialize;
2022
use std::sync::Arc;
@@ -29,17 +31,32 @@ pub(crate) struct Build {
2931
errors: Option<String>,
3032
}
3133

34+
#[derive(Template)]
35+
#[template(path = "crate/builds.html")]
3236
#[derive(Debug, Clone, Serialize)]
3337
struct BuildsPage {
3438
metadata: MetaData,
3539
builds: Vec<Build>,
3640
limits: Limits,
3741
canonical_url: CanonicalUrl,
3842
use_direct_platform_links: bool,
43+
csp_nonce: String,
3944
}
4045

41-
impl_axum_webpage! {
42-
BuildsPage = "crate/builds.html",
46+
impl_axum_webpage! { BuildsPage }
47+
48+
impl BuildsPage {
49+
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
50+
None
51+
}
52+
53+
pub(crate) fn permalink_path(&self) -> &str {
54+
""
55+
}
56+
57+
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
58+
Some(&self.metadata)
59+
}
4360
}
4461

4562
pub(crate) async fn build_list_handler(
@@ -64,6 +81,7 @@ pub(crate) async fn build_list_handler(
6481
limits: Limits::for_crate(&config, &mut conn, &name).await?,
6582
canonical_url: CanonicalUrl::from_path(format!("/crate/{name}/latest/builds")),
6683
use_direct_platform_links: true,
84+
csp_nonce: String::new(),
6785
}
6886
.into_response())
6987
}

0 commit comments

Comments
 (0)