MCPcopy Index your code
hub / github.com/cmyr/keypath

github.com/cmyr/keypath @v0.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.0 ↗ · + Follow
119 symbols 182 edges 23 files 12 documented · 10%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

keypath

Strongly typed references to arbitrarily nested fields.

This is an early experiment in implementing Swift-style keypaths in Rust. It is currently intended as a proof of concept, and is missing some fancier features such as 'partial keypaths' and composibility, although implementing these should not be especially challanging. What this does include is what I believe is the most difficult case, of generating typed keypaths for arbitrary types that are guaranteed at compile time.

This means you can do the following:

#[derive(Keyable)]
struct Person {
    name: String,
    friends: Vec<String>,
    size: Size,
}
#[derive(Keyable)]
struct Size {
    big: bool,
    heft: u8,
}

let mut person = Person {
    name: "coco".into(),
    friends: vec!["eli".into(), "nico".into(), "yaya".into()],
    size: Size { big: false, heft: 45 }
};

let first_friend: KeyPath<Person, String> = keypath!(Person.friends[0]);
let heft = keypath!(Person.size.heft);

assert_eq!(person[&first_friend], "eli");

// mutation:
person[&heft] = 101;
assert_eq!(person.size.heft, 101);

This may not seem especially useful on its own, but it is an ergonomic building block for things like UI bindings, and observable objects.

todos

There are a bunch of additional features and ideas that would be worth exploring, here:

  • allow collection access to return optionals, with chaining: People.friends[10]?.age would allow the indexing operation to fail.
  • easier implementation for collections (currently manual, no derive available)
  • support for generics in the keypath! macro

Extension points exported contracts — how you extend this code

Keyable (Interface)
A trait for types that can be indexed with keypaths. [2 implementers]
keypath/src/lib.rs
Plugin (Interface)
(no doc)
sketch.rs
RawKeyable (Interface)
A trait for types that expose their properties via keypath. All of the dynamism and traversal logic happens here; its s [2 …
keypath/src/internals.rs
Keyable (Interface)
(no doc)
sketch.rs

Core symbols most depended-on inside this repo

iter
called by 14
keypath-proc-macros/src/attr.rs
as_ref
called by 4
keypath/src/lib.rs
clone
called by 4
keypath/src/lib.rs
match_arms
called by 2
keypath-proc-macros/src/attr.rs
add_generic_bounds
called by 2
keypath-proc-macros/src/keyable.rs
parse_literal
called by 2
keypath-proc-macros/src/keypath_parse.rs
next_token
called by 2
keypath-proc-macros/src/keypath_parse.rs
path_component_tokens
called by 2
keypath-proc-macros/src/shared.rs

Shape

Class 39
Method 37
Function 31
Enum 8
Interface 4

Languages

Rust100%

Modules by API surface

keypath-proc-macros/src/keypath_parse.rs17 symbols
keypath/src/lib.rs13 symbols
keypath/src/impls.rs13 symbols
keypath-proc-macros/src/attr.rs11 symbols
sketch.rs7 symbols
keypath/tests/derive-types.rs7 symbols
keypath-proc-macros/src/shared.rs6 symbols
keypath-proc-macros/src/keyable.rs6 symbols
keypath-proc-macros/tests/keypath/basic_structs.rs5 symbols
keypath/tests/collections.rs4 symbols
keypath/src/error.rs4 symbols
keypath/tests/tuple_struct.rs3 symbols

For agents

$ claude mcp add keypath \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page