ftui-runtime 0.3.1

Elm-style runtime loop and subscriptions for FrankenTUI.
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Bidirectional lenses for state-widget binding.
//!
//! A lens focuses on a part of a larger structure, providing both read
//! (view) and write (set) access with algebraic guarantees:
//!
//! - **GetPut**: Setting the value you just read is a no-op.
//! - **PutGet**: Reading after a set returns the value you set.
//!
//! # Usage
//!
//! ```
//! use ftui_runtime::lens::{Lens, field_lens, compose};
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct Config { volume: u8, brightness: u8 }
//!
//! let volume = field_lens(
//!     |c: &Config| c.volume,
//!     |c: &mut Config, v| c.volume = v,
//! );
//!
//! let mut config = Config { volume: 50, brightness: 100 };
//! assert_eq!(volume.view(&config), 50);
//!
//! volume.set(&mut config, 75);
//! assert_eq!(config.volume, 75);
//! ```
//!
//! # Composition
//!
//! Lenses compose for nested state access:
//!
//! ```
//! use ftui_runtime::lens::{Lens, field_lens, compose};
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct App { settings: Settings }
//!
//! #[derive(Clone, Debug, PartialEq)]
//! struct Settings { font_size: u16 }
//!
//! let settings = field_lens(
//!     |a: &App| a.settings.clone(),
//!     |a: &mut App, s| a.settings = s,
//! );
//! let font_size = field_lens(
//!     |s: &Settings| s.font_size,
//!     |s: &mut Settings, v| s.font_size = v,
//! );
//!
//! let app_font_size = compose(settings, font_size);
//!
//! let mut app = App { settings: Settings { font_size: 14 } };
//! assert_eq!(app_font_size.view(&app), 14);
//!
//! app_font_size.set(&mut app, 18);
//! assert_eq!(app.settings.font_size, 18);
//! ```

/// A bidirectional lens focusing on part `A` of a whole `S`.
///
/// # Laws
///
/// A well-behaved lens satisfies:
/// 1. **GetPut**: `lens.set(s, lens.view(s))` leaves `s` unchanged.
/// 2. **PutGet**: After `lens.set(s, a)`, `lens.view(s)` returns `a`.
pub trait Lens<S, A> {
    /// View the focused part.
    fn view(&self, whole: &S) -> A;

    /// Set the focused part, mutating the whole in place.
    fn set(&self, whole: &mut S, part: A);

    /// Modify the focused part with a function.
    fn over(&self, whole: &mut S, f: impl FnOnce(A) -> A)
    where
        A: Clone,
    {
        let current = self.view(whole);
        self.set(whole, f(current));
    }

    /// Compose this lens with an inner lens to focus deeper.
    fn then<B, L2: Lens<A, B>>(self, inner: L2) -> Composed<Self, L2, A>
    where
        Self: Sized,
        A: Clone,
    {
        Composed {
            outer: self,
            inner,
            _mid: std::marker::PhantomData,
        }
    }
}

/// A lens built from getter and setter closures.
pub struct FieldLens<G, P> {
    getter: G,
    putter: P,
}

impl<S, A, G, P> Lens<S, A> for FieldLens<G, P>
where
    G: Fn(&S) -> A,
    P: Fn(&mut S, A),
{
    fn view(&self, whole: &S) -> A {
        (self.getter)(whole)
    }

    fn set(&self, whole: &mut S, part: A) {
        (self.putter)(whole, part);
    }
}

/// Create a lens from getter and setter functions.
///
/// # Example
///
/// ```
/// use ftui_runtime::lens::{Lens, field_lens};
///
/// struct Point { x: f64, y: f64 }
///
/// let x_lens = field_lens(|p: &Point| p.x, |p: &mut Point, v| p.x = v);
///
/// let mut p = Point { x: 1.0, y: 2.0 };
/// assert_eq!(x_lens.view(&p), 1.0);
/// x_lens.set(&mut p, 3.0);
/// assert_eq!(p.x, 3.0);
/// ```
pub fn field_lens<S, A>(
    getter: impl Fn(&S) -> A + 'static,
    setter: impl Fn(&mut S, A) + 'static,
) -> FieldLens<impl Fn(&S) -> A, impl Fn(&mut S, A)> {
    FieldLens {
        getter,
        putter: setter,
    }
}

