rust-overture 0.6.1

A rust overture library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Keypath utilities for functional programming
// Equivalent to Swift's keypath functions for property access and modification
// Using key-paths-core library for type-safe keypath operations

use key_paths_core::KeyPaths;

/// Produces a getter function for a given key path. Useful for composing property access with functions.
/// Equivalent to Swift's get<Root, Value>(_ keyPath: KeyPath<Root, Value>) -> (Root) -> Value
///
/// # Examples
/// ```
/// use rust_overture::keypaths::get;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::failable_owned(|person: Person| Some(person.name));
/// let get_name = get(name_keypath);
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// assert_eq!(get_name(person), Some("Alice".to_string()));
/// ```
pub fn get<Root, Value>(keypath: KeyPaths<Root, Value>) -> impl FnOnce(Root) -> Option<Value>
where
    Value: Clone,
{
    move |root: Root| {keypath.get_failable_owned(root)}
}

/// Produces an immutable setter function for a given key path. Useful for composing property changes.
/// Equivalent to Swift's prop<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>) -> (@escaping (Value) -> Value) -> (Root) -> Root
///
/// # Examples
/// ```
/// use rust_overture::keypaths::prop;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let update_age = prop(age_keypath);
/// let double_age = update_age(Box::new(|age| age * 2));
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let updated = double_age(person);
/// assert_eq!(updated.age, 60);
/// ```
pub fn prop<Root, Value>(
    keypath: KeyPaths<Root, Value>,
) -> impl Fn(Box<dyn Fn(Value) -> Value>) -> Box<dyn Fn(Root) -> Root>
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    move |update: Box<dyn Fn(Value) -> Value>| {
        let keypath = keypath.clone();
        Box::new(move |root: Root| {
            let mut copy = root.clone();
            if let Some(mut_ref) = keypath.get_mut(&mut copy) {
                let current_value = mut_ref.clone();
                let new_value = update(current_value);
                *mut_ref = new_value;
            }
            copy
        })
    }
}

/// Produces an immutable setter function for a given key path and update function.
/// Equivalent to Swift's over<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ update: @escaping (Value) -> Value) -> (Root) -> Root
///
/// # Examples
/// ```
/// use rust_overture::keypaths::over;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let double_age = over(age_keypath, |age| age * 2);
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let updated = double_age(person);
/// assert_eq!(updated.age, 60);
/// ```
pub fn over<Root, Value>(
    keypath: KeyPaths<Root, Value>,
    update: impl Fn(Value) -> Value + 'static,
) -> impl Fn(Root) -> Root
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    let prop_fn = prop(keypath);
    prop_fn(Box::new(update))
}

/// Produces an immutable setter function for a given key path and constant value.
/// Equivalent to Swift's set<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ value: Value) -> (Root) -> Root
///
/// # Examples
/// ```
/// use rust_overture::keypaths::set;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let set_age_25 = set(age_keypath, 25);
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let updated = set_age_25(person);
/// assert_eq!(updated.age, 25);
/// ```
pub fn set<Root, Value>(keypath: KeyPaths<Root, Value>, value: Value) -> impl Fn(Root) -> Root
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    over(keypath, move |_| value.clone())
}

// MARK: - Mutation

/// Produces an in-place setter function for a given key path. Useful for composing value property changes efficiently.
/// Equivalent to Swift's mprop<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>) -> (@escaping (inout Value) -> Void) -> (inout Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mprop;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let mut_update_age = mprop(age_keypath);
/// let mut double_age = mut_update_age(Box::new(|age| *age *= 2));
/// let mut person = Person { name: "Alice".to_string(), age: 30 };
/// double_age(&mut person);
/// assert_eq!(person.age, 60);
/// ```
pub fn mprop<Root, Value>(
    keypath: KeyPaths<Root, Value>,
) -> impl Fn(Box<dyn FnMut(&mut Value)>) -> Box<dyn FnMut(&mut Root)>
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    move |mut update: Box<dyn FnMut(&mut Value)>| {
        let keypath = keypath.clone();
        Box::new(move |root: &mut Root| {
            if let Some(mut_ref) = keypath.get_mut(root) {
                update(mut_ref);
            }
        })
    }
}

/// Uncurried `mver`. Takes a key path and update function all at once.
/// Equivalent to Swift's mver<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ update: @escaping (inout Value) -> Void) -> (inout Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mver;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let mut double_age = mver(age_keypath, |age| *age *= 2);
/// let mut person = Person { name: "Alice".to_string(), age: 30 };
/// double_age(&mut person);
/// assert_eq!(person.age, 60);
/// ```
pub fn mver<Root, Value>(
    keypath: KeyPaths<Root, Value>,
    update: impl FnMut(&mut Value) + 'static,
) -> impl FnMut(&mut Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    let mprop_fn = mprop(keypath);
    let boxed_update = Box::new(update);
    let mut setter_fn = mprop_fn(boxed_update);
    move |root| setter_fn(root)
}

/// Produces a reference-mutable setter function for a given key path to a reference. Useful for composing reference property changes efficiently.
/// Equivalent to Swift's mprop<Root, Value>(_ keyPath: ReferenceWritableKeyPath<Root, Value>) -> (@escaping (Value) -> Void) -> (Root) -> Void where Value: AnyObject
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mprop_ref;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::writable(|person: &mut Person| &mut person.name);
/// let mut_update_name = mprop_ref(name_keypath);
/// let mut uppercase_name = mut_update_name(Box::new(|mut name| name.make_ascii_uppercase()));
/// let person = Person { name: "alice".to_string(), age: 30 };
/// uppercase_name(person);
/// ```
pub fn mprop_ref<Root, Value>(
    keypath: KeyPaths<Root, Value>,
) -> impl Fn(Box<dyn Fn(Value)>) -> Box<dyn Fn(Root)>
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    move |update: Box<dyn Fn(Value)>| {
        let keypath = keypath.clone();
        Box::new(move |root: Root| {
            if let Some(value) = keypath.get(&root) {
                update(value.clone());
            }
        })
    }
}

