|
1 | | -# rust-key-paths |
2 | | -ReadableKeyPath and WritableKeyPath for struct and enums in Rust |
| 1 | +# 🔑 KeyPaths & CasePaths in Rust |
| 2 | + |
| 3 | +Key paths and case paths provide a **safe, composable way to access and modify nested data** in Rust. |
| 4 | +Inspired by **Swift’s KeyPath / CasePath** system, this crate lets you work with **struct fields** and **enum variants** as *first-class values*. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## ✨ Features |
| 9 | + |
| 10 | +* ✅ **ReadableKeyPath** → safely read struct fields. |
| 11 | +* ✅ **WritableKeyPath** → safely read/write struct fields. |
| 12 | +* ✅ **EnumKeyPath (CasePaths)** → extract and embed enum variants. |
| 13 | +* ✅ **Composable** → chain key paths together(Upcoming). |
| 14 | +* ✅ **Iterable** → iterate or mutate values across collections. |
| 15 | +* ✅ **Macros** → concise `readable_keypath!`, `writable_keypath!`, `enum_keypath!`. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 📦 Installation |
| 20 | + |
| 21 | +```toml |
| 22 | +[dependencies] |
| 23 | +key_paths_core = "0.1" |
| 24 | +``` |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 🚀 Examples |
| 29 | + |
| 30 | +### 1. CasePaths with Enums |
| 31 | + |
| 32 | +```rust |
| 33 | +use key_paths_core::enum_keypath; |
| 34 | +use key_paths_core::EnumKeyPath; |
| 35 | + |
| 36 | +#[derive(Debug)] |
| 37 | +struct User { |
| 38 | + id: u32, |
| 39 | + name: String, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug)] |
| 43 | +enum Status { |
| 44 | + Active(User), |
| 45 | + Inactive(()), |
| 46 | +} |
| 47 | + |
| 48 | +fn main() { |
| 49 | + let cp = enum_keypath!(Status::Active(User)); |
| 50 | + |
| 51 | + let status = Status::Active(User { |
| 52 | + id: 42, |
| 53 | + name: "Charlie".to_string(), |
| 54 | + }); |
| 55 | + |
| 56 | + if let Some(u) = cp.extract(&status) { |
| 57 | + println!("Extracted user: {:?}", u); |
| 58 | + } |
| 59 | + |
| 60 | + let new_status = cp.embed(User { |
| 61 | + id: 99, |
| 62 | + name: "Diana".to_string(), |
| 63 | + }); |
| 64 | + println!("Embedded back: {:?}", new_status); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +### 2. Readable KeyPaths |
| 71 | + |
| 72 | +```rust |
| 73 | +use key_paths_core::Readable; |
| 74 | +use key_paths_core::ReadableKeyPath; |
| 75 | +use key_paths_core::readable_keypath; |
| 76 | + |
| 77 | +#[derive(Debug)] |
| 78 | +struct User { |
| 79 | + name: String, |
| 80 | + age: u32, |
| 81 | +} |
| 82 | + |
| 83 | +fn main() { |
| 84 | + let users = vec![ |
| 85 | + User { name: "Akash".into(), age: 25 }, |
| 86 | + User { name: "Soni".into(), age: 30 }, |
| 87 | + User { name: "Neha".into(), age: 20 }, |
| 88 | + ]; |
| 89 | + |
| 90 | + let name_key = readable_keypath!(User, name); |
| 91 | + |
| 92 | + println!("Names:"); |
| 93 | + for name in name_key.iter(&users) { |
| 94 | + println!("{}", name); |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +### 3. Writable KeyPaths |
| 102 | + |
| 103 | +```rust |
| 104 | +use key_paths_core::writable_keypath; |
| 105 | +use key_paths_core::WritableKeyPath; |
| 106 | +use key_paths_core::Readable; |
| 107 | +use key_paths_core::Writable; |
| 108 | + |
| 109 | +#[derive(Debug)] |
| 110 | +struct User { |
| 111 | + name: String, |
| 112 | + age: u32, |
| 113 | +} |
| 114 | + |
| 115 | +fn main() { |
| 116 | + let mut users = vec![ |
| 117 | + User { name: "Akash".into(), age: 25 }, |
| 118 | + User { name: "Soni".into(), age: 30 }, |
| 119 | + User { name: "Neha".into(), age: 20 }, |
| 120 | + ]; |
| 121 | + |
| 122 | + let age_key = writable_keypath!(User, age); |
| 123 | + |
| 124 | + println!("Ages before:"); |
| 125 | + for age in age_key.iter(&users) { |
| 126 | + println!("{}", age); |
| 127 | + } |
| 128 | + |
| 129 | + for age in age_key.iter_mut(&mut users) { |
| 130 | + *age += 1; |
| 131 | + } |
| 132 | + |
| 133 | + println!("Ages after:"); |
| 134 | + for age in age_key.iter(&users) { |
| 135 | + println!("{}", age); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## 🔗 Helpful Links & Resources |
| 143 | + |
| 144 | +* 📘 [Swift KeyPath documentation](https://developer.apple.com/documentation/swift/keypath) |
| 145 | +* 📘 [Swift CasePath library (pointfreeco)](https://github.com/pointfreeco/swift-case-paths) |
| 146 | +* 📘 [Elm Architecture & Functional Lenses](https://guide.elm-lang.org/architecture/) |
| 147 | +* 📘 [Rust Macros Book](https://doc.rust-lang.org/book/ch19-06-macros.html) |
| 148 | +* 📘 [Category Theory in FP (for intuition)](https://bartoszmilewski.com/2014/11/24/category-the-essence-of-composition/) |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## 💡 Why use KeyPaths? |
| 153 | + |
| 154 | +* Avoids repetitive `match` / `.` chains. |
| 155 | +* Encourages **compositional design**. |
| 156 | +* Plays well with **DDD (Domain-Driven Design)** and **Actor-based systems**. |
| 157 | +* Useful for **reflection-like behaviors** in Rust (without unsafe). |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## 🛠 Roadmap |
| 162 | + |
| 163 | +* [ ] `zip` support for combining multiple key paths (Upcoming). |
| 164 | +* [ ] Derive macros for automatic KeyPath generation. |
| 165 | +* [ ] Nested struct & enum traversal. |
| 166 | +* [ ] Optional chaining (`User?.profile?.name`). |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## 📜 License |
| 171 | + |
| 172 | +* Mozilla Public License 2.0 |
0 commit comments