From f060f3ff7cc75ce56162db350b6ea677a10d5956 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Thu, 8 Feb 2024 00:24:29 +1300 Subject: [PATCH] feat: find file directly, regardless of filters --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/route/frontmatter_file.rs | 21 ++++++++++++++++++--- src/route/frontmatter_list.rs | 2 +- src/route/mod.rs | 8 +++++++- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69b2a4f..57a5cb0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -200,7 +200,7 @@ dependencies = [ [[package]] name = "custard" -version = "0.2.0" +version = "0.2.1" dependencies = [ "anyhow", "axum", diff --git a/Cargo.toml b/Cargo.toml index d405fc2..83370d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "custard" -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "MIT" description = "A frontmatter-querying server" diff --git a/src/route/frontmatter_file.rs b/src/route/frontmatter_file.rs index fe98a0e..f970747 100644 --- a/src/route/frontmatter_file.rs +++ b/src/route/frontmatter_file.rs @@ -124,11 +124,12 @@ fn post_inner( ) -> Result<(HeaderMap, String), StatusCode> { let keeper = lock_keeper(files)?; - let mut files = keeper.files().collect::>(); + let files = keeper.files().collect::>(); - sort_with_params(params, &mut files); + let mut filtered_files = + query_files(files.clone().into_iter(), query, Some(name)).collect::>(); - let filtered_files = query_files(files.clone().into_iter(), query).collect::>(); + sort_with_params(params, &mut filtered_files); let (i, file) = find_file_and_index(&filtered_files, name)?; @@ -354,5 +355,19 @@ mod test { .unwrap(); assert_eq!(None, prev_file_name); assert_eq!(Some("about.md"), next_file_name); + + let (headers, _) = post_inner(&keeper, ¶ms, "something.md", &query).unwrap(); + let next_file_name = headers + .get("x-next-file") + .map(|h| h.to_str()) + .transpose() + .unwrap(); + let prev_file_name = headers + .get("x-prev-file") + .map(|h| h.to_str()) + .transpose() + .unwrap(); + assert_eq!(Some("about.md"), prev_file_name); + assert_eq!(None, next_file_name); } } diff --git a/src/route/frontmatter_list.rs b/src/route/frontmatter_list.rs index 183d339..690e6d1 100644 --- a/src/route/frontmatter_list.rs +++ b/src/route/frontmatter_list.rs @@ -73,7 +73,7 @@ fn post_inner( let files = keeper.files(); - let mut filtered_files = query_files(files, query) + let mut filtered_files = query_files(files, query, None) .map(|file| file.clone().into()) .collect::>(); diff --git a/src/route/mod.rs b/src/route/mod.rs index 84662a3..db1ac02 100644 --- a/src/route/mod.rs +++ b/src/route/mod.rs @@ -14,8 +14,14 @@ use crate::{ fn query_files<'a>( files: impl Iterator, query: &'a FrontmatterQuery, + name: Option<&'a str>, ) -> impl Iterator { - files.filter(|file| { + files.filter(move |file| { + if let Some(name) = name { + if file.name == name { + return true; + } + } let Some(frontmatter) = file.frontmatter() else { // if query is '{}', include this return query.is_empty();