phlow 3.0.0

An engine for scripting reactive browsers in Rust by adding custom views to structures
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
use std::any::type_name;
use std::borrow::Cow;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::sync::Arc;

use crate::{AnySendObject, BaseView, DefiningMethod, ObjectRef, PhlowView, ViewInstance};

type NameComputation<T> = dyn for<'a> Fn(&'a T) -> Cow<'a, str> + Send + Sync + 'static;
type RowSendComputation<T> = dyn for<'a> Fn(&'a T) -> Option<AnySendObject> + Send + Sync + 'static;
type ItemTextComputation<Item> = dyn for<'a> Fn(&'a Item) -> Cow<'a, str> + Send + Sync + 'static;
type SentItemComputation<Item, SentItem> =
    dyn for<'a> Fn(&'a Item) -> Option<SentItem> + Send + Sync + 'static;
type InstanceSendComputation =
    dyn for<'a> Fn(ObjectRef<'a>) -> Option<AnySendObject> + Send + Sync + 'static;

#[repr(transparent)]
pub struct ProtoInfoView<T: ?Sized>(BaseView, PhantomData<fn() -> T>);

impl<T: ?Sized> ProtoInfoView<T> {
    pub fn new(defining_method: Option<DefiningMethod>) -> Self {
        Self(BaseView::new(defining_method), PhantomData)
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.0.title = title.into();
        self
    }

    pub fn priority(mut self, priority: usize) -> Self {
        self.0.priority = priority;
        self
    }
}

pub struct Row<T: ?Sized, Item> {
    name_computation: Arc<NameComputation<T>>,
    item_computation: ItemComputation<T, Item>,
    send_computation: Option<Arc<RowSendComputation<T>>>,
    text_computation: Arc<ItemTextComputation<Item>>,
}

pub struct InfoView<T: ?Sized, I, Item> {
    previous_row: Arc<I>,
    row: Row<T, Item>,
}

impl<Item, T: ?Sized, I: InfoRow<T>> InfoView<T, I, Item> {}

pub trait InfoRow<T: ?Sized>: Send + Sync {
    fn row<Next, Builder>(
        self,
        builder: impl FnOnce(ProtoRowBuilder<T>) -> Builder,
    ) -> InfoView<T, Self, Next>
    where
        Self: Sized,
        Builder: RowBuilder<T, Next>,
    {
        let builder = builder(ProtoRowBuilder {
            phantom_data: Default::default(),
        });
        InfoView {
            previous_row: Arc::new(self),
            row: builder.into_row(),
        }
    }

    fn proto_view(&self) -> &ProtoInfoView<T>;
    fn compute_row_text<'a>(&self, object: &'a T) -> (Cow<'a, str>, String);
    fn compute_item_to_send(&self, object: &T) -> Option<AnySendObject>;
    fn accumulate_rows(&self, rows: &mut Vec<Arc<dyn InfoRow<T>>>);
}

pub trait RowBuilder<T: ?Sized, Item> {
    fn into_row(self) -> Row<T, Item>;
}

pub struct ProtoRowBuilder<T: ?Sized> {
    phantom_data: PhantomData<T>,
}

pub struct RowBuilderWithName<T: ?Sized> {
    name_computation: Arc<NameComputation<T>>,
}

pub struct RowBuilderWithoutSend<T: ?Sized, Item: ?Sized> {
    name_computation: Arc<NameComputation<T>>,
    item_computation: ItemComputation<T, Item>,
    text_computation: Arc<ItemTextComputation<Item>>,
}

pub struct RowBuilderWithSend<T: ?Sized, Item: ?Sized, SentItem: Send> {
    name_computation: Arc<NameComputation<T>>,
    item_computation: ItemComputation<T, Item>,
    send_computation: Arc<SentItemComputation<Item, SentItem>>,
    text_computation: Arc<ItemTextComputation<Item>>,
}

