Lutra/crates/vfs/src/error.rs
danny 5d49b57b9c M1.2: vfs with ignore rules, polling watcher and safe file IO
- Project::open canonicalises the root (no \?\ prefix on Windows where
  safe); typed errors for missing and non-directory paths.
- Lazy children(dir): dirs first, case-insensitive; kind, ignored flag
  and role (vendor/ library, node_modules/ excluded, .git hidden).
- Ignore rules via the ignore crate: .gitignore, .ignore,
  .git/info/exclude, global git excludes, with git precedence.
- Watcher: std-only polling (100 ms, watched dirs only) with
  create/modify/remove/rename events and a Failed event on root loss.
  notify is CC0-1.0 and fails cargo deny; owner decision pending.
- read_text with a 32 MiB cap, UTF-8 + BOM, typed InvalidUtf8 and
  UnsupportedEncoding; write_atomic via temp file + rename, keeping
  permissions and refusing read-only files.
- 28 tests on temp dirs (30 on Unix: permission and symlink write tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 19:24:02 +02:00

103 lines
3.5 KiB
Rust

//! 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(),
},
}
}
}