Skip to content

Commit

Permalink
not_before: introduce enum to clarify API
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Mar 28, 2024
1 parent 2950600 commit 06d0c6f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
28 changes: 27 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ mod macros;
//
// At minimum, we don't want to list the indices (may need proc-macro)

#[derive(Clone, Eq, PartialEq, Debug, serde::Serialize, serde::Deserialize)]
pub enum NotBefore {
/// Start iteration at the beginning of the directory
None,
/// Start iteration at an exact match with the provided filename
Filename(PathBuf),
/// Start iteration at the first path that is "after" the provided filename
FilenamePart(PathBuf),
}

impl NotBefore {
pub fn with_filename(value: Option<PathBuf>) -> Self {
match value {
None => Self::None,
Some(p) => Self::Filename(p),
}
}

pub fn with_filename_part(value: Option<PathBuf>) -> Self {
match value {
None => Self::None,
Some(p) => Self::FilenamePart(p),
}
}
}

generate_enums! {

////////////
Expand Down Expand Up @@ -244,7 +270,7 @@ pub mod request {
ReadDirFirst:
- location: Location
- dir: PathBuf
- not_before: Option<(PathBuf, bool)>
- not_before: NotBefore

ReadDirNext:

Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ pub trait FilesystemClient: PollClient {
self.request(request::ReadDirFirst {
location,
dir,
not_before: not_before_filename.map(|p| (p, true)),
not_before: NotBefore::with_filename(not_before_filename),
})
}

Expand All @@ -630,7 +630,7 @@ pub trait FilesystemClient: PollClient {
self.request(request::ReadDirFirst {
location,
dir,
not_before: not_before_filename.map(|p| (p, false)),
not_before: NotBefore::with_filename_part(not_before_filename),
})
}

Expand Down
5 changes: 1 addition & 4 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,7 @@ impl<P: Platform> ServiceResources<P> {
let maybe_entry = match filestore.read_dir_first(
&request.dir,
request.location,
request
.not_before
.as_ref()
.map(|(path, require_equal)| (&**path, *require_equal)),
&request.not_before,
)? {
Some((entry, read_dir_state)) => {
ctx.read_dir_state = Some(read_dir_state);
Expand Down
27 changes: 11 additions & 16 deletions src/store/filestore.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::cmp::Ordering;

use crate::{
api::NotBefore,
error::{Error, Result},
// service::ReadDirState,
store::{self, DynFilesystem, Store},
Expand Down Expand Up @@ -119,7 +120,7 @@ pub trait Filestore {
&mut self,
dir: &Path,
location: Location,
not_before: Option<(&Path, bool)>,
not_before: &NotBefore,
) -> Result<Option<(DirEntry, ReadDirState)>>;

/// Continue iterating over entries of a directory.
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<S: Store> ClientFilestore<S> {
&mut self,
clients_dir: &Path,
location: Location,
not_before: Option<(&Path, bool)>,
not_before: &NotBefore,
) -> Result<Option<(DirEntry, ReadDirState)>> {
let fs = self.store.fs(location);
let dir = self.actual_path(clients_dir)?;
Expand All @@ -172,19 +173,13 @@ impl<S: Store> ClientFilestore<S> {
// Option<usize, Result<DirEntry>> -> ??
.map(|(i, entry)| (i, entry.unwrap()))
// if there is a "not_before" entry, skip all entries before it.
.find(|(_, entry)| {
if let Some((not_before, require_equal)) = not_before {
if require_equal {
entry.file_name() == not_before
} else {
match entry.file_name().cmp_str(not_before) {
Ordering::Less => false,
Ordering::Equal | Ordering::Greater => true,
}
}
} else {
true
}
.find(|(_, entry)| match not_before {
NotBefore::None => true,
NotBefore::Filename(path) => entry.file_name() == &**path,
NotBefore::FilenamePart(path) => match entry.file_name().cmp_str(path) {
Ordering::Less => false,
Ordering::Equal | Ordering::Greater => true,
},
})
// if there is an entry, construct the state that needs storing out of it,
// remove the prefix from the entry's path to not leak implementation details to
Expand Down Expand Up @@ -446,7 +441,7 @@ impl<S: Store> Filestore for ClientFilestore<S> {
&mut self,
clients_dir: &Path,
location: Location,
not_before: Option<(&Path, bool)>,
not_before: &NotBefore,
) -> Result<Option<(DirEntry, ReadDirState)>> {
self.read_dir_first_impl(clients_dir, location, not_before)
}
Expand Down

0 comments on commit 06d0c6f

Please sign in to comment.