@@ -76,7 +76,9 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
76
76
// `state.config.og_image_base_url` will always be `Some` as that's required
77
77
// if `state.config.serve_html` is `true`, and otherwise this
78
78
// 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 ) ) ;
80
82
81
83
// Fetch the HTML from cache given `og_image_url` as key or render it
82
84
let html = RENDERED_HTML_CACHE
@@ -123,29 +125,21 @@ fn extract_crate_name(path: &str) -> Option<&str> {
123
125
/// Come up with an Open Graph image URL. In case a crate page is requested,
124
126
/// we use the crate's name as extracted from the request path and the OG image
125
127
/// 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) ?;
137
132
138
- OG_IMAGE_FALLBACK_URL . into ( )
133
+ let filename = format ! ( "{krate}.png" ) ;
134
+ og_image_base_url. join ( & filename) . ok ( )
139
135
}
140
136
141
137
#[ cfg( test) ]
142
138
mod tests {
143
139
use googletest:: { assert_that, prelude:: eq} ;
144
140
use url:: Url ;
145
141
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} ;
149
143
150
144
#[ test]
151
145
fn test_extract_crate_name ( ) {
@@ -167,27 +161,27 @@ mod tests {
167
161
168
162
#[ test]
169
163
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" ) ) ,
172
166
(
173
167
"/crates/tokio/versions" ,
174
- "http://localhost:3000/og/tokio.png" ,
168
+ Some ( "http://localhost:3000/og/tokio.png" ) ,
175
169
) ,
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 ) ,
182
176
] ;
183
177
184
178
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) ;
186
180
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 ( ) )
191
185
) ;
192
186
}
193
187
}
0 commit comments