rust-key-paths 1.0.7

ReadableKeyPath, WritableKeyPath and EnumKeypath for struct and enums in Rust.
Documentation

🔑 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 feature rich crate lets you work with struct fields and enum variants as first-class values.


✨ Features

  • ✅ Readable/Writable keypaths for struct fields
  • ✅ Failable keypaths for Option<T> chains (_fr/_fw)
  • ✅ Enum CasePaths (readable and writable prisms)
  • ✅ Composition across structs, options and enum cases
  • ✅ Iteration helpers over collections via keypaths
  • ✅ Proc-macros: #[derive(Keypath)] for structs/tuple-structs and enums, #[derive(Casepaths)] for enums

📦 Installation

[dependencies]
key-paths-core = "1.0.5"
key-paths-derive = "0.9"

🎯 Choose Your Macro

#[derive(Keypath)] - Simple & Beginner-Friendly

  • One method per field: field_name()
  • Smart keypath selection: Automatically chooses readable or failable readable based on field type
  • No option chaining: Perfect for beginners and simple use cases
  • Clean API: Just call Struct::field_name() and you're done!
use key_paths_derive::Keypath;

#[derive(Keypath)]
struct User {
    name: String,           // -> User::name() returns readable keypath
    email: Option<String>,  // -> User::email() returns failable readable keypath
}

// Usage
let user = User { name: "Alice".into(), email: Some("alice@example.com".into()) };
let name_keypath = User::name();
let email_keypath = User::email();
let name = name_keypath.get(&user);        // Some("Alice")
let email = email_keypath.get(&user);      // Some("alice@example.com")

#[derive(Keypaths)] - Advanced & Feature-Rich

  • Multiple methods per field: field_r(), field_w(), field_fr(), field_fw(), field_o(), field_fo()
  • Full control: Choose exactly which type of keypath you need
  • Option chaining: Perfect for intermediate and advanced developers
  • Comprehensive: Supports all container types and access patterns
use key_paths_derive::Keypaths;

#[derive(Keypaths)]
struct User {
    name: String,
    email: Option<String>,
}

// Usage - you choose the exact method
let user = User { name: "Alice".into(), email: Some("alice@example.com".into()) };
let name_keypath = User::name_r();
let email_keypath = User::email_fr();
let name = name_keypath.get(&user);      // Some("Alice") - readable
let email = email_keypath.get(&user);   // Some("alice@example.com") - failable readable

Recommendation: Start with #[derive(Keypath)] for simplicity, upgrade to #[derive(Keypaths)] when you need more control!

Keypath vs Keypaths - When to Use Which?

Feature #[derive(Keypath)] #[derive(Keypaths)]
API Complexity Simple - one method per field Advanced - multiple methods per field
Learning Curve Beginner-friendly Requires understanding of keypath types
Container Support Basic containers only Full container support including Result, Mutex, RwLock, Weak
Option Chaining No - smart selection only Yes - full control over failable vs non-failable
Writable Access Limited Full writable support
Use Case Simple field access, beginners Complex compositions, advanced users

When to use Keypath:

  • You're new to keypaths
  • You want simple, clean field access
  • You don't need complex option chaining
  • You're working with basic types

When to use Keypaths:

  • You need full control over keypath types
  • You're composing complex nested structures
  • You need writable access to fields
  • You're working with advanced container types

🚀 Examples

See examples/ for many runnable samples. Below are a few highlights.

Quick Start - Simple Keypath Usage

use key_paths_derive::Keypath;

#[derive(Keypath)]
struct User {
    name: String,
    age: u32,
    email: Option<String>,
}

fn main() {
    let user = User {
        name: "Alice".to_string(),
        age: 30,
        email: Some("alice@example.com".to_string()),
    };

    // Access fields using keypaths
    let name_keypath = User::name();
    let age_keypath = User::age();
    let email_keypath = User::email();
    
    let name = name_keypath.get(&user);        // Some("Alice")
    let age = age_keypath.get(&user);          // Some(30)
    let email = email_keypath.get(&user);      // Some("alice@example.com")

    println!("Name: {:?}", name);
    println!("Age: {:?}", age);
    println!("Email: {:?}", email);
}

Widely used - Deeply nested struct

use key_paths_core::KeyPaths;
use key_paths_derive::{Casepaths, Keypath};

#[derive(Debug, Keypath)]
struct SomeComplexStruct {
    scsf: Option<SomeOtherStruct>,
    // scsf2: Option<SomeOtherStruct>,
}

impl SomeComplexStruct {
    fn new() -> Self {
        Self {
            scsf: Some(SomeOtherStruct {
                sosf: OneMoreStruct {
                    omsf: String::from("no value for now"),
                    omse: SomeEnum::B(DarkStruct { dsf: String::from("dark field") }),
                },
            }),
        }
    }
}

