diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-05 12:58:02 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-05 13:03:36 +1300 |
commit | 80da2af821385b2fc89091e9ac37a047349da4bd (patch) | |
tree | 2ba50368301e041f8d1b99145ab0a1fe28f91571 /src/errors/file_error.rs | |
parent | 8d11be64f6c1747e7c4049105a6dd4ea9ab0d27f (diff) | |
download | assembler-80da2af821385b2fc89091e9ac37a047349da4bd.zip |
Implement source unit compilation, symbol resolution, error reporting
This library can now carry out all stages of assembly from collecting
source fragments to resolving symbols to pruning unused libraries to
generating a single compiled source file.
Pretty-printing of state has also been implemented in this library.
The source tree hierarchy, symbol resolution errors, and file read
errors can all be printed in a tidy format.
Diffstat (limited to 'src/errors/file_error.rs')
-rw-r--r-- | src/errors/file_error.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/errors/file_error.rs b/src/errors/file_error.rs new file mode 100644 index 0000000..e601f94 --- /dev/null +++ b/src/errors/file_error.rs @@ -0,0 +1,41 @@ +pub use std::path::{Path, PathBuf}; + + +pub enum FileError { + InvalidExtension, + NotFound, + NotReadable, + IsADirectory, + InvalidUtf8, + Unknown, +} + +impl std::fmt::Debug for FileError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + let message = match self { + Self::InvalidExtension => "File has invalid extension", + Self::NotFound => "File was not found", + Self::InvalidUtf8 => "File does not contain valid UTF-8 text", + Self::NotReadable => "File is not readable", + Self::IsADirectory => "File is a directory", + Self::Unknown => "Unknown error while attempting to read from path", + }; + write!(f, "{message}") + } +} + + +pub fn read_file(path: &Path) -> Result<String, FileError> { + match std::fs::read(&path) { + Ok(bytes) => match String::from_utf8(bytes) { + Ok(source) => Ok(source), + Err(_) => return Err(FileError::InvalidUtf8), + } + Err(err) => return Err( match err.kind() { + std::io::ErrorKind::NotFound => FileError::NotFound, + std::io::ErrorKind::PermissionDenied => FileError::NotReadable, + std::io::ErrorKind::IsADirectory => FileError::IsADirectory, + _ => FileError::Unknown, + } ) + } +} |