//! The error type of the VFS. //! //! Every fallible operation in this crate returns [`VfsError`]. It is `Clone` //! and `PartialEq` so the engine can store it, compare it in tests and turn it //! into a message for the user; I/O errors are kept as their kind plus text. use std::io; use std::path::{Path, PathBuf}; /// Everything that can go wrong in the VFS. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum VfsError { /// The path does not exist. #[error("{} does not exist", path.display())] NotFound { /// The missing path. path: PathBuf, }, /// A directory was expected but the path is something else. #[error("{} is not a directory", path.display())] NotADirectory { /// The offending path. path: PathBuf, }, /// A regular file was expected but the path is a directory or has no file name. #[error("{} is not a file", path.display())] NotAFile { /// The offending path. path: PathBuf, }, /// A project-relative path is absolute or climbs out of the project with `..`. #[error("{} is outside the project", path.display())] OutsideProject { /// The rejected path, as given by the caller. path: PathBuf, }, /// A file is larger than the read limit. #[error("{} is {size} bytes, more than the limit of {limit} bytes", path.display())] TooLarge { /// The file. path: PathBuf, /// Its size in bytes (at least `limit + 1` when the file grew while reading). size: u64, /// The limit that was exceeded. limit: u64, }, /// A file is not valid UTF-8. #[error("{} is not valid UTF-8 (first invalid byte at offset {valid_up_to})", path.display())] InvalidUtf8 { /// The file. path: PathBuf, /// Byte offset in the file (including any BOM) of the first invalid byte. valid_up_to: usize, }, /// A file starts with the byte order mark of an encoding Lutra cannot decode yet. #[error("{} is encoded as {encoding}, which is not supported yet", path.display())] UnsupportedEncoding { /// The file. path: PathBuf, /// Name of the detected encoding, e.g. `UTF-16LE`. encoding: &'static str, }, /// A file is marked read-only, so it is not overwritten. #[error("{} is read-only", path.display())] ReadOnly { /// The file. path: PathBuf, }, /// Any other I/O failure. #[error("{}: {message}", path.display())] Io { /// The path the operation was about. path: PathBuf, /// The kind of the underlying error. kind: io::ErrorKind, /// The text of the underlying error. message: String, }, /// File watching could not be started or stopped working; the project /// stays usable without it. #[error("file watching is unavailable: {message}")] WatchUnavailable { /// Why watching is unavailable. message: String, }, } impl VfsError { /// Converts an I/O error about `path`, mapping "not found" to /// [`VfsError::NotFound`] and everything else to [`VfsError::Io`]. pub(crate) fn io(path: &Path, err: io::Error) -> Self { match err.kind() { io::ErrorKind::NotFound => VfsError::NotFound { path: path.to_path_buf(), }, kind => VfsError::Io { path: path.to_path_buf(), kind, message: err.to_string(), }, } } }