#[derive(Debug, Keypath)]
struct SomeOtherStruct {
    sosf: OneMoreStruct,
}

#[derive(Debug, Casepaths)]
enum SomeEnum {
    A(String), 
    B(DarkStruct)
}

#[derive(Debug, Keypath)]
struct OneMoreStruct {
    omsf: String,
    omse: SomeEnum
}

#[derive(Debug, Keypath)]
struct DarkStruct {
    dsf: String
}

fn main() {    
    let op = SomeComplexStruct::scsf()
        .then(SomeOtherStruct::sosf())
        .then(OneMoreStruct::omse())
        .then(SomeEnum::b_case_w())
        .then(DarkStruct::dsf());
    let mut instance = SomeComplexStruct::new();
    let omsf = op.get_mut(&mut instance);
    *omsf.unwrap() =
        String::from("we can change the field with the other way unlocked by keypaths");
    println!("instance = {:?}", instance);

}

Iteration via keypaths

use key_paths_core::KeyPaths;

#[derive(Debug)]
struct Size {
   width: u32,
   height: u32,
}
#[derive(Debug)]
enum Color {
   Red,
   Green,
   Blue,
   Other(RGBU8),
}
#[derive(Debug)]
struct RGBU8(u8, u8, u8);

#[derive(Debug)]
struct ABox {
   name: String,
   size: Size,
   color: Color,
}
#[derive(Debug)]
struct Rectangle {
   size: Size,
   name: String,
}
fn main() {
   let mut a_box = ABox {
       name: String::from("A box"),
       size: Size {
           width: 10,
           height: 20,
       },
       color: Color::Other(
           RGBU8(10, 20, 30)
       ),
   };

   let color_kp: KeyPaths<ABox, Color> = KeyPaths::failable_writable(|x: &mut ABox| Some(&mut x.color));
   let case_path = KeyPaths::writable_enum(
       {
           |v| Color::Other(v)
       },
       |p: &Color| match p {
           Color::Other(rgb) => Some(rgb),
           _ => None,
       },
       |p: &mut Color| match p {
           Color::Other(rgb) => Some(rgb),
           _ => None,
       },

   );
   
   println!("{:?}", a_box);
   let color_rgb_kp = color_kp.compose(case_path);
   if let Some(value) = color_rgb_kp.get_mut(&mut a_box) {
       *value = RGBU8(0, 0, 0);
   }
   println!("{:?}", a_box);
}
/*
ABox { name: "A box", size: Size { width: 10, height: 20 }, color: Other(RGBU8(10, 20, 30)) }
ABox { name: "A box", size: Size { width: 10, height: 20 }, color: Other(RGBU8(0, 0, 0)) }
*/

📦 Container Adapters & References (NEW!)

KeyPaths now support smart pointers, containers, and references via adapter methods:

Smart Pointer Adapters

Use .for_arc(), .for_box(), or .for_rc() to adapt keypaths for wrapped types:

use key_paths_derive::Keypath;
use std::sync::Arc;

#[derive(Keypath)]
struct Product {
    name: String,
    price: f64,
}

let products: Vec<Arc<Product>> = vec![
    Arc::new(Product { name: "Laptop".into(), price: 999.99 }),
];

// Adapt keypath to work with Arc<Product>
let price_path = Product::price().for_arc();

let affordable: Vec<&Arc<Product>> = products
    .iter()
    .filter(|p| price_path.get(p).map_or(false, |&price| price < 100.0))
    .collect();

Reference Support

Use .get_ref() and .get_mut_ref() for collections of references:

use key_paths_derive::Keypath;

#[derive(Keypath)]
struct Product {
    name: String,
    price: f64,
}

let products: Vec<&Product> = hashmap.values().collect();
let price_path = Product::price();

for product_ref in &products {
    if let Some(&price) = price_path.get_ref(product_ref) {
        println!("Price: ${}", price);
    }
}

Supported Adapters:

  • .for_arc() - Works with Arc<T> (read-only)
  • .for_box() - Works with Box<T> (read & write)
  • .for_rc() - Works with Rc<T> (read-only)
  • .get_ref() - Works with &T references
  • .get_mut_ref() - Works with &mut T references

Examples:

Documentation: See CONTAINER_ADAPTERS.md and REFERENCE_SUPPORT.md


🔗 Helpful Links & Resources


💡 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

  • Compose across structs, options and enum cases
  • Derive macros for automatic keypath generation (Keypaths, Keypath, Casepaths)
  • Optional chaining with failable keypaths
  • Smart pointer adapters (.for_arc(), .for_box(), .for_rc())
  • Container support for Result, Mutex, RwLock, Weak, and collections
  • Helper derive macros (ReadableKeypaths, WritableKeypaths)
  • [] Derive macros for complex multi-field enum variants

📜 License

  • Mozilla Public License 2.0