impl<T: ?Sized, Item> RowBuilder<T, Item> for RowBuilderWithoutSend<T, Item> {
    fn into_row(self) -> Row<T, Item> {
        Row {
            name_computation: self.name_computation,
            item_computation: self.item_computation,
            send_computation: None,
            text_computation: self.text_computation,
        }
    }
}

impl<T: 'static + ?Sized, Item: 'static, SentItem: Send + 'static> RowBuilder<T, Item>
    for RowBuilderWithSend<T, Item, SentItem>
{
    fn into_row(self) -> Row<T, Item> {
        Row {
            name_computation: self.name_computation,
            item_computation: self.item_computation.clone(),
            send_computation: Some(Arc::new(move |object| {
                self.item_computation.value_do(object, |item| {
                    (self.send_computation)(item).map(AnySendObject::new)
                })
            })),
            text_computation: self.text_computation,
        }
    }
}

impl<T: ?Sized> ProtoRowBuilder<T> {
    pub fn named(self, name: impl Into<String>) -> RowBuilderWithName<T> {
        let cow = Cow::from(name.into());
        RowBuilderWithName {
            name_computation: Arc::new(move |_item| cow.clone()),
        }
    }

    pub fn named_str(self, name: &'static str) -> RowBuilderWithName<T> {
        RowBuilderWithName {
            name_computation: Arc::new(move |_item| Cow::from(name)),
        }
    }

    pub fn name(self, f: impl Fn(&T) -> String + Send + Sync + 'static) -> RowBuilderWithName<T> {
        RowBuilderWithName {
            name_computation: Arc::new(move |item| f(item).into()),
        }
    }

    pub fn name_str(self, f: impl Fn(&T) -> &str + Send + Sync + 'static) -> RowBuilderWithName<T> {
        RowBuilderWithName {
            name_computation: Arc::new(move |item| f(item).into()),
        }
    }
}

impl<T: ?Sized> RowBuilderWithName<T> {
    /// Uses a borrowed item derived from the receiver.
    ///
    /// Prefer this over cloning into `item(...)` when you want to expose an existing field, for
    /// example `item_ref(|value| &value.field)`.
    ///
    /// Keep the real domain item here and convert it to a string later in `text(...)` or
    /// `text_str(...)`. That keeps `send_*` working on the actual item instead of on its string
    /// representation.
    pub fn item_ref<Next>(
        self,
        item: impl Fn(&T) -> &Next + Send + Sync + 'static,
    ) -> RowBuilderWithoutSend<T, Next> {
        RowBuilderWithoutSend {
            name_computation: self.name_computation,
            item_computation: ItemComputation::Ref(Arc::new(item)),
            text_computation: Arc::new(|_item| type_name::<Next>().into()),
        }
    }

    pub fn item<Next>(
        self,
        item: impl Fn(&T) -> Next + Send + Sync + 'static,
    ) -> RowBuilderWithoutSend<T, Next> {
        // Use this for real domain values. Prefer string conversion in `text(...)` so `send_*`
        // operates on the original item value.
        RowBuilderWithoutSend {
            name_computation: self.name_computation,
            item_computation: ItemComputation::Owned(Arc::new(item)),
            text_computation: Arc::new(|_item| type_name::<Next>().into()),
        }
    }
}

impl<T: ?Sized, Item: ?Sized> RowBuilderWithoutSend<T, Item> {
    pub fn text(mut self, text: impl Fn(&Item) -> String + Send + Sync + 'static) -> Self {
        self.text_computation = Arc::new(move |item| text(item).into());
        self
    }

    pub fn text_str(mut self, text: impl Fn(&Item) -> &str + Send + Sync + 'static) -> Self {
        self.text_computation = Arc::new(move |item| text(item).into());
        self
    }

    pub fn send<SentItem: Send + Sync>(
        self,
        send: impl Fn(&Item) -> SentItem + Send + Sync + 'static,
    ) -> RowBuilderWithSend<T, Item, SentItem> {
        RowBuilderWithSend {
            name_computation: self.name_computation,
            item_computation: self.item_computation,
            send_computation: Arc::new(move |item| Some(send(item))),
            text_computation: self.text_computation,
        }
    }
}

