pub struct EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>where
Root: Borrow<Enum>,
Value: Borrow<Variant>,
MutRoot: BorrowMut<Enum>,
MutValue: BorrowMut<Variant>,
G: Fn(Root) -> Option<Value>,
S: Fn(MutRoot) -> Option<MutValue>,
E: Fn(Variant) -> Enum,{ /* private fields */ }Expand description
EnumKp - A keypath for enum variants that supports both extraction and embedding
Leverages the existing Kp architecture where optionals are built-in via Option
This struct serves dual purposes:
- As a concrete keypath instance for extracting and embedding enum variants
- As a namespace for static factory methods:
EnumKp::for_ok(),EnumKp::for_some(), etc.
Implementations§
Source§impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
Sourcepub fn new(
extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>,
embedder: E,
) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
pub fn new( extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>, embedder: E, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
Create a new EnumKp with extractor and embedder functions
Sourcepub fn get(&self, enum_value: Root) -> Option<Value>
pub fn get(&self, enum_value: Root) -> Option<Value>
Extract the variant from an enum (returns None if wrong variant)
Sourcepub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue>
pub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue>
Extract the variant mutably from an enum (returns None if wrong variant)
Sourcepub fn get_ref<'a>(&self, enum_value: &'a Enum) -> Option<&'a Variant>
pub fn get_ref<'a>(&self, enum_value: &'a Enum) -> Option<&'a Variant>
Read when the extractor uses reference-shaped getters (e.g. #[derive(Kp)] enum variants).
Sourcepub fn embed_fn(&self) -> Ewhere
E: Copy,
pub fn embed_fn(&self) -> Ewhere
E: Copy,
Copyable embed fn when the embedder is a function pointer.
Sourcepub fn then<Leaf, G2, S2, E2>(
self,
inner: EnumKp<Variant, Leaf, &'static Variant, &'static Leaf, &'static mut Variant, &'static mut Leaf, G2, S2, E2>,
) -> EnumKp<Enum, Leaf, &'static Enum, &'static Leaf, &'static mut Enum, &'static mut Leaf, impl for<'b> Fn(&'b Enum), impl for<'b> Fn(&'b mut Enum), impl Fn(Leaf) + Copy>where
Enum: 'static,
Variant: 'static,
Leaf: 'static,
G: for<'b> Fn(&'b Enum) -> Option<&'b Variant>,
S: for<'b> Fn(&'b mut Enum) -> Option<&'b mut Variant>,
E: Fn(Variant) -> Enum + Copy,
G2: for<'b> Fn(&'b Variant) -> Option<&'b Leaf>,
S2: for<'b> Fn(&'b mut Variant) -> Option<&'b mut Leaf>,
E2: Fn(Leaf) -> Variant + Copy,
pub fn then<Leaf, G2, S2, E2>(
self,
inner: EnumKp<Variant, Leaf, &'static Variant, &'static Leaf, &'static mut Variant, &'static mut Leaf, G2, S2, E2>,
) -> EnumKp<Enum, Leaf, &'static Enum, &'static Leaf, &'static mut Enum, &'static mut Leaf, impl for<'b> Fn(&'b Enum), impl for<'b> Fn(&'b mut Enum), impl Fn(Leaf) + Copy>where
Enum: 'static,
Variant: 'static,
Leaf: 'static,
G: for<'b> Fn(&'b Enum) -> Option<&'b Variant>,
S: for<'b> Fn(&'b mut Enum) -> Option<&'b mut Variant>,
E: Fn(Variant) -> Enum + Copy,
G2: for<'b> Fn(&'b Variant) -> Option<&'b Leaf>,
S2: for<'b> Fn(&'b mut Variant) -> Option<&'b mut Leaf>,
E2: Fn(Leaf) -> Variant + Copy,
Append an inner casepath (Swift CasePaths append).
Composes extract and embed through two single-payload variants:
let cp = RootAction::app_cp()
.then(AppAction::panel_cp())
.then(PanelAction::widget_cp());
cp.get_ref(&root); // extract leaf by reference
cp.embed(WidgetAction::Submit); // embed leaf into root enumSourcepub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
pub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
Get the underlying Kp for composition with other keypaths
Sourcepub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
pub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>
Convert to Kp (loses embedding capability but gains composition)
Sourcepub fn map<MappedValue, F>(
&self,
mapper: F,
) -> EnumKp<Enum, MappedValue, Root, MappedValue, MutRoot, MappedValue, impl Fn(Root), impl Fn(MutRoot), impl Fn(MappedValue)>
pub fn map<MappedValue, F>( &self, mapper: F, ) -> EnumKp<Enum, MappedValue, Root, MappedValue, MutRoot, MappedValue, impl Fn(Root), impl Fn(MutRoot), impl Fn(MappedValue)>
Map the variant value through a transformation function
§Example
use rust_key_paths::enum_ok;
let result: Result<String, i32> = Ok("hello".to_string());
let ok_kp = enum_ok();
let len_kp = ok_kp.map(|s: &String| s.len());
assert_eq!(len_kp.get(&result), Some(5));Sourcepub fn filter<F>(
&self,
predicate: F,
) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, impl Fn(Root), impl Fn(MutRoot), E>
pub fn filter<F>( &self, predicate: F, ) -> EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, impl Fn(Root), impl Fn(MutRoot), E>
Filter the variant value based on a predicate Returns None if the predicate fails or if wrong variant
§Example
use rust_key_paths::enum_ok;
let result: Result<i32, String> = Ok(42);
let ok_kp = enum_ok();
let positive_kp = ok_kp.filter(|x: &i32| *x > 0);
assert_eq!(positive_kp.get(&result), Some(&42));