/// Composed lens: outer ∘ inner.
///
/// First uses `outer` to access an intermediate value, then `inner` to
/// access the final target within it.
pub struct Composed<L1, L2, B> {
    outer: L1,
    inner: L2,
    _mid: std::marker::PhantomData<B>,
}

impl<S, B, A, L1, L2> Lens<S, A> for Composed<L1, L2, B>
where
    L1: Lens<S, B>,
    L2: Lens<B, A>,
    B: Clone,
{
    fn view(&self, whole: &S) -> A {
        let mid = self.outer.view(whole);
        self.inner.view(&mid)
    }

    fn set(&self, whole: &mut S, part: A) {
        let mut mid = self.outer.view(whole);
        self.inner.set(&mut mid, part);
        self.outer.set(whole, mid);
    }
}

/// Compose two lenses: `outer` then `inner`.
///
/// The resulting lens focuses on `inner`'s target through `outer`'s target.
pub fn compose<S, B, A, L1, L2>(outer: L1, inner: L2) -> Composed<L1, L2, B>
where
    L1: Lens<S, B>,
    L2: Lens<B, A>,
    B: Clone,
{
    Composed {
        outer,
        inner,
        _mid: std::marker::PhantomData,
    }
}

/// Identity lens that focuses on the whole value.
pub struct Identity;

impl<S: Clone> Lens<S, S> for Identity {
    fn view(&self, whole: &S) -> S {
        whole.clone()
    }

    fn set(&self, whole: &mut S, part: S) {
        *whole = part;
    }
}

/// Lens into the first element of a tuple.
pub struct Fst;

impl<A: Clone, B: Clone> Lens<(A, B), A> for Fst {
    fn view(&self, whole: &(A, B)) -> A {
        whole.0.clone()
    }

    fn set(&self, whole: &mut (A, B), part: A) {
        whole.0 = part;
    }
}

/// Lens into the second element of a tuple.
pub struct Snd;

impl<A: Clone, B: Clone> Lens<(A, B), B> for Snd {
    fn view(&self, whole: &(A, B)) -> B {
        whole.1.clone()
    }

    fn set(&self, whole: &mut (A, B), part: B) {
        whole.1 = part;
    }
}

/// Lens into a Vec element by index.
///
/// # Panics
///
/// Panics if the index is out of bounds.
pub struct AtIndex {
    index: usize,
}

impl AtIndex {
    pub fn new(index: usize) -> Self {
        Self { index }
    }
}

impl<T: Clone> Lens<Vec<T>, T> for AtIndex {
    fn view(&self, whole: &Vec<T>) -> T {
        whole[self.index].clone()
    }

    fn set(&self, whole: &mut Vec<T>, part: T) {
        whole[self.index] = part;
    }
}

/// Create a lens that focuses on a Vec element by index.
pub fn at_index(index: usize) -> AtIndex {
    AtIndex::new(index)
}

/// A prism for optional focusing (lens that may fail to view).
///
/// Unlike a lens, a prism's `preview` returns `Option<A>` — the target
/// may not exist. Useful for enum variants and optional fields.
pub trait Prism<S, A> {
    /// Try to view the focused part. Returns `None` if the prism doesn't match.
    fn preview(&self, whole: &S) -> Option<A>;

    /// Set the focused part if the prism matches. Returns true if applied.
    fn set_if(&self, whole: &mut S, part: A) -> bool;
}

/// Prism into an `Option<T>` value.
pub struct SomePrism;

impl<T: Clone> Prism<Option<T>, T> for SomePrism {
    fn preview(&self, whole: &Option<T>) -> Option<T> {
        whole.clone()
    }

