feat(02-01): add Phase 2 deps and vault file-loading module
- Add pulldown-cmark 0.13.1, syntect 5.3 (default-fancy), syntect-tui 3.0 to Cargo.toml - Create src/vault.rs with VaultDocument enum (Loaded/Missing/ReadError variants) - Implement load_document() using std::fs::read_to_string with structured error mapping - cargo check passes with all new dependencies resolved
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
// ── VaultDocument ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Represents the outcome of attempting to load a markdown file from the vault.
|
||||
#[allow(dead_code)]
|
||||
pub enum VaultDocument {
|
||||
/// File was read successfully.
|
||||
Loaded { path: PathBuf, content: String },
|
||||
/// File does not exist at the resolved path.
|
||||
Missing { path: PathBuf },
|
||||
/// File exists or path was valid, but an I/O error occurred during reading.
|
||||
ReadError { path: PathBuf, reason: String },
|
||||
}
|
||||
|
||||
// ── load_document ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Load a markdown document from the vault.
|
||||
///
|
||||
/// Joins `vault_path` with `relative` (e.g. `"index.md"`) to form the full
|
||||
/// file path, then reads the file to a String.
|
||||
///
|
||||
/// Returns:
|
||||
/// - `VaultDocument::Loaded` — file read successfully
|
||||
/// - `VaultDocument::Missing` — file does not exist
|
||||
/// - `VaultDocument::ReadError` — any other I/O error
|
||||
pub fn load_document(vault_path: &Path, relative: &str) -> VaultDocument {
|
||||
let full_path = vault_path.join(relative);
|
||||
|
||||
match std::fs::read_to_string(&full_path) {
|
||||
Ok(content) => VaultDocument::Loaded {
|
||||
path: full_path,
|
||||
content,
|
||||
},
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => VaultDocument::Missing { path: full_path },
|
||||
Err(e) => VaultDocument::ReadError {
|
||||
path: full_path,
|
||||
reason: e.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user