Keypaths

Derive Macro Keypaths 

Source
#[derive(Keypaths)]
{
    // Attributes available to this derive:
    #[Readable]
    #[Writable]
    #[Owned]
    #[All]
}
Expand description

Derives keypath methods for struct fields.

This macro generates methods to create keypaths for accessing struct fields. By default, it generates readable keypaths, but you can control which methods are generated using attributes.

§Generated Methods

For each field field_name, the following methods are generated (depending on attributes):

  • field_name_r() - Returns a KeyPath<Struct, FieldType> for non-optional fields
  • field_name_w() - Returns a WritableKeyPath<Struct, FieldType> for non-optional fields
  • field_name_fr() - Returns an OptionalKeyPath<Struct, InnerType> for optional/container fields
  • field_name_fw() - Returns a WritableOptionalKeyPath<Struct, InnerType> for optional/container fields
  • field_name_fr_at(index) - Returns an OptionalKeyPath for indexed access (Vec, HashMap, etc.)
  • field_name_fw_at(index) - Returns a WritableOptionalKeyPath for indexed mutable access
  • field_name_o() - Returns a KeyPath for owned access (when #[Owned] is used)
  • field_name_fo() - Returns an OptionalKeyPath for owned optional access

§Attributes

§Struct-level attributes:

  • #[All] - Generate all methods (readable, writable, and owned)
  • #[Readable] - Generate only readable methods (default)
  • #[Writable] - Generate only writable methods
  • #[Owned] - Generate only owned methods

§Field-level attributes:

  • #[Readable] - Generate readable methods for this field only
  • #[Writable] - Generate writable methods for this field only
  • #[Owned] - Generate owned methods for this field only
  • #[All] - Generate all methods for this field

§Supported Field Types

The macro automatically handles various container types:

  • Option<T> - Generates failable keypaths
  • Vec<T> - Generates keypaths with iteration support
  • Box<T>, Rc<T>, Arc<T> - Generates keypaths that dereference
  • HashMap<K, V>, BTreeMap<K, V> - Generates key-based access methods
  • Result<T, E> - Generates failable keypaths for Ok variant
  • Tuple structs - Generates f0_r(), f1_r(), etc. for each field

§Examples

use keypaths_proc::Keypaths;

#[derive(Keypaths)]
#[All]  // Generate all methods
struct User {
    name: String,
    age: Option<u32>,
    tags: Vec<String>,
}

// Usage:
let name_path = User::name_r();  // KeyPath<User, String>
let age_path = User::age_fr();   // OptionalKeyPath<User, u32>
let tags_path = User::tags_r();  // KeyPath<User, Vec<String>>

let user = User {
    name: "Alice".to_string(),
    age: Some(30),
    tags: vec!["admin".to_string()],
};

// Read values
let name = name_path.get(&user);
let age = age_path.get(&user);  // Returns Option<&u32>

§Field-level Control

#[derive(Keypaths)]
struct Config {
    #[Readable]  // Only readable methods for this field
    api_key: String,

    #[Writable]  // Only writable methods for this field
    counter: u32,

    #[All]  // All methods for this field
    settings: Option<Settings>,
}