Skip to content

Commit bec1a37

Browse files
committed
ember_html: Simplify generate_og_image_url() fn by returning Option
1 parent 77f3de1 commit bec1a37

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

src/middleware/ember_html.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
7676
// `state.config.og_image_base_url` will always be `Some` as that's required
7777
// if `state.config.serve_html` is `true`, and otherwise this
7878
// middleware won't be executed; see `crate::middleware::apply_axum_middleware`.
79-
let og_image_url = generate_og_image_url(path, &state.config.og_image_base_url);
79+
let og_image_url = generate_og_image_url(path, state.config.og_image_base_url.as_ref())
80+
.map(|url| Cow::Owned(url.to_string()))
81+
.unwrap_or(Cow::Borrowed(OG_IMAGE_FALLBACK_URL));
8082

8183
// Fetch the HTML from cache given `og_image_url` as key or render it
8284
let html = RENDERED_HTML_CACHE
@@ -123,29 +125,21 @@ fn extract_crate_name(path: &str) -> Option<&str> {
123125
/// Come up with an Open Graph image URL. In case a crate page is requested,
124126
/// we use the crate's name as extracted from the request path and the OG image
125127
/// base URL from config to generate one, otherwise we use the fallback image.
126-
fn generate_og_image_url(path: &str, og_image_base_url: &Option<Url>) -> Cow<'static, str> {
127-
let Some(og_image_base_url) = og_image_base_url else {
128-
return OG_IMAGE_FALLBACK_URL.into();
129-
};
130-
131-
if let Some(krate) = extract_crate_name(path) {
132-
let filename = format!("{krate}.png");
133-
if let Ok(og_img_url) = og_image_base_url.join(&filename) {
134-
return og_img_url.into();
135-
}
136-
}
128+
fn generate_og_image_url(path: &str, og_image_base_url: Option<&Url>) -> Option<Url> {
129+
let og_image_base_url = og_image_base_url?;
130+
131+
let krate = extract_crate_name(path)?;
137132

138-
OG_IMAGE_FALLBACK_URL.into()
133+
let filename = format!("{krate}.png");
134+
og_image_base_url.join(&filename).ok()
139135
}
140136

141137
#[cfg(test)]
142138
mod tests {
143139
use googletest::{assert_that, prelude::eq};
144140
use url::Url;
145141

146-
use crate::middleware::ember_html::{
147-
extract_crate_name, generate_og_image_url, OG_IMAGE_FALLBACK_URL,
148-
};
142+
use crate::middleware::ember_html::{extract_crate_name, generate_og_image_url};
149143

150144
#[test]
151145
fn test_extract_crate_name() {
@@ -167,27 +161,27 @@ mod tests {
167161

168162
#[test]
169163
fn test_generate_og_image_url() {
170-
const PATHS: &[(&str, &str)] = &[
171-
("/crates/tokio", "http://localhost:3000/og/tokio.png"),
164+
const PATHS: &[(&str, Option<&str>)] = &[
165+
("/crates/tokio", Some("http://localhost:3000/og/tokio.png")),
172166
(
173167
"/crates/tokio/versions",
174-
"http://localhost:3000/og/tokio.png",
168+
Some("http://localhost:3000/og/tokio.png"),
175169
),
176-
("/crates/tokio/", "http://localhost:3000/og/tokio.png"),
177-
("/", OG_IMAGE_FALLBACK_URL),
178-
("/crates", OG_IMAGE_FALLBACK_URL),
179-
("/crates/", OG_IMAGE_FALLBACK_URL),
180-
("/dashboard/", OG_IMAGE_FALLBACK_URL),
181-
("/settings/profile", OG_IMAGE_FALLBACK_URL),
170+
("/crates/tokio/", Some("http://localhost:3000/og/tokio.png")),
171+
("/", None),
172+
("/crates", None),
173+
("/crates/", None),
174+
("/dashboard/", None),
175+
("/settings/profile", None),
182176
];
183177

184178
let og_image_base_url: Url = "http://localhost:3000/og/".parse().unwrap();
185-
let og_image_base_url = Some(og_image_base_url);
179+
let og_image_base_url = Some(&og_image_base_url);
186180

187-
for (path, expected) in PATHS.iter().copied() {
188-
assert_that!(
189-
generate_og_image_url(path, &og_image_base_url),
190-
eq(expected)
181+
for (path, expected) in PATHS.iter() {
182+
assert_eq!(
183+
generate_og_image_url(path, og_image_base_url),
184+
expected.map(|url| Url::parse(url).unwrap())
191185
);
192186
}
193187
}

0 commit comments

Comments
 (0)