    fn set_if(&self, whole: &mut Option<T>, part: T) -> bool {
        if whole.is_some() {
            *whole = Some(part);
            true
        } else {
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone, Debug, PartialEq)]
    struct Point {
        x: f64,
        y: f64,
    }

    #[derive(Clone, Debug, PartialEq)]
    struct Line {
        start: Point,
        end: Point,
    }

    fn x_lens() -> FieldLens<impl Fn(&Point) -> f64, impl Fn(&mut Point, f64)> {
        field_lens(|p: &Point| p.x, |p: &mut Point, v| p.x = v)
    }

    fn y_lens() -> FieldLens<impl Fn(&Point) -> f64, impl Fn(&mut Point, f64)> {
        field_lens(|p: &Point| p.y, |p: &mut Point, v| p.y = v)
    }

    fn start_lens() -> FieldLens<impl Fn(&Line) -> Point, impl Fn(&mut Line, Point)> {
        field_lens(|l: &Line| l.start.clone(), |l: &mut Line, p| l.start = p)
    }

    fn end_lens() -> FieldLens<impl Fn(&Line) -> Point, impl Fn(&mut Line, Point)> {
        field_lens(|l: &Line| l.end.clone(), |l: &mut Line, p| l.end = p)
    }

    // ── Basic lens operations ───────────────────────────────────────

    #[test]
    fn view_reads_field() {
        let lens = x_lens();
        let p = Point { x: 3.0, y: 4.0 };
        assert_eq!(lens.view(&p), 3.0);
    }

    #[test]
    fn set_writes_field() {
        let lens = x_lens();
        let mut p = Point { x: 3.0, y: 4.0 };
        lens.set(&mut p, 5.0);
        assert_eq!(p.x, 5.0);
        assert_eq!(p.y, 4.0); // y unchanged
    }

    #[test]
    fn over_modifies_with_function() {
        let lens = x_lens();
        let mut p = Point { x: 3.0, y: 4.0 };
        lens.over(&mut p, |x| x * 2.0);
        assert_eq!(p.x, 6.0);
    }

    // ── Lens laws ───────────────────────────────────────────────────

    #[test]
    fn law_get_put() {
        // Setting what you got is a no-op
        let lens = x_lens();
        let mut p = Point { x: 3.0, y: 4.0 };
        let original = p.clone();
        let viewed = lens.view(&p);
        lens.set(&mut p, viewed);
        assert_eq!(p, original);
    }

    #[test]
    fn law_put_get() {
        // Getting after a set returns the set value
        let lens = x_lens();
        let mut p = Point { x: 3.0, y: 4.0 };
        lens.set(&mut p, 99.0);
        assert_eq!(lens.view(&p), 99.0);
    }

    #[test]
    fn law_put_put() {
        // Setting twice is the same as setting once with the second value
        let lens = x_lens();
        let mut p1 = Point { x: 3.0, y: 4.0 };
        let mut p2 = p1.clone();

        lens.set(&mut p1, 10.0);
        lens.set(&mut p1, 20.0);

        lens.set(&mut p2, 20.0);

        assert_eq!(p1, p2);
    }

    // ── Composition ─────────────────────────────────────────────────

    #[test]
    fn compose_view() {
        let start_x = compose(start_lens(), x_lens());
        let line = Line {
            start: Point { x: 1.0, y: 2.0 },
            end: Point { x: 3.0, y: 4.0 },
        };
        assert_eq!(start_x.view(&line), 1.0);
    }

    #[test]
    fn compose_set() {
        let start_x = compose(start_lens(), x_lens());
        let mut line = Line {
            start: Point { x: 1.0, y: 2.0 },
            end: Point { x: 3.0, y: 4.0 },
        };
        start_x.set(&mut line, 99.0);
        assert_eq!(line.start.x, 99.0);
        assert_eq!(line.start.y, 2.0); // y unchanged
        assert_eq!(line.end.x, 3.0); // end unchanged
    }

