Skip to content

Commit

Permalink
Path: Add ordering comparison functions
Browse files Browse the repository at this point in the history
The ordering of path (as obtained when iterating over a directory) in Littlefs
is not exactly what is expected.

This implementation contains 2 comparision functions, one matching what is expected,
and one matching the iteration order of littlefs directories,
as described in littlefs-project/littlefs#923

The fact that directories are ordered is documented:
https://github.com/littlefs-project/littlefs/blob/f53a0cc961a8acac85f868b431d2f3e58e447ba3/SPEC.md?plain=1#L304
  • Loading branch information
sosthene-nitrokey committed Feb 5, 2024
1 parent c2876ed commit fcbdf21
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Paths

use core::{convert::TryFrom, fmt, iter::FusedIterator, marker::PhantomData, ops, ptr, slice, str};
use core::{
cmp::Ordering, convert::TryFrom, fmt, iter::FusedIterator, marker::PhantomData, ops, ptr,
slice, str,
};

use cstr_core::CStr;
use cty::{c_char, size_t};
Expand All @@ -20,6 +23,66 @@ pub struct Path {
inner: CStr,
}

impl Path {
/// Compare the path using their string representation
/// This comarison function as would be expected for a `String` type.
///
/// <div class="warning">
/// This ordering does not match the ordering obsvered when iterating over a directory.
///
/// See <a href="#method.cmp_lfs">cmp_lfs</a> and <a href = "https://github.com/littlefs-project/littlefs/issues/923">littlefs#923</a>.
/// </div>
///
/// ```
///# use std::cmp::Ordering;
///# use littlefs2::path;
/// assert_eq!(path!("some_path_a").cmp_str(path!("some_path_b")), Ordering::Less);
/// assert_eq!(path!("some_path_b").cmp_str(path!("some_path_a")), Ordering::Greater);
/// assert_eq!(path!("some_path").cmp_str(path!("some_path_a")), Ordering::Less);
/// assert_eq!(path!("some_path").cmp_str(path!("some_path_b")), Ordering::Less);
/// assert_eq!(path!("some_path").cmp_str(path!("some_path")), Ordering::Equal);
///```
pub fn cmp_str(&self, other: &Path) -> Ordering {
self.inner.cmp(&other.inner)
}

/// Compare the path using their string representation
///
/// This comparison function matches the iteration order of `littlefs` when iterating over directory.
/// For more information, see [littlefs#923](https://github.com/littlefs-project/littlefs/issues/923)
///
/// ```
///# use std::cmp::Ordering;
///# use littlefs2::path;
/// assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path_b")), Ordering::Less);
/// assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path_a")), Ordering::Greater);
/// assert_eq!(path!("some_path").cmp_lfs(path!("some_path_a")), Ordering::Greater);
/// assert_eq!(path!("some_path").cmp_lfs(path!("some_path_b")), Ordering::Greater);
/// assert_eq!(path!("some_path_a").cmp_lfs(path!("some_path")), Ordering::Less);
/// assert_eq!(path!("some_path_b").cmp_lfs(path!("some_path")), Ordering::Less);
/// assert_eq!(path!("some_path").cmp_lfs(path!("some_path")), Ordering::Equal);
///```
pub fn cmp_lfs(&self, other: &Path) -> Ordering {
let this = self.inner.to_bytes();
let other = other.inner.to_bytes();

let min_len = this.len().min(other.len());

match this[0..min_len].cmp(&other[0..min_len]) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal if this.len() != other.len() => {
if this.len() < other.len() {
Ordering::Greater
} else {
Ordering::Less
}
}
Ordering::Equal => Ordering::Equal,
}
}
}

/// Iterator over the ancestors of a Path
///
/// See documentation for [`Path::ancestors`][]
Expand Down Expand Up @@ -587,6 +650,8 @@ pub type Result<T> = core::result::Result<T, Error>;

#[cfg(test)]
mod tests {
use core::cmp::Ordering;

use super::{Path, PathBuf};
use crate::path;

Expand Down

0 comments on commit fcbdf21

Please sign in to comment.