anyfs_backend/lib.rs
1//! # anyfs-backend
2//!
3//! Core traits and types for the **AnyFS pluggable virtual filesystem standard**.
4//!
5//! This crate provides the foundational API that filesystem backends implement.
6//! It contains **only trait definitions and types** — no concrete implementations.
7//! Implementations live in the `anyfs` crate.
8//!
9//! ---
10//!
11//! ## Quick Start
12//!
13//! Most users only need [`Fs`] — it covers 90% of use cases.
14//!
15//! A typical usage pattern with any backend that implements `Fs`:
16//!
17//! ```rust
18//! use anyfs_backend::Fs;
19//! use std::path::Path;
20//!
21//! // Generic function that works with any Fs implementation
22//! fn work_with_files<B: Fs>(backend: &B) -> Result<(), anyfs_backend::FsError> {
23//! let data = backend.read(Path::new("/input.txt"))?;
24//! backend.write(Path::new("/output.txt"), &data)?;
25//! backend.create_dir_all(Path::new("/archive/2024"))?;
26//! for entry in backend.read_dir(Path::new("/"))? {
27//! println!("{}", entry?.name);
28//! }
29//! Ok(())
30//! }
31//! ```
32//!
33//! See the `anyfs` crate for concrete backend implementations.
34//!
35//! ---
36//!
37//! ## Core Types
38//!
39//! | Type | Purpose |
40//! |------|---------|
41//! | [`Fs`] | Basic filesystem trait — read, write, and directory operations |
42//! | [`FsFull`] | Extended filesystem — adds links, permissions, sync, stats |
43//! | [`FsFuse`] | FUSE-mountable — adds inode-based operations |
44//! | [`FsPosix`] | Full POSIX — adds handles, locks, extended attributes |
45//! | [`FsError`] | Comprehensive error type with context |
46//! | [`Metadata`] | File/directory metadata (size, type, times, permissions) |
47//! | [`DirEntry`] | Single directory listing entry |
48//!
49//! ---
50//!
51//! ## Which Trait Should I Use?
52//!
53//! **[`Fs`]** — When you need basic file operations.
54//! - Use for: Config files, data serialization, file processing, simple I/O
55//! - Methods: `read`, `write`, `create_dir`, `read_dir`, `exists`, `metadata`
56//! - Coverage: **90% of use cases**
57//!
58//! **[`FsFull`]** — When you need filesystem features beyond basic I/O.
59//! - Use for: Backup tools, file managers, archive extraction
60//! - Adds: `symlink`, `hard_link`, `set_permissions`, `sync`, `statfs`
61//! - Includes: Everything in [`Fs`]
62//!
63//! **[`FsFuse`]** — When building a FUSE filesystem.
64//! - Use for: Userspace mounts, virtual drives, network filesystems
65//! - Adds: `path_to_inode`, `inode_to_path`, `lookup`, `metadata_by_inode`
66//! - Includes: Everything in [`FsFull`]
67//!
68//! **[`FsPosix`]** — When you need full POSIX semantics.
69//! - Use for: Database storage, lock-based coordination, xattr metadata
70//! - Adds: `open`/`close` handles, `lock`/`unlock`, `get_xattr`/`set_xattr`
71//! - Includes: Everything in [`FsFuse`]
72//!
73//! ---
74//!
75//! ## Complete Trait Reference
76//!
77//! ### Component Traits (What You Implement)
78//!
79//! | Trait | Provides | Key Methods | When to Use |
80//! |-------|----------|-------------|-------------|
81//! | [`FsRead`] | Read operations | `read`, `exists`, `metadata` | Always (core) |
82//! | [`FsWrite`] | Write operations | `write`, `remove_file`, `rename` | Always (core) |
83//! | [`FsDir`] | Directory ops | `read_dir`, `create_dir` | Always (core) |
84//! | [`FsLink`] | Symlinks/hardlinks | `symlink`, `hard_link` | Backup tools |
85//! | [`FsPermissions`] | Permission mgmt | `set_permissions` | File managers |
86//! | [`FsSync`] | Disk sync | `sync`, `sync_path` | Databases |
87//! | [`FsStats`] | FS statistics | `statfs` | Disk monitoring |
88//! | [`FsInode`] | Inode ↔ path | `path_to_inode`, `lookup` | FUSE filesystems |
89//! | [`FsHandles`] | File handles | `open`, `read_at`, `write_at` | Random access |
90//! | [`FsLock`] | File locking | `lock`, `unlock` | Multi-process |
91//! | [`FsXattr`] | Extended attrs | `get_xattr`, `set_xattr` | Metadata storage |
92//! | [`FsPath`] | Path resolution | `canonicalize` | Symlink handling |
93//!
94//! ### Composite Traits (What You Use in Bounds)
95//!
96//! | Trait | Combines | Typical Consumer |
97//! |-------|----------|------------------|
98//! | [`Fs`] | `FsRead + FsWrite + FsDir` | Generic file code |
99//! | [`FsFull`] | `Fs + FsLink + FsPermissions + FsSync + FsStats` | File managers |
100//! | [`FsFuse`] | `FsFull + FsInode` | FUSE implementations |
101//! | [`FsPosix`] | `FsFuse + FsHandles + FsLock + FsXattr` | Databases, POSIX apps |
102//!
103//! ---
104//!
105//! ## Trait Hierarchy
106//!
107//! ```text
108//! Layer 1 (Core): FsRead + FsWrite + FsDir = Fs
109//! ↓
110//! Layer 2 (Extended): Fs + FsLink + FsPermissions + FsSync + FsStats = FsFull
111//! ↓
112//! Layer 3 (FUSE): FsFull + FsInode = FsFuse
113//! ↓
114//! Layer 4 (POSIX): FsFuse + FsHandles + FsLock + FsXattr = FsPosix
115//! ```
116//!
117//! All composite traits ([`Fs`], [`FsFull`], [`FsFuse`], [`FsPosix`]) have **blanket
118//! implementations**. Just implement the component traits and you get the composite
119//! trait for free.
120//!
121//! ---
122//!
123//! ## Error Handling
124//!
125//! All operations return `Result<T, FsError>`. Errors include context:
126//!
127//! ```rust
128//! use anyfs_backend::FsError;
129//! use std::path::PathBuf;
130//!
131//! // Errors include the path that caused the problem
132//! let err = FsError::NotFound { path: PathBuf::from("/missing.txt") };
133//! assert_eq!(err.to_string(), "not found: /missing.txt");
134//!
135//! // Permission errors include the operation
136//! let err = FsError::PermissionDenied {
137//! path: PathBuf::from("/secret"),
138//! operation: "read",
139//! };
140//! assert_eq!(err.to_string(), "read: permission denied: /secret");
141//! ```
142//!
143//! ---
144//!
145//! ## Thread Safety
146//!
147//! All traits require `Send + Sync`. Methods take `&self` (not `&mut self`),
148//! enabling safe concurrent access. Backends use interior mutability internally.
149//!
150//! You can safely share a backend across threads using `Arc<B>` and spawn
151//! concurrent operations without explicit locking at the call site.
152//!
153//! ---
154//!
155//! ## Feature Flags
156//!
157//! | Feature | Description |
158//! |---------|-------------|
159//! | `serde` | Enable serialization for [`Metadata`], [`DirEntry`], [`Permissions`], etc. |
160//!
161//! ---
162//!
163//! ## Crate Organization
164//!
165//! This crate (`anyfs-backend`) contains **only traits and types**.
166//!
167//! For concrete implementations, see the `anyfs` crate which provides:
168//! - `MemoryBackend` — In-memory filesystem
169//! - `NativeBackend` — Wrapper around `std::fs`
170//! - `OverlayBackend` — UnionFS-style layering
171//! - `FileStorage` — Type-erased filesystem wrapper
172//! - Middleware (encryption, compression, caching, etc.)
173
174// Private modules
175mod error;
176mod ext;
177mod layer;
178mod markers;
179mod path_resolver;
180mod traits;
181mod types;
182
183// Public re-exports - error types
184pub use error::FsError;
185
186// Public re-exports - core types
187pub use types::{
188 DirEntry, FileType, Handle, LockType, Metadata, OpenFlags, Permissions, StatFs, ROOT_INODE,
189};
190
191// Public re-exports - Layer 1 core traits
192pub use traits::{Fs, FsDir, FsRead, FsWrite, ReadDirIter};
193
194// Public re-exports - Layer 2 extended traits
195pub use traits::{FsFull, FsLink, FsPath, FsPermissions, FsStats, FsSync};
196
197// Public re-exports - Layer 3 FUSE traits
198pub use traits::{FsFuse, FsInode};
199
200// Public re-exports - Layer 4 POSIX traits
201pub use traits::{FsHandles, FsLock, FsPosix, FsXattr};
202
203// Public re-exports - path resolution
204pub use path_resolver::PathResolver;
205
206// Public re-exports - infrastructure
207pub use ext::FsExt;
208pub use layer::{Layer, LayerExt};
209pub use markers::SelfResolving;
210
211// Conditional re-exports
212#[cfg(feature = "serde")]
213pub use ext::FsExtJson;