Skip to content

Commit 37869ef

Browse files
committed
enum case path added
1 parent b46c743 commit 37869ef

File tree

1 file changed

+115
-30
lines changed

1 file changed

+115
-30
lines changed

key-paths-core/src/lib.rs

Lines changed: 115 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use std::rc::Rc;
33
// #[derive(Clone)]
44
pub enum KeyPaths<Root, Value> {
55
Readable(Rc<dyn for<'a> Fn(&'a Root) -> &'a Value>),
6-
Prism {
6+
ReadableEnum {
77
extract: Rc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value>>,
88
embed: Rc<dyn Fn(Value) -> Root>,
99
},
1010
FailableReadable(Rc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value>>),
1111

1212
Writable(Rc<dyn for<'a> Fn(&'a mut Root) -> &'a mut Value>),
1313
FailableWritable(Rc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value>>),
14-
PrismMut {
14+
WritableEnum {
1515
extract: Rc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value>>,
1616
extract_mut: Rc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value>>,
1717
embed: Rc<dyn Fn(Value) -> Root>,
@@ -39,22 +39,22 @@ impl<Root, Value> KeyPaths<Root, Value> {
3939
Self::FailableWritable(Rc::new(get_mut))
4040
}
4141

42-
pub fn prism(
42+
pub fn readable_enum(
4343
embed: impl Fn(Value) -> Root + 'static,
4444
extract: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + 'static,
4545
) -> Self {
46-
Self::Prism {
46+
Self::ReadableEnum {
4747
extract: Rc::new(extract),
4848
embed: Rc::new(embed),
4949
}
5050
}
5151

52-
pub fn prism_mut(
52+
pub fn writable_enum(
5353
embed: impl Fn(Value) -> Root + 'static,
5454
extract: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + 'static,
5555
extract_mut: impl for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + 'static,
5656
) -> Self {
57-
Self::PrismMut {
57+
Self::WritableEnum {
5858
extract: Rc::new(extract),
5959
embed: Rc::new(embed),
6060
extract_mut: Rc::new(extract_mut),
@@ -70,8 +70,8 @@ impl<Root, Value> KeyPaths<Root, Value> {
7070
KeyPaths::Writable(_) => None, // Writable requires mut
7171
KeyPaths::FailableReadable(f) => f(root),
7272
KeyPaths::FailableWritable(_) => None, // needs mut
73-
KeyPaths::Prism { extract, .. } => extract(root),
74-
KeyPaths::PrismMut { extract, .. } => extract(root),
73+
KeyPaths::ReadableEnum { extract, .. } => extract(root),
74+
KeyPaths::WritableEnum { extract, .. } => extract(root),
7575
}
7676
}
7777

@@ -82,7 +82,7 @@ impl<Root, Value> KeyPaths<Root, Value> {
8282
KeyPaths::Writable(f) => Some(f(root)),
8383
KeyPaths::FailableReadable(_) => None, // immutable only
8484
KeyPaths::FailableWritable(f) => f(root),
85-
KeyPaths::PrismMut { extract_mut, .. } => extract_mut(root),
85+
KeyPaths::WritableEnum { extract_mut, .. } => extract_mut(root),
8686
_ => None,
8787
}
8888
}
@@ -92,7 +92,7 @@ impl<Root, Value> KeyPaths<Root, Value> {
9292
Value: Clone,
9393
{
9494
match self {
95-
KeyPaths::Prism { embed, .. } => Some(embed(value)),
95+
KeyPaths::ReadableEnum { embed, .. } => Some(embed(value)),
9696
_ => None,
9797
}
9898
}
@@ -102,7 +102,7 @@ impl<Root, Value> KeyPaths<Root, Value> {
102102
Value: Clone,
103103
{
104104
match self {
105-
KeyPaths::PrismMut { embed, .. } => Some(embed(value)),
105+
KeyPaths::WritableEnum { embed, .. } => Some(embed(value)),
106106
_ => None,
107107
}
108108
}
@@ -139,8 +139,8 @@ impl<Root, Value> KeyPaths<Root, Value> {
139139
KeyPaths::Writable(_) => None,
140140
KeyPaths::FailableReadable(f) => f(&root).map(|v| v.clone().into_iter()),
141141
KeyPaths::FailableWritable(_) => None,
142-
KeyPaths::Prism { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
143-
KeyPaths::PrismMut { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
142+
KeyPaths::ReadableEnum { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
143+
KeyPaths::WritableEnum { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
144144
}
145145
}
146146
}
@@ -181,71 +181,156 @@ where
181181
FailableWritable(Rc::new(move |r| f1(r).and_then(|m| f2(m))))
182182
}
183183

184-
(Prism { extract, .. }, Readable(f2)) => {
184+
(ReadableEnum { extract, .. }, Readable(f2)) => {
185185
FailableReadable(Rc::new(move |r| extract(r).map(|m| f2(m))))
186186
}
187187

188-
(Prism { extract, .. }, FailableReadable(f2)) => {
188+
(ReadableEnum { extract, .. }, FailableReadable(f2)) => {
189189
FailableReadable(Rc::new(move |r| extract(r).and_then(|m| f2(m))))
190190
}
191191

192-
(PrismMut { extract, .. }, Readable(f2)) => {
192+
(WritableEnum { extract, .. }, Readable(f2)) => {
193193
FailableReadable(Rc::new(move |r| extract(r).map(|m| f2(m))))
194194
}
195195

196-
(PrismMut { extract, .. }, FailableReadable(f2)) => {
196+
(WritableEnum { extract, .. }, FailableReadable(f2)) => {
197197
FailableReadable(Rc::new(move |r| extract(r).and_then(|m| f2(m))))
198198
}
199199

200-
(PrismMut { extract_mut, .. }, Writable(f2)) => {
200+
(WritableEnum { extract_mut, .. }, Writable(f2)) => {
201201
FailableWritable(Rc::new(move |r| extract_mut(r).map(|m| f2(m))))
202202
}
203203

204-
(PrismMut { extract_mut, .. }, FailableWritable(f2)) => {
204+
// (FailableWritable(f2), WritableEnum { extract_mut, .. }) => {
205+
// // FailableWritable(Rc::new(move |r|
206+
// // {
207+
// // // let mut x = extract_mut(r);
208+
// // // x.as_mut().map(|m| f2(m))
209+
// // // extract_mut(r).map(|m| f2(m))
210+
// // // extract_mut(r).and_then(|m| f2(m))
211+
// // // let x = f2(m);
212+
// // extract_mut(r).and_then(|a| f2(a))
213+
// //
214+
// // }
215+
// //
216+
// // ))
217+
// // FailableWritable(Rc::new(move |r| extract_mut(r).and_then(|a| f2(a))))
218+
// // FailableWritable(Rc::new(move |r: &mut Root| {
219+
// // match extract_mut(r) {
220+
// // Some(mid) => f2(mid), // mid: &mut Mid -> Option<&mut Value>
221+
// // None => None,
222+
// // }
223+
// // }) as Rc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value>>)
224+
//
225+
// FailableWritable(Rc::new(move |r: &mut Root| {
226+
// // First extract the intermediate value using extract_mut
227+
// extract_mut(r).and_then(|intermediate| {
228+
// // Now apply f2 to the intermediate value
229+
// // f2 expects &mut Value but intermediate is &mut Value
230+
// f2(intermediate)
231+
// })
232+
// }))
233+
// }
234+
235+
// (WritableEnum { extract_mut, .. }, FailableWritable(f2)) => {
236+
// FailableWritable(Rc::new(move |r: &mut Root| {
237+
// // Extract the intermediate Mid value
238+
// let mid_ref = extract_mut(r)?;
239+
// // Apply the second function to get the final Value
240+
// f2(mid_ref)
241+
// }))
242+
// }
243+
244+
// (FailableWritable(f2), WritableEnum { extract_mut, .. }) => {
245+
// FailableWritable(Rc::new(move |r: &mut Root| {
246+
// // Extract the intermediate Mid value
247+
// let mid_ref = extract_mut(r)?;
248+
// // Apply the second function to get the final Value
249+
// f2(mid_ref)
250+
// }))
251+
// }
252+
253+
/* (FailableWritable(f2), WritableEnum { extract_mut, .. }) => {
254+
FailableWritable(Rc::new(move |r: &mut Root| {
255+
extract_mut(r).and_then(|intermediate_mid| f2(intermediate_mid))
256+
}))
257+
}
258+
*/
259+
// (FailableWritable(f2), WritableEnum { extract_mut, .. }) => {
260+
// // Here's the fix: f2 must be a function that operates on a Mid and returns a Value
261+
// // It is already of this type since the 'mid' KeyPaths is KeyPaths<Mid, Value>
262+
// FailableWritable(Rc::new(move |r: &mut Root| {
263+
// extract_mut(r).and_then(|intermediate_mid| f2(intermediate_mid))
264+
// }))
265+
// }
266+
267+
// (FailableWritable(f2), WritableEnum { extract_mut, .. }) => {
268+
// FailableWritable(Rc::new(move |r: &mut Root| -> Option<&mut Value> {
269+
// // Extract the intermediate Mid value
270+
// let mid_ref: &mut Mid = extract_mut(r).unwrap();
271+
// // Apply the second function to get the final Value
272+
// f2(mid_ref)
273+
// }))
274+
// }
275+
276+
(FailableWritable(f_root_mid), WritableEnum { extract_mut: exm_mid_val, .. }) => {
277+
FailableWritable(Rc::new(move |r: &mut Root| {
278+
// First, apply the function that operates on Root.
279+
// This will give us `Option<&mut Mid>`.
280+
let intermediate_mid_ref = f_root_mid(r);
281+
282+
// Then, apply the function that operates on Mid.
283+
// This will give us `Option<&mut Value>`.
284+
intermediate_mid_ref.and_then(|intermediate_mid| exm_mid_val(intermediate_mid))
285+
}))
286+
}
287+
288+
289+
(WritableEnum { extract_mut, .. }, FailableWritable(f2)) => {
205290
FailableWritable(Rc::new(move |r| extract_mut(r).and_then(|m| f2(m))))
206291
}
207292

208293
(
209-
Prism {
294+
ReadableEnum {
210295
extract: ex1,
211296
embed: em1,
212297
},
213-
Prism {
298+
ReadableEnum {
214299
extract: ex2,
215300
embed: em2,
216301
},
217-
) => Prism {
302+
) => ReadableEnum {
218303
extract: Rc::new(move |r| ex1(r).and_then(|m| ex2(m))),
219304
embed: Rc::new(move |v| em1(em2(v))),
220305
},
221306

222307
(
223-
PrismMut {
308+
WritableEnum {
224309
extract: ex1,
225310
extract_mut,
226311
embed: em1,
227312
},
228-
Prism {
313+
ReadableEnum {
229314
extract: ex2,
230315
embed: em2,
231316
},
232-
) => Prism {
317+
) => ReadableEnum {
233318
extract: Rc::new(move |r| ex1(r).and_then(|m| ex2(m))),
234319
embed: Rc::new(move |v| em1(em2(v))),
235320
},
236321

237322
(
238-
PrismMut {
323+
WritableEnum {
239324
extract: ex1,
240325
extract_mut: exm1,
241326
embed: em1,
242327
},
243-
PrismMut {
328+
WritableEnum {
244329
extract: ex2,
245330
extract_mut: exm2,
246331
embed: em2,
247332
},
248-
) => PrismMut {
333+
) => WritableEnum {
249334
extract: Rc::new(move |r| ex1(r).and_then(|m| ex2(m))),
250335
extract_mut: Rc::new(move |r| exm1(r).and_then(|m| exm2(m))),
251336
embed: Rc::new(move |v| em1(em2(v))),
@@ -267,7 +352,7 @@ fn kind_name<Root, Value>(k: &KeyPaths<Root, Value>) -> &'static str {
267352
Writable(_) => "Writable",
268353
FailableReadable(_) => "FailableReadable",
269354
FailableWritable(_) => "FailableWritable",
270-
Prism { .. } => "Prism",
271-
PrismMut { .. } => "PrismMut",
355+
ReadableEnum { .. } => "ReadableEnum",
356+
WritableEnum { .. } => "WritableEnum",
272357
}
273358
}

0 commit comments

Comments
 (0)