impl<T: ?Sized, Item: Copy + Send + Sync> RowBuilderWithoutSend<T, Item> {
    pub fn send_copy(self) -> RowBuilderWithSend<T, Item, Item> {
        self.send(|item| *item)
    }
}

impl<T: ?Sized, Item: Clone + Send + Sync> RowBuilderWithoutSend<T, Item> {
    /// Sends a cloned item.
    ///
    /// Prefer this over `send(|item| item.clone())` when the item itself should be sent.
    pub fn send_clone(self) -> RowBuilderWithSend<T, Item, Item> {
        self.send(|item| item.clone())
    }
}

impl<T: ?Sized, Item: Send + Sync> RowBuilderWithoutSend<T, Option<Item>> {
    /// Sends a derived item only when the row item is `Some(...)`.
    ///
    /// This API is intentionally only available for `Option<T>` row items. Use `item(...)` or
    /// `item_ref(...)` to keep the row item as `Option<T>`, then `text(...)` to render it.
    pub fn try_send<SentItem: Send + Sync>(
        self,
        send: impl Fn(&Item) -> SentItem + Send + Sync + 'static,
    ) -> RowBuilderWithSend<T, Option<Item>, SentItem> {
        RowBuilderWithSend {
            name_computation: self.name_computation,
            item_computation: self.item_computation,
            send_computation: Arc::new(move |item| item.as_ref().map(&send)),
            text_computation: self.text_computation,
        }
    }
}

impl<T: ?Sized, Item: Copy + Send + Sync> RowBuilderWithoutSend<T, Option<Item>> {
    /// Sends the inner copied item only when the row item is `Some(...)`.
    pub fn try_send_copy(self) -> RowBuilderWithSend<T, Option<Item>, Item> {
        self.try_send(|item| *item)
    }
}

impl<T: ?Sized, Item: Clone + Send + Sync> RowBuilderWithoutSend<T, Option<Item>> {
    /// Sends the inner cloned item only when the row item is `Some(...)`.
    ///
    /// Prefer this over `try_send(|item| item.clone())` when the row item is `Option<T>` and the
    /// sent object should be the contained domain item, not its string representation.
    pub fn try_send_clone(self) -> RowBuilderWithSend<T, Option<Item>, Item> {
        self.try_send(|item| item.clone())
    }
}

impl<T: ?Sized, Item: ?Sized, SentItem: Send> RowBuilderWithSend<T, Item, SentItem> {
    pub fn text(mut self, text: impl Fn(&Item) -> String + Send + Sync + 'static) -> Self {
        self.text_computation = Arc::new(move |item| text(item).into());
        self
    }

    pub fn text_str(mut self, text: impl Fn(&Item) -> &str + Send + Sync + 'static) -> Self {
        self.text_computation = Arc::new(move |item| text(item).into());
        self
    }
}

impl<Item: 'static, T: ?Sized + 'static, I: InfoRow<T> + 'static> InfoRow<T>
    for InfoView<T, I, Item>
{
    fn proto_view(&self) -> &ProtoInfoView<T> {
        self.previous_row.proto_view()
    }

    fn compute_row_text<'a>(&self, object: &'a T) -> (Cow<'a, str>, String) {
        let row_name = (self.row.name_computation)(object);
        let row_str = self.row.item_computation.value_do(object, |row_item| {
            (self.row.text_computation)(row_item).to_string()
        });

        (row_name, row_str)
    }

    fn compute_item_to_send(&self, object: &T) -> Option<AnySendObject> {
        self.row
            .send_computation
            .as_ref()
            .and_then(|send_computation| send_computation(object))
    }

    fn accumulate_rows(&self, rows: &mut Vec<Arc<dyn InfoRow<T>>>) {
        self.previous_row.accumulate_rows(rows);
        rows.push(Arc::new(self.clone()));
    }
}

impl<T: ?Sized> InfoRow<T> for ProtoInfoView<T> {
    fn proto_view(&self) -> &ProtoInfoView<T> {
        self
    }