/// Uncurried `mver`. Takes a key path and update function all at once.
/// Equivalent to Swift's mverObject<Root, Value>(_ keyPath: ReferenceWritableKeyPath<Root, Value>, _ update: @escaping (Value) -> Void) -> (Root) -> Void where Value: AnyObject
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mver_object;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::writable(|person: &mut Person| &mut person.name);
/// let uppercase_name = mver_object(name_keypath, |mut name| name.make_ascii_uppercase());
/// let person = Person { name: "alice".to_string(), age: 30 };
/// uppercase_name(person);
/// ```
pub fn mver_object<Root, Value>(
    keypath: KeyPaths<Root, Value>,
    update: impl Fn(Value) + 'static,
) -> impl Fn(Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    let mprop_fn = mprop_ref(keypath);
    let boxed_update = Box::new(update);
    mprop_fn(boxed_update)
}

/// Produces a reference-mutable setter function for a given key path to a value. Useful for composing reference property changes efficiently.
/// Equivalent to Swift's mprop<Root, Value>(_ keyPath: ReferenceWritableKeyPath<Root, Value>) -> (@escaping (inout Value) -> Void) -> (Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mprop_ref_mut;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::writable(|person: &mut Person| &mut person.name);
/// let mut mut_update_name = mprop_ref_mut(name_keypath);
/// let mut uppercase_name = mut_update_name(Box::new(|mut name| name.make_ascii_uppercase()));
/// let person = Person { name: "alice".to_string(), age: 30 };
/// uppercase_name(person);
/// ```
pub fn mprop_ref_mut<Root, Value>(
    keypath: KeyPaths<Root, Value>,
) -> impl FnMut(Box<dyn FnMut(&mut Value)>) -> Box<dyn Fn(Root)>
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    let keypath = keypath.clone();
    move |update: Box<dyn FnMut(&mut Value)>| {
        let keypath = keypath.clone();
        let update = std::cell::RefCell::new(update);
        Box::new(move |mut root: Root| {
            if let Some(mut_ref) = keypath.get_mut(&mut root) {
                update.borrow_mut()(mut_ref);
            }
        })
    }
}

/// Uncurried `mver`. Takes a key path and update function all at once.
/// Equivalent to Swift's mver<Root, Value>(_ keyPath: ReferenceWritableKeyPath<Root, Value>, _ update: @escaping (inout Value) -> Void) -> (Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mver_ref;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::writable(|person: &mut Person| &mut person.name);
/// let mut uppercase_name = mver_ref(name_keypath, |name| name.make_ascii_uppercase());
/// let person = Person { name: "alice".to_string(), age: 30 };
/// uppercase_name(person);
/// ```
pub fn mver_ref<Root, Value>(
    keypath: KeyPaths<Root, Value>,
    update: impl FnMut(&mut Value) + 'static,
) -> impl Fn(Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    let mut mprop_fn = mprop_ref_mut(keypath);
    let boxed_update = Box::new(update);
    mprop_fn(boxed_update)
}

/// Produces a value-mutable setter function for a given key path and new value.
/// Equivalent to Swift's mut<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ value: Value) -> (inout Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mut_set;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
/// let mut set_age_25 = mut_set(age_keypath, 25);
/// let mut person = Person { name: "Alice".to_string(), age: 30 };
/// set_age_25(&mut person);
/// assert_eq!(person.age, 25);
/// ```
pub fn mut_set<Root, Value>(keypath: KeyPaths<Root, Value>, value: Value) -> impl FnMut(&mut Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    mver(keypath, move |v| *v = value.clone())
}

/// Produces a reference-mutable setter function for a given key path and new value.
/// Equivalent to Swift's mut<Root, Value>(_ keyPath: ReferenceWritableKeyPath<Root, Value>, _ value: Value) -> (Root) -> Void
///
/// # Examples
/// ```
/// use rust_overture::keypaths::mut_set_ref;
/// use key_paths_core::KeyPaths;
///
/// #[derive(Clone)]
/// struct Person {
///     name: String,
///     age: u32,
/// }
///
/// let name_keypath = KeyPaths::writable(|person: &mut Person| &mut person.name);
/// let set_name_bob = mut_set_ref(name_keypath, "Bob".to_string());
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// set_name_bob(person);
/// ```
pub fn mut_set_ref<Root, Value>(keypath: KeyPaths<Root, Value>, value: Value) -> impl Fn(Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    mver_ref(keypath, move |v| *v = value.clone())
}

// Legacy aliases for backward compatibility
#[doc(hidden)]
pub fn mset<Root, Value>(keypath: KeyPaths<Root, Value>, value: Value) -> impl FnMut(&mut Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    mut_set(keypath, value)
}

#[doc(hidden)]
pub fn mset_ref<Root, Value>(keypath: KeyPaths<Root, Value>, value: Value) -> impl Fn(Root)
where
    Root: Clone + 'static,
    Value: Clone + 'static,
{
    mut_set_ref(keypath, value)
}