Skip to content

Commit

Permalink
feat: find file directly, regardless of filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Teajey committed Feb 7, 2024
1 parent abb889c commit f060f3f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "custard"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "MIT"
description = "A frontmatter-querying server"
Expand Down
21 changes: 18 additions & 3 deletions src/route/frontmatter_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ fn post_inner(
) -> Result<(HeaderMap, String), StatusCode> {
let keeper = lock_keeper(files)?;

let mut files = keeper.files().collect::<Vec<_>>();
let files = keeper.files().collect::<Vec<_>>();

sort_with_params(params, &mut files);
let mut filtered_files =
query_files(files.clone().into_iter(), query, Some(name)).collect::<Vec<_>>();

let filtered_files = query_files(files.clone().into_iter(), query).collect::<Vec<_>>();
sort_with_params(params, &mut filtered_files);

let (i, file) = find_file_and_index(&filtered_files, name)?;

Expand Down Expand Up @@ -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, &params, "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);
}
}
2 changes: 1 addition & 1 deletion src/route/frontmatter_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

Expand Down
8 changes: 7 additions & 1 deletion src/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ use crate::{
fn query_files<'a>(
files: impl Iterator<Item = &'a FrontmatterFile>,
query: &'a FrontmatterQuery,
name: Option<&'a str>,
) -> impl Iterator<Item = &'a FrontmatterFile> {
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();
Expand Down

0 comments on commit f060f3f

Please sign in to comment.