    fn compute_row_text<'a>(&self, _object: &'a T) -> (Cow<'a, str>, String) {
        ("".into(), "".to_string())
    }

    fn compute_item_to_send(&self, _object: &T) -> Option<AnySendObject> {
        None
    }

    fn accumulate_rows(&self, _rows: &mut Vec<Arc<dyn InfoRow<T>>>) {}
}

impl<Item, T: ?Sized, I: InfoRow<T>> Debug for InfoView<T, I, Item> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(type_name::<Self>()).finish()
    }
}

impl<T: ?Sized> Clone for ProtoInfoView<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone(), PhantomData)
    }
}

impl<T: ?Sized, Item> Clone for Row<T, Item> {
    fn clone(&self) -> Self {
        Self {
            name_computation: self.name_computation.clone(),
            item_computation: self.item_computation.clone(),
            send_computation: self.send_computation.clone(),
            text_computation: self.text_computation.clone(),
        }
    }
}

impl<Item, T: ?Sized, I: InfoRow<T>> Clone for InfoView<T, I, Item> {
    fn clone(&self) -> Self {
        Self {
            previous_row: self.previous_row.clone(),
            row: self.row.clone(),
        }
    }
}

enum ItemComputation<T: ?Sized, Item: ?Sized> {
    Owned(Arc<dyn Fn(&T) -> Item + Send + Sync>),
    Ref(Arc<dyn for<'a> Fn(&'a T) -> &'a Item + Send + Sync>),
}

impl<T: ?Sized, Item> ItemComputation<T, Item> {
    fn value_do<R>(&self, value: &T, block: impl Fn(&Item) -> R) -> R {
        match self {
            ItemComputation::Owned(f) => block(&f(value)),
            ItemComputation::Ref(f) => block(f(value)),
        }
    }
}

impl<T: ?Sized, Item: ?Sized> Clone for ItemComputation<T, Item> {
    fn clone(&self) -> Self {
        match self {
            ItemComputation::Owned(f) => Self::Owned(f.clone()),
            ItemComputation::Ref(f) => Self::Ref(f.clone()),
        }
    }
}

impl<Item: 'static, T: 'static, I: InfoRow<T> + 'static> PhlowView for InfoView<T, I, Item> {
    fn get_title(&self) -> &str {
        self.proto_view().0.title.as_str()
    }

    fn get_priority(&self) -> usize {
        self.proto_view().0.priority
    }

    fn get_view_type(&self) -> &str {
        Self::view_type()
    }

    fn get_defining_method(&self) -> Option<&DefiningMethod> {
        self.proto_view().0.defining_method.as_ref()
    }

    fn create_instance(&self, object: ObjectRef<'_>) -> Box<dyn ViewInstance> {
        // SAFETY: the view is only instantiated with the receiver type it was defined for.
        let object = unsafe { object.cast::<T>() };
        let mut row_views = Vec::new();
        self.accumulate_rows(&mut row_views);

        let rows = row_views
            .into_iter()
            .map(|row| {
                let (name, text) = row.compute_row_text(object);
                InfoRowInstance {
                    name: name.into_owned(),
                    text,
                    send_computation: Arc::new(move |object| {
                        row.compute_item_to_send(unsafe { object.cast() })
                    }),
                }
            })
            .collect();

        Box::new(InfoViewInstance {
            base_view: self.proto_view().0.clone(),
            rows,
        })
    }

    fn view_type() -> &'static str
    where
        Self: Sized,
    {
        "info_view"
    }
}

pub struct InfoRowInstance {
    pub name: String,
    pub text: String,
    pub send_computation: Arc<InstanceSendComputation>,
}

pub struct InfoViewInstance {
    pub base_view: BaseView,
    pub rows: Vec<InfoRowInstance>,
}

impl ViewInstance for InfoViewInstance {
    fn get_title(&self) -> &str {
        self.base_view.title.as_str()
    }

    fn get_priority(&self) -> usize {
        self.base_view.priority
    }

    fn get_view_type(&self) -> &str {
        "info_view"
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}