From 65b53003e8de9543ba25a3b3d3cace399b92dc1d Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 6 Aug 2024 19:11:46 +1200 Subject: 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. --- src/devices/file/buffered_file.rs | 44 ++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src/devices/file/buffered_file.rs') diff --git a/src/devices/file/buffered_file.rs b/src/devices/file/buffered_file.rs index 04fefbd..73d3536 100644 --- a/src/devices/file/buffered_file.rs +++ b/src/devices/file/buffered_file.rs @@ -3,27 +3,6 @@ use std::io::{BufReader, BufWriter}; use std::io::{Read, Write}; use std::io::{ErrorKind, Seek, SeekFrom}; -enum AccessMode { - Read(BufReader), - Write(BufWriter), - None, -} - -impl AccessMode { - pub fn unwrap(self) -> File { - match self { - Self::Read(reader) => reader.into_inner(), - Self::Write(writer) => writer.into_inner().unwrap(), - Self::None => unreachable!(), - } - } -} - -impl Default for AccessMode { - fn default() -> Self { - Self::None - } -} pub struct BufferedFile { file: AccessMode, @@ -135,3 +114,26 @@ impl BufferedFile { }; } } + + +enum AccessMode { + Read(BufReader), + Write(BufWriter), + None, +} + +impl AccessMode { + pub fn unwrap(self) -> File { + match self { + Self::Read(reader) => reader.into_inner(), + Self::Write(writer) => writer.into_inner().unwrap(), + Self::None => unreachable!(), + } + } +} + +impl Default for AccessMode { + fn default() -> Self { + Self::None + } +} -- cgit v1.2.3-70-g09d2