diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-08-06 19:11:46 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-08-06 19:12:43 +1200 |
commit | 65b53003e8de9543ba25a3b3d3cace399b92dc1d (patch) | |
tree | 90ba625f4522553136e55b452c9f2f91f29d9753 /src/devices/file/directory_entry.rs | |
parent | 406b4d3ea56a19c0389b4214a7d8d15bf3028eb8 (diff) | |
download | bedrock-pc-65b53003e8de9543ba25a3b3d3cace399b92dc1d.zip |
Refactor the file device
The major feature of this refactor is the creation of BedrockFilePath,
a type which can be losslessly converted into an operating-system
specific path or a Bedrock-style byte path. It also prevents file paths
which can't be represented as Bedrock-style byte paths from being
constructed, and allows the use of a base directory which acts as an
inescapable sandbox.
BedrockFilePath will support the creation of a virtual root directory
on Windows which will allow navigation between drive letters via the
standard hierarchical navigation ports.
This commit has been tested on Linux, but not yet Windows. Further work
is expected before the new code will work on Windows.
Diffstat (limited to 'src/devices/file/directory_entry.rs')
-rw-r--r-- | src/devices/file/directory_entry.rs | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/src/devices/file/directory_entry.rs b/src/devices/file/directory_entry.rs deleted file mode 100644 index 7f0e69b..0000000 --- a/src/devices/file/directory_entry.rs +++ /dev/null @@ -1,57 +0,0 @@ -use super::*; - -use std::cmp::Ordering; - -#[derive(PartialEq, Eq)] -pub struct DirectoryChild { - pub byte_path: Vec<u8>, - pub entry_type: EntryType, -} - -impl PartialOrd for DirectoryChild { - fn partial_cmp(&self, other: &Self) -> Option<Ordering> { - Some(self.cmp(other)) - } -} - -impl Ord for DirectoryChild { - fn cmp(&self, other: &Self) -> Ordering { - match self.entry_type.cmp(&other.entry_type) { - Ordering::Equal => - compare_ascii_arrays(&self.byte_path,&other.byte_path), - other => other, - } - } -} - -// Compare two ASCII arrays in case-agnostic alphabetic order. -fn compare_ascii_arrays(left: &[u8], right: &[u8]) -> Ordering { - let l = std::cmp::min(left.len(), right.len()); - let lhs = &left[..l]; - let rhs = &right[..l]; - - for i in 0..l { - let a = remap_ascii(lhs[i]); - let b = remap_ascii(rhs[i]); - match a.cmp(&b) { - Ordering::Equal => (), - non_eq => return non_eq, - } - } - - left.len().cmp(&right.len()) -} - -// Remap ASCII values so that they sort in case-agnostic alphabetic order: -// !"#$%&'()*+,-./0123456789:;<=>? -// @`AaBbCcDdEeFfGgHhIiJjKkLlMmNnOo -// PpQqRrSsTtUuVvWwXxYyZz[{\|]}^~_ -fn remap_ascii(c: u8) -> u8 { - if 0x40 <= c && c <= 0x5F { - (c - 0x40) * 2 + 0x40 - } else if 0x60 <= c && c <= 0x7F { - (c - 0x60) * 2 + 0x41 - } else { - c - } -} |