🔑 KeyPaths & CasePaths in Rust
Key paths and case paths provide a safe, composable way to access and modify nested data in Rust. Inspired by Swift’s KeyPath / CasePath system, this crate lets you work with struct fields and enum variants as first-class values.
✨ Features
- ✅ ReadableKeyPath → safely read struct fields.
- ✅ WritableKeyPath → safely read/write struct fields.
- ✅ EnumKeyPath (CasePaths) → extract and embed enum variants.
- ✅ Composable → chain key paths together(Upcoming).
- ✅ Iterable → iterate or mutate values across collections.
- ✅ Macros → concise
readable_keypath!
,writable_keypath!
,enum_keypath!
.
📦 Installation
[]
= "0.1"
🚀 Examples
1. CasePaths with Enums
use enum_keypath;
use EnumKeyPath;
2. Readable KeyPaths
use Readable;
use ReadableKeyPath;
use readable_keypath;
3. Writable KeyPaths
use writable_keypath;
use WritableKeyPath;
use Readable;
use Writable;
🔗 Helpful Links & Resources
- 📘 Swift KeyPath documentation
- 📘 Swift CasePath library (pointfreeco)
- 📘 Elm Architecture & Functional Lenses
- 📘 Rust Macros Book
- 📘 Category Theory in FP (for intuition)
💡 Why use KeyPaths?
- Avoids repetitive
match
/.
chains. - Encourages compositional design.
- Plays well with DDD (Domain-Driven Design) and Actor-based systems.
- Useful for reflection-like behaviors in Rust (without unsafe).
🛠Roadmap
-
zip
support for combining multiple key paths (Upcoming). - Derive macros for automatic KeyPath generation (Upcoming).
- Nested struct & enum traversal (Upcoming).
- Optional chaining (
User?.profile?.name
) (Upcoming).
📜 License
- Mozilla Public License 2.0