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.2"
walkdir = "2"

# Date and Time utilities
Expand Down
8 changes: 4 additions & 4 deletions src/utils/html.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::web::page::templates::{Body, Head, Topbar, Vendored};
use crate::web::page::templates::{Body, Head, Vendored};
use crate::web::rustdoc::RustdocPage;
use lol_html::element;
use lol_html::errors::RewritingError;
Expand All @@ -19,9 +19,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
93 changes: 71 additions & 22 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 @@ -31,7 +31,6 @@ use serde_json::Value;
use std::sync::Arc;

// TODO: Add target name and versions

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CrateDetails {
pub(crate) name: String,
Expand Down Expand Up @@ -415,34 +414,41 @@ pub(crate) async fn releases_for_crate(
#[template(path = "crate/details.html")]
#[derive(Debug, Clone, PartialEq)]
struct CrateDetailsPage {
details: CrateDetails,
version: Version,
name: String,
owners: Vec<(String, String, OwnerKind)>,
metadata: MetaData,
documented_items: Option<i32>,
total_items: Option<i32>,
total_items_needing_examples: Option<i32>,
items_with_examples: Option<i32>,
homepage_url: Option<String>,
documentation_url: Option<String>,
repository_url: Option<String>,
repository_metadata: Option<RepositoryMetadata>,
dependencies: Option<Value>,
releases: Vec<Release>,
readme: Option<String>,
build_status: BuildStatus,
rustdoc_status: Option<bool>,
is_library: Option<bool>,
last_successful_build: Option<String>,
rustdoc: Option<String>, // this is description_long in database
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)
}

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

impl_axum_webpage! {
CrateDetailsPage,
cpu_intensive_rendering = true,
}

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

let CrateDetails {
version,
name,
owners,
metadata,
documented_items,
total_items,
total_items_needing_examples,
items_with_examples,
homepage_url,
documentation_url,
repository_url,
repository_metadata,
dependencies,
releases,
readme,
build_status,
rustdoc_status,
is_library,
last_successful_build,
rustdoc,
..
} = details;

let mut res = CrateDetailsPage {
details,
version,
name,
owners,
metadata,
documented_items,
total_items,
total_items_needing_examples,
items_with_examples,
homepage_url,
documentation_url,
repository_url,
repository_metadata,
dependencies,
releases,
readme,
build_status,
rustdoc_status,
is_library,
last_successful_build,
rustdoc,
csp_nonce: String::new(),
}
.into_response();
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
50 changes: 18 additions & 32 deletions src/web/page/templates.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
use crate::error::Result;
use crate::web::rustdoc::RustdocPage;
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: Option<&'a str>,
}

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.as_deref(),
}
};
}
}

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;

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