Skip to content

Commit e862706

Browse files
committed
refactor logic into a function
1 parent 9f1dc7d commit e862706

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/web/rustdoc.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::page::Page;
1515
use rustc_serialize::json::{Json, ToJson};
1616
use std::collections::BTreeMap;
1717
use iron::headers::{Expires, HttpDate, CacheControl, CacheDirective};
18+
use postgres::Connection;
1819
use time;
1920
use iron::Handler;
2021
use utils;
@@ -260,32 +261,11 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
260261

261262
content.full = file_content;
262263
let crate_details = cexpect!(CrateDetails::new(&conn, &name, &version));
263-
let latest_version = latest_version(&crate_details.versions, &version);
264-
265-
let (path, version) = if let Some(version) = latest_version {
264+
let (path, version) = if let Some(version) = latest_version(&crate_details.versions, &version) {
266265
req_path[2] = &version;
267-
let path = if File::from_path(&conn, &req_path.join("/")).is_some() {
268-
req_path[3..].join("/") // NOTE: this adds 'index.html' if it wasn't there before
269-
} else { // this page doesn't exist in the latest version
270-
let search_item = if *req_path.last().unwrap() == "index.html" { // this is a module
271-
req_path[&req_path.len() - 2]
272-
} else { // this is an item
273-
req_path.last().unwrap().split('.').nth(1)
274-
.expect("paths should be of the form <class>.<name>.html")
275-
};
276-
// check if req_path[3] is the platform choice or the name of the crate
277-
let concat_path;
278-
let crate_root = if req_path[3] != crate_details.target_name {
279-
concat_path = format!("{}/{}", req_path[3], req_path[4]);
280-
&concat_path
281-
} else {
282-
req_path[3]
283-
};
284-
format!("{}/?search={}", crate_root, search_item)
285-
};
286-
(path, version)
266+
(path_for_version(&req_path, &crate_details.target_name, &conn), version)
287267
} else {
288-
(String::new(), String::new())
268+
Default::default()
289269
};
290270

291271
content.crate_details = Some(crate_details);
@@ -300,7 +280,27 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
300280
.to_resp("rustdoc")
301281
}
302282

303-
283+
fn path_for_version(req_path: &[&str], target_name: &str, conn: &Connection) -> String {
284+
if File::from_path(&conn, &req_path.join("/")).is_some() {
285+
req_path[3..].join("/") // NOTE: this adds 'index.html' if it wasn't there before
286+
} else { // this page doesn't exist in the latest version
287+
let search_item = if *req_path.last().unwrap() == "index.html" { // this is a module
288+
req_path[req_path.len() - 2]
289+
} else { // this is an item
290+
req_path.last().unwrap().split('.').nth(1)
291+
.expect("paths should be of the form <class>.<name>.html")
292+
};
293+
// check if req_path[3] is the platform choice or the name of the crate
294+
let concat_path;
295+
let crate_root = if req_path[3] != target_name {
296+
concat_path = format!("{}/{}", req_path[3], req_path[4]);
297+
&concat_path
298+
} else {
299+
req_path[3]
300+
};
301+
format!("{}/?search={}", crate_root, search_item)
302+
}
303+
}
304304

305305
pub fn badge_handler(req: &mut Request) -> IronResult<Response> {
306306
use iron::headers::ContentType;

0 commit comments

Comments
 (0)