From 8e7a3a36b67dc5db4457f6c330ad1711d042dc47 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Mon, 20 Nov 2017 10:27:27 +0100 Subject: [PATCH] Cleanup. --- src/server/mod.rs | 22 +++++++++++----------- src/server/splitlist.rs | 7 +++++-- templates/photo_link.rs.html | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index a1670a2..cfdf8c4 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -512,28 +512,28 @@ fn person_one<'mw>( .order(date.desc().nulls_last()) .load::(c) .unwrap(); - if photos.len() < 42 { - return res.ok(|o| { - templates::person( - o, - req, - &photos.iter().map(PhotoLink::from).collect::>(), - &person, - ) - }); - } else { + if let Some(groups) = split_to_groups(&photos) { return res.ok(|o| { let path = req.path_without_query().unwrap_or("/"); templates::person( o, req, - &split_to_groups(&photos) + &groups .iter() .map(|g| PhotoLink::for_group(g, path)) .collect::>(), &person, ) }); + } else { + return res.ok(|o| { + templates::person( + o, + req, + &photos.iter().map(PhotoLink::from).collect::>(), + &person, + ) + }); } } res.not_found("Not a person") diff --git a/src/server/splitlist.rs b/src/server/splitlist.rs index 1ced7df..aa1c84e 100644 --- a/src/server/splitlist.rs +++ b/src/server/splitlist.rs @@ -1,6 +1,9 @@ use models::Photo; -pub fn split_to_groups(photos: &[Photo]) -> Vec<&[Photo]> { +pub fn split_to_groups(photos: &[Photo]) -> Option> { + if photos.len() < 42 { + return None; + } let wanted_groups = (photos.len() as f64).sqrt() as usize; let mut groups = vec![&photos[..]]; while groups.len() < wanted_groups { @@ -9,7 +12,7 @@ pub fn split_to_groups(photos: &[Photo]) -> Vec<&[Photo]> { groups[i] = a; groups.insert(i + 1, b); } - groups + Some(groups) } fn find_largest(groups: &[&[Photo]]) -> usize { diff --git a/templates/photo_link.rs.html b/templates/photo_link.rs.html index 9674528..a226a3f 100644 --- a/templates/photo_link.rs.html +++ b/templates/photo_link.rs.html @@ -3,5 +3,5 @@ @(photo: &PhotoLink)

- @if let &Some(ref d) = &photo.lable {
@d} + @if let Some(ref d) = photo.lable {
@d}