    #[test]
    fn compose_laws_hold() {
        let start_x = compose(start_lens(), x_lens());
        let mut line = Line {
            start: Point { x: 1.0, y: 2.0 },
            end: Point { x: 3.0, y: 4.0 },
        };

        // GetPut
        let original = line.clone();
        let v = start_x.view(&line);
        start_x.set(&mut line, v);
        assert_eq!(line, original);

        // PutGet
        start_x.set(&mut line, 42.0);
        assert_eq!(start_x.view(&line), 42.0);
    }

    #[test]
    fn compose_end_y() {
        let end_y = compose(end_lens(), y_lens());
        let mut line = Line {
            start: Point { x: 1.0, y: 2.0 },
            end: Point { x: 3.0, y: 4.0 },
        };
        assert_eq!(end_y.view(&line), 4.0);
        end_y.set(&mut line, 100.0);
        assert_eq!(line.end.y, 100.0);
    }

    // ── Identity lens ───────────────────────────────────────────────

    #[test]
    fn identity_view() {
        let p = Point { x: 1.0, y: 2.0 };
        assert_eq!(Identity.view(&p), p);
    }

    #[test]
    fn identity_set() {
        let mut p = Point { x: 1.0, y: 2.0 };
        let new = Point { x: 5.0, y: 6.0 };
        Identity.set(&mut p, new.clone());
        assert_eq!(p, new);
    }

    // ── Tuple lenses ────────────────────────────────────────────────

    #[test]
    fn fst_lens() {
        let mut pair = (10u32, "hello");
        assert_eq!(Fst.view(&pair), 10);
        Fst.set(&mut pair, 20);
        assert_eq!(pair, (20, "hello"));
    }

    #[test]
    fn snd_lens() {
        let mut pair = (10u32, 20u32);
        assert_eq!(Snd.view(&pair), 20);
        Snd.set(&mut pair, 30);
        assert_eq!(pair, (10, 30));
    }

    // ── AtIndex lens ────────────────────────────────────────────────

    #[test]
    fn at_index_view() {
        let v = vec![10, 20, 30];
        assert_eq!(at_index(1).view(&v), 20);
    }

    #[test]
    fn at_index_set() {
        let mut v = vec![10, 20, 30];
        at_index(1).set(&mut v, 99);
        assert_eq!(v, vec![10, 99, 30]);
    }

    #[test]
    #[should_panic]
    fn at_index_out_of_bounds() {
        let v = vec![1, 2, 3];
        at_index(5).view(&v);
    }

    // ── Prism ───────────────────────────────────────────────────────

    #[test]
    fn some_prism_preview() {
        let opt = Some(42);
        assert_eq!(SomePrism.preview(&opt), Some(42));

        let none: Option<i32> = None;
        assert_eq!(SomePrism.preview(&none), None);
    }

    #[test]
    fn some_prism_set_if_some() {
        let mut opt = Some(42);
        assert!(SomePrism.set_if(&mut opt, 99));
        assert_eq!(opt, Some(99));
    }

    #[test]
    fn some_prism_set_if_none() {
        let mut opt: Option<i32> = None;
        assert!(!SomePrism.set_if(&mut opt, 99));
        assert_eq!(opt, None);
    }

    // ── Composed lens with over ─────────────────────────────────────

    #[test]
    fn composed_over() {
        let start_x = compose(start_lens(), x_lens());
        let mut line = Line {
            start: Point { x: 10.0, y: 20.0 },
            end: Point { x: 30.0, y: 40.0 },
        };
        start_x.over(&mut line, |x| x + 5.0);
        assert_eq!(line.start.x, 15.0);
    }

    // ── Method chaining with then() ─────────────────────────────────

    #[test]
    fn then_composition() {
        let start_x = start_lens().then(x_lens());
        let line = Line {
            start: Point { x: 7.0, y: 8.0 },
            end: Point { x: 9.0, y: 10.0 },
        };
        assert_eq!(start_x.view(&line), 7.0);
    }
}