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 rinja to 0.3.2 and do some template cleanup #2591

Merged
merged 11 commits into from
Sep 4, 2024
14 changes: 8 additions & 6 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 @@ -100,7 +100,7 @@ tempfile = "3.1.0"
fn-error-context = "0.2.0"

# Templating
rinja = "0.3"
rinja = "0.3.1"
walkdir = "2"

# Date and Time utilities
Expand Down
9 changes: 4 additions & 5 deletions src/utils/html.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::web::page::templates::{Body, Head, Topbar, Vendored};
use crate::web::rustdoc::RustdocPage;
use crate::web::page::templates::{Body, Head, RustdocPage, Vendored};
use lol_html::element;
use lol_html::errors::RewritingError;
use rinja::Template;
Expand All @@ -19,9 +18,9 @@ pub(crate) fn rewrite_lol(
use lol_html::{HtmlRewriter, MemorySettings, Settings};

let head_html = Head::new(data).render().unwrap();
let vendored_html = Vendored::new(data).render().unwrap();
let body_html = Body::new(data).render().unwrap();
let topbar_html = Topbar::new(data).render().unwrap();
let vendored_html = Vendored.render().unwrap();
let body_html = Body.render().unwrap();
let topbar_html = data.render().unwrap();

// Before: <body> ... rustdoc content ... </body>
// After:
Expand Down
10 changes: 0 additions & 10 deletions src/web/build_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
db::types::BuildStatus,
impl_axum_webpage,
web::{
crate_details::CrateDetails,
error::{AxumNope, AxumResult},
extractors::{DbConnection, Path},
file::File,
Expand Down Expand Up @@ -45,15 +44,6 @@ impl_axum_webpage! { BuildDetailsPage }

// Used for template rendering.
impl BuildDetailsPage {
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
None
}
pub(crate) fn permalink_path(&self) -> &str {
""
}
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
Some(&self.metadata)
}
pub(crate) fn use_direct_platform_links(&self) -> bool {
true
}
Expand Down
10 changes: 0 additions & 10 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
impl_axum_webpage,
utils::spawn_blocking,
web::{
crate_details::CrateDetails,
error::AxumResult,
extractors::{DbConnection, Path},
filters, match_version, MetaData, ReqVersion,
Expand Down Expand Up @@ -55,15 +54,6 @@ struct BuildsPage {
impl_axum_webpage! { BuildsPage }

impl BuildsPage {
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
None
}
pub(crate) fn permalink_path(&self) -> &str {
""
}
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
Some(&self.metadata)
}
pub(crate) fn use_direct_platform_links(&self) -> bool {
true
}
Expand Down
54 changes: 16 additions & 38 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{match_version, MetaData};
use crate::registry_api::OwnerKind;
use crate::utils::{get_correct_docsrs_style_file, report_error};
use crate::web::rustdoc::RustdocHtmlParams;
use crate::{
db::types::BuildStatus,
impl_axum_webpage,
Expand All @@ -12,6 +11,7 @@ use crate::{
error::{AxumNope, AxumResult},
extractors::{DbConnection, Path},
page::templates::filters,
rustdoc::RustdocHtmlParams,
MatchedRelease, ReqVersion,
},
AsyncStorage,
Expand All @@ -32,6 +32,8 @@ use std::sync::Arc;

// TODO: Add target name and versions

#[derive(Template)]
#[template(path = "crate/details.html")]
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CrateDetails {
pub(crate) name: String,
Expand Down Expand Up @@ -66,6 +68,12 @@ pub(crate) struct CrateDetails {
pub(crate) crate_id: i32,
/// Database id for this release
pub(crate) release_id: i32,
pub(crate) csp_nonce: String,
}

impl_axum_webpage! {
CrateDetails,
cpu_intensive_rendering = true,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -251,6 +259,7 @@ impl CrateDetails {
items_with_examples: krate.items_with_examples,
crate_id: krate.crate_id,
release_id: krate.release_id,
csp_nonce: String::new(),
};

// get owners
Expand Down Expand Up @@ -342,6 +351,11 @@ impl CrateDetails {
pub fn latest_release(&self) -> Result<&Release> {
latest_release(&self.releases).ok_or_else(|| anyhow!("crate without releases"))
}

// Used by templates.
pub(crate) fn use_direct_platform_links(&self) -> bool {
true
}
}

pub(crate) fn latest_release(releases: &[Release]) -> Option<&Release> {
Expand Down Expand Up @@ -411,38 +425,6 @@ pub(crate) async fn releases_for_crate(
Ok(releases)
}

#[derive(Template)]
#[template(path = "crate/details.html")]
#[derive(Debug, Clone, PartialEq)]
struct CrateDetailsPage {
details: CrateDetails,
csp_nonce: String,
}

impl_axum_webpage! {
CrateDetailsPage,
cpu_intensive_rendering = true,
}

// Used by templates.
impl CrateDetailsPage {
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
None
}

pub(crate) fn permalink_path(&self) -> &str {
""
}

pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
Some(&self.details.metadata)
}

pub(crate) fn use_direct_platform_links(&self) -> bool {
true
}
}

#[derive(Deserialize, Clone, Debug)]
pub(crate) struct CrateDetailHandlerParams {
name: String,
Expand Down Expand Up @@ -479,11 +461,7 @@ pub(crate) async fn crate_details_handler(
Err(e) => warn!("error fetching readme: {:?}", &e),
}

let mut res = CrateDetailsPage {
details,
csp_nonce: String::new(),
}
.into_response();
let mut res = details.into_response();
res.extensions_mut()
.insert::<CachePolicy>(if req_version.is_latest() {
CachePolicy::ForeverInCdn
Expand Down
10 changes: 0 additions & 10 deletions src/web/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
impl_axum_webpage,
web::{
cache::CachePolicy,
crate_details::CrateDetails,
error::{AxumNope, AxumResult},
extractors::{DbConnection, Path},
filters,
Expand Down Expand Up @@ -113,15 +112,6 @@ impl_axum_webpage! {
}

impl FeaturesPage {
pub(crate) fn krate(&self) -> Option<&CrateDetails> {
None
}
pub(crate) fn permalink_path(&self) -> &str {
""
}
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
Some(&self.metadata)
}
pub(crate) fn use_direct_platform_links(&self) -> bool {
true
}
Expand Down
6 changes: 0 additions & 6 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,6 @@ pub(crate) struct AxumErrorPage {
pub csp_nonce: String,
}

impl AxumErrorPage {
pub(crate) fn get_metadata(&self) -> Option<&MetaData> {
None
}
}

impl_axum_webpage! {
AxumErrorPage,
status = |err| err.status,
Expand Down
69 changes: 37 additions & 32 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
use crate::error::Result;
use crate::web::rustdoc::RustdocPage;
use crate::web::crate_details::CrateDetails;
use crate::web::MetaData;
use anyhow::Context;
use rinja::Template;
use std::{fmt, ops::Deref, sync::Arc};
use std::{fmt, sync::Arc};
use tracing::trace;

macro_rules! rustdoc_page {
($name:ident, $path:literal $(, $meta:ident)?) => {
#[derive(Template)]
#[template(path = $path)]
pub struct $name<'a> {
inner: &'a RustdocPage,
}

impl<'a> $name<'a> {
pub fn new(inner: &'a RustdocPage) -> Self {
Self { inner }
}

$(
pub(crate) fn $meta(&self) -> Option<&MetaData> {
Some(&self.inner.metadata)
}
)?
}

impl<'a> Deref for $name<'a> {
type Target = RustdocPage;
#[derive(Template)]
#[template(path = "rustdoc/head.html")]
pub struct Head<'a> {
rustdoc_css_file: &'a Option<String>,
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
}

fn deref(&self) -> &Self::Target {
self.inner
}
impl<'a> Head<'a> {
pub fn new(inner: &'a RustdocPage) -> Self {
Self {
rustdoc_css_file: &inner.metadata.rustdoc_css_file,
}
};
}
}

rustdoc_page!(Head, "rustdoc/head.html");
rustdoc_page!(Vendored, "rustdoc/vendored.html");
rustdoc_page!(Body, "rustdoc/body.html");
rustdoc_page!(Topbar, "rustdoc/topbar.html", get_metadata);
#[derive(Template)]
#[template(path = "rustdoc/vendored.html")]
pub struct Vendored;

#[derive(Template)]
#[template(path = "rustdoc/body.html")]
pub struct Body;

#[derive(Template)]
#[template(path = "rustdoc/topbar.html")]
#[derive(Debug, Clone)]
pub struct RustdocPage {
pub latest_path: String,
pub permalink_path: String,
pub inner_path: String,
// true if we are displaying the latest version of the crate, regardless
// of whether the URL specifies a version number or the string "latest."
pub is_latest_version: bool,
// true if the URL specifies a version using the string "latest."
pub is_latest_url: bool,
pub is_prerelease: bool,
pub krate: CrateDetails,
pub metadata: MetaData,
pub current_target: String,
}

/// Holds all data relevant to templating
#[derive(Debug)]
Expand Down
Loading
Loading