diff options
| author | Fuwn <[email protected]> | 2025-05-27 02:43:36 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-05-27 02:43:36 -0700 |
| commit | b8f61569dbede77a3642bb44f6d213df8f4a9e5e (patch) | |
| tree | 9cc1c1d3f794f99f2fec90c33c67cff1dfa6f43f | |
| parent | build(container): Update build steps to remedy over-caching (diff) | |
| download | locus-b8f61569dbede77a3642bb44f6d213df8f4a9e5e.tar.xz locus-b8f61569dbede77a3642bb44f6d213df8f4a9e5e.zip | |
feat(directory): Sort routes by hits
| -rw-r--r-- | src/modules/directory.rs | 25 | ||||
| -rw-r--r-- | src/route.rs | 4 |
2 files changed, 23 insertions, 6 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 9eddc31..4179175 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -1,3 +1,5 @@ +use crate::route::hits_from; + pub fn module(router: &mut windmark::router::Router) { crate::route::track_mount( router, @@ -8,11 +10,24 @@ pub fn module(router: &mut windmark::router::Router) { &format!( "# Directory\n\nA map of all publicly available routes on this \ Gemini capsule\n\n{}", - (*crate::route::ROUTES.lock().unwrap()) - .iter() - .map(|(r, d)| format!("=> {} {}", r, d.description)) - .collect::<Vec<_>>() - .join("\n") + { + let mut lines = (*crate::route::ROUTES.lock().unwrap()) + .iter() + .map(|(r, d)| format!("=> {} {}", r, d.description)) + .collect::<Vec<_>>(); + let standard_transform = |route: &str| { + route.replace("=> ", "").split(' ').collect::<Vec<_>>()[0] + .to_string() + }; + + lines.sort_by(|a, b| { + hits_from(&standard_transform(a)) + .cmp(&hits_from(&standard_transform(b))) + }); + lines.reverse(); + + lines.join("\n") + } ), &context, ) diff --git a/src/route.rs b/src/route.rs index 12cc8f1..171952c 100644 --- a/src/route.rs +++ b/src/route.rs @@ -25,7 +25,9 @@ impl Route { pub fn hits_from(route: &str) -> i32 { crate::DATABASE.lock().map_or(0, |database| { - (*database).get::<i32>(if route.is_empty() { "/" } else { route }).unwrap() + (*database) + .get::<i32>(if route.is_empty() { "/" } else { route }) + .unwrap_or(0) }) } |