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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Client/server messages.

prelude! {}
use filter::*;

/// Chart settings message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChartSettingsMsg {
    /// Toggles a chart's visibility.
    ToggleVisible,
    /// Changes the title of a chart.
    ChangeTitle(String),
    /// Changes the display mode of a chart.
    SetDisplayMode(chart::settings::DisplayMode),
    /// Changes the resolution of a chart.
    SetResolution(chart::settings::Resolution),
}

impl ChartSettingsMsg {
    /// Toggles a chart's visibility.
    pub fn toggle_visible<Res>(uid: uid::Chart) -> Res
    where
        (uid::Chart, Self): Into<Res>,
    {
        (uid, Self::ToggleVisible).into()
    }

    /// Changes the display mode of a chart.
    pub fn set_display_mode<Res>(uid: uid::Chart, mode: chart::settings::DisplayMode) -> Res
    where
        (uid::Chart, Self): Into<Res>,
    {
        (uid, Self::SetDisplayMode(mode)).into()
    }

    /// Changes the title of a chart.
    pub fn change_title<Res>(uid: uid::Chart, title: impl Into<String>) -> Res
    where
        (uid::Chart, Self): Into<Res>,
    {
        (uid, Self::ChangeTitle(title.into())).into()
    }

    /// Changes the resolution of a chart.
    pub fn set_resolution<Res>(
        uid: uid::Chart,
        resolution: impl Into<chart::settings::Resolution>,
    ) -> Res
    where
        (uid::Chart, Self): Into<Res>,
    {
        (uid, Self::SetResolution(resolution.into())).into()
    }
}

impl fmt::Display for ChartSettingsMsg {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ToggleVisible => write!(fmt, "toggle visible"),
            Self::SetDisplayMode(mode) => write!(fmt, "set display mode: {}", mode.desc()),
            Self::ChangeTitle(title) => write!(fmt, "change title: {}", title),
            Self::SetResolution(resolution) => write!(fmt, "set resolution: {}", resolution),
        }
    }
}

/// Messages from the client to the server.
pub mod to_server {
    use super::*;

    /// A list of messages from the client to the server.
    pub type Msgs = Vec<Msg>;

    /// Messages from the client to the server.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum Msg {
        /// Operations over charts.
        Charts(ChartsMsg),

        /// Operation over filters.
        Filters(FiltersMsg),
    }
    impl fmt::Display for Msg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::Charts(msg) => write!(fmt, "charts({})", msg),
                Self::Filters(msg) => write!(fmt, "filters({})", msg),
            }
        }
    }

    impl Msg {
        /// Encodes the message as bytes.
        pub fn to_bytes(&self) -> Res<Vec<u8>> {
            Ok(base::bincode::serialize(self)?)
        }

        /// Decodes the message from bytes.
        pub fn from_bytes(bytes: &[u8]) -> Res<Self> {
            Ok(base::bincode::deserialize(bytes)?)
        }
    }

    base::implement! {
        impl Msg {
            From {
                from (uid::Chart, ChartMsg) => |pair| Self::Charts(ChartsMsg::from(pair)),
                from (uid::Chart, ChartSettingsMsg) => |pair| Self::Charts(ChartsMsg::from(pair)),
                from FiltersMsg => |msg| Self::Filters(msg),
                from ChartsMsg => |msg| Self::Charts(msg),
            }

            Into {
                to yew::format::Text => |self| anyhow::bail!(
                    "trying to encode a message as text, only binary is supported"
                ),
                to yew::format::Binary => |self| match self.to_bytes() {
                    Ok(bytes) => Ok(bytes),
                    Err(e) => anyhow::bail!("{}", e),
                }
            }
        }
    }

    /// Operations over charts.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum ChartsMsg {
        /// Creates a new chart.
        New(chart::axis::XAxis, chart::axis::YAxis),
        /// Reloads all charts.
        Reload,
        /// An update for a specific chart.
        ChartUpdate {
            /// UID of the chart the message is for.
            uid: uid::Chart,
            /// Actual message.
            msg: ChartMsg,
        },
        /// New value for the global charts settings.
        Settings(settings::Charts),
    }
    impl fmt::Display for ChartsMsg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::New(_, _) => write!(fmt, "new chart"),
                Self::Reload => write!(fmt, "reload"),
                Self::ChartUpdate { uid, msg } => write!(fmt, "update({}, {})", uid, msg),
                Self::Settings(_) => write!(fmt, "new settings"),
            }
        }
    }
    impl ChartsMsg {
        /// Constructs a chart creation message.
        pub fn new(x: chart::axis::XAxis, y: chart::axis::YAxis) -> Msg {
            Self::New(x, y).into()
        }
        /// Reloads all charts.
        pub fn reload() -> Msg {
            Self::Reload.into()
        }
        /// New global chart settings.
        pub fn settings(settings: settings::Charts) -> Msg {
            Self::Settings(settings).into()
        }
    }

    base::implement! {
        impl ChartsMsg {
            From {
                from (uid::Chart, ChartMsg) => |(uid, msg)| Self::ChartUpdate { uid, msg },
                from (uid::Chart, ChartSettingsMsg) => |(uid, msg)| Self::ChartUpdate {
                    uid,
                    msg: msg.into(),
                }
            }
        }
    }

    /// A message for a specific chart.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum ChartMsg {
        /// Settings update.
        SettingsUpdate(ChartSettingsMsg),
    }
    impl fmt::Display for ChartMsg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::SettingsUpdate(_) => write!(fmt, "settings update"),
            }
        }
    }

    base::implement! {
        impl ChartMsg {
            From {
                from ChartSettingsMsg => |msg| Self::SettingsUpdate(msg)
            }
        }
    }

    /// Operations over filters.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum FiltersMsg {
        /// Requests a new filter.
        ///
        /// This will cause the server to generate a new filter to send to the client (*via*
        /// [`FiltersMsg::Add`]). The server will **not** register the filter in any way, this will
        /// happen when/if the user saves the modifications.
        ///
        /// [`FiltersMsg::Add`]: to_client::FiltersMsg::Add
        /// (The Add message)
        RequestNew,

        /// Requests a new sub filter.
        ///
        /// This will cause the server to generate a new sub filter to send to the client (*via*
        /// [`FiltersMsg::AddSub`]). The server will **not** register the filter in any way, this
        /// will happen when/if the user saves the modifications.
        ///
        /// [`FiltersMsg::AddSub`]: to_client::FiltersMsg::AddSub
        /// (The Add message)
        RequestNewSub(uid::Filter),

        /// Requests the current server-side list of filters.
        Revert,

        /// Updates all the filters.
        UpdateAll {
            /// New specificationfor the "everything" filter.
            everything: filter::FilterSpec,
            /// New filters.
            filters: Vec<Filter>,
            /// New specification for the "catch-all" filter.
            catch_all: filter::FilterSpec,
        },
    }
    impl fmt::Display for FiltersMsg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::RequestNew => write!(fmt, "request new"),
                Self::RequestNewSub(_) => write!(fmt, "request new sub"),
                Self::Revert => write!(fmt, "revert"),
                Self::UpdateAll { .. } => write!(fmt, "update all"),
            }
        }
    }

    impl FiltersMsg {
        /// Requests a new filter.
        pub fn request_new() -> Msg {
            Self::RequestNew.into()
        }
        /// Requests a new subfilter.
        pub fn request_new_sub(uid: uid::Filter) -> Msg {
            Self::RequestNewSub(uid).into()
        }
        /// Requests the current server-side list of filters.
        pub fn revert() -> Msg {
            Self::Revert.into()
        }

        /// Updates all the filters.
        pub fn update_all(
            everything: filter::FilterSpec,
            filters: Vec<Filter>,
            catch_all: filter::FilterSpec,
        ) -> Msg {
            Self::UpdateAll {
                everything,
                filters,
                catch_all,
            }
            .into()
        }
    }
}

/// Messages from the server to the client.
pub mod to_client {
    use super::*;

    /// A list of messages from the server to the client.
    pub type Msgs = Vec<Msg>;

    /// Messages from the server to the client.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum Msg {
        /// Info about the current allocation data.
        Info,
        /// An alert.
        Alert {
            /// Alert message.
            msg: String,
            /// True if the error is fatal.
            fatal: bool,
        },
        /// Loading progress.
        ///
        /// Sent by the server when it is loading data, *i.e.* not ready to actually produce charts
        /// yet.
        LoadProgress(LoadInfo),
        /// Allocation statistics.
        AllocStats(AllocStats),
        /// Sent by the server when it is done loading dumps.
        DoneLoading,
        /// A message for the charts.
        Charts(ChartsMsg),
        /// A filter operation.
        Filters(FiltersMsg),
        /// Some filter statistics.
        FilterStats(filter::stats::AllFilterStats),
    }
    impl Msg {
        /// Constructor for `Info`.
        pub fn info() -> Self {
            Self::Info
        }
        /// Constructor for `Alert`.
        pub fn alert(msg: impl Into<String>, fatal: bool) -> Self {
            Self::Alert {
                msg: msg.into(),
                fatal,
            }
        }
        /// Constructor for chart messages.
        pub fn charts(msg: ChartsMsg) -> Self {
            Self::Charts(msg)
        }
        /// Constructor for a load progress message.
        pub fn load_progress(info: LoadInfo) -> Self {
            Self::LoadProgress(info)
        }
        /// Constructor for an allocation-statistics message.
        pub fn alloc_stats(stats: AllocStats) -> Self {
            Self::AllocStats(stats)
        }
        /// Constructor for a filter-statistics message.
        pub fn filter_stats(stats: filter::stats::AllFilterStats) -> Self {
            Self::FilterStats(stats)
        }

        /// Encodes the message as bytes.
        pub fn to_bytes(&self) -> Res<Vec<u8>> {
            Ok(base::bincode::serialize(self)?)
        }

        /// Decodes the message from bytes.
        pub fn from_bytes(bytes: &[u8]) -> Res<Self> {
            Ok(base::bincode::deserialize(bytes)?)
        }

        /// True if the message is a minor message.
        ///
        /// *Minor messages* are all messages that do not act on charts or filters directly.
        pub fn is_minor(&self) -> bool {
            match self {
                Self::Charts(_) | Self::Filters(_) => false,
                Self::Info
                | Self::Alert { .. }
                | Self::LoadProgress(_)
                | Self::AllocStats(_)
                | Self::DoneLoading
                | Self::FilterStats(_) => true,
            }
        }
    }

    impl fmt::Display for Msg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::Info => "info".fmt(fmt),
                Self::Alert { .. } => "alert".fmt(fmt),
                Self::Charts(msg) => write!(fmt, "charts({})", msg),
                Self::LoadProgress(_) => "load progress".fmt(fmt),
                Self::AllocStats(_) => "alloc stats".fmt(fmt),
                Self::FilterStats(_) => "filter stats".fmt(fmt),
                Self::DoneLoading => "done loading".fmt(fmt),
                Self::Filters(_) => "filter".fmt(fmt),
            }
        }
    }

    base::implement! {
        impl Msg {
            From {
                from ChartsMsg => |msg| Self::Charts(msg),
                from FiltersMsg => |msg| Self::Filters(msg),
            }
        }
    }

    /// Messages for the charts of the client.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum ChartsMsg {
        /// Creates a new chart.
        NewChart(chart::ChartSpec, settings::Chart),
        /// Message for a specific chart.
        Chart {
            /// UID of the chart this message is for.
            uid: uid::Chart,
            /// Actual chart message.
            msg: ChartMsg,
        },
        /// A new collection of points, overwrites existing points.
        NewPoints {
            /// New points.
            points: point::ChartPoints,
            /// If true, refresh all filters.
            refresh_filters: bool,
        },
        /// Some points to append to existing points.
        AddPoints(point::ChartPoints),
    }
    impl ChartsMsg {
        /// Constructor for `NewChart`.
        pub fn new_chart(spec: chart::ChartSpec, settings: settings::Chart) -> Msg {
            Msg::charts(Self::NewChart(spec, settings))
        }
        /// Constructor for `NewPoints`.
        pub fn new_points(points: point::ChartPoints, refresh_filters: bool) -> Msg {
            Msg::charts(Self::NewPoints {
                points,
                refresh_filters,
            })
        }
        /// Constructor for `AddPoints`.
        pub fn add_points(points: point::ChartPoints) -> Msg {
            Msg::charts(Self::AddPoints(points))
        }

        /// Constructs a `NewPoints` if `overwrite`, and a `AddPoints` otherwise.
        pub fn points(points: point::ChartPoints, overwrite: bool) -> Msg {
            if overwrite {
                Self::new_points(points, false)
            } else {
                Self::add_points(points)
            }
        }
    }

    impl fmt::Display for ChartsMsg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::NewChart(_, _) => "new chart".fmt(fmt),
                Self::Chart { uid, msg } => write!(fmt, "chart({}, {})", uid, msg),
                Self::NewPoints { points, .. } => {
                    "new points:".fmt(fmt)?;
                    for (idx, (uid, points)) in points.iter().enumerate() {
                        write!(fmt, " ")?;
                        if idx > 0 {
                            write!(fmt, "| ")?
                        }
                        write!(fmt, "{}: {}, {}", uid, points.len(), points.point_count())?
                    }
                    Ok(())
                }
                Self::AddPoints(points) => {
                    "add points:".fmt(fmt)?;
                    for (idx, (uid, points)) in points.iter().enumerate() {
                        write!(fmt, " ")?;
                        if idx > 0 {
                            write!(fmt, "| ")?
                        }
                        write!(fmt, "{}: {}, {}", uid, points.len(), points.point_count())?
                    }
                    Ok(())
                }
            }
        }
    }

    /// Messages for a specific chart in the client.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum ChartMsg {
        /// A brand new list of points.
        ///
        /// Replaces all the points in a chart.
        NewPoints(point::Points),
        /// Some points to append.
        Points(point::Points),
    }

    impl ChartMsg {
        /// List of points overwriting the existing points.
        pub fn new_points(uid: uid::Chart, points: point::Points) -> Msg {
            Msg::charts(ChartsMsg::Chart {
                uid,
                msg: Self::NewPoints(points),
            })
        }
        /// List of points to append.
        pub fn points(uid: uid::Chart, points: point::Points) -> Msg {
            Msg::charts(ChartsMsg::Chart {
                uid,
                msg: Self::Points(points),
            })
        }
    }

    impl fmt::Display for ChartMsg {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::NewPoints(points) => write!(fmt, "{} new points", points.len()),
                Self::Points(points) => write!(fmt, "add {} points", points.len()),
            }
        }
    }

    /// Filter operations.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum FiltersMsg {
        /// Adds a filter.
        ///
        /// This message always comes in response to a [`FiltersMsg::RequestNew`] message for the
        /// server.
        ///
        /// [`FiltersMsg::RequestNew`]: to_server::FiltersMsg::RequestNew
        /// (The RequestNew message)
        Add(filter::Filter),
        /// Adds a subfilter.
        ///
        /// This message always comes in response to a [`FiltersMsg::RequestNewSub`] message for the
        /// server.
        ///
        /// [`FiltersMsg::RequestNewSub`]: to_server::FiltersMsg::RequestNewSub
        /// (The RequestNew message)
        AddSub(uid::Filter, filter::SubFilter),

        /// Orders the client to revert all its filters.
        Revert {
            /// Specification for the `everything` filter.
            everything: FilterSpec,
            /// Specification for custom filters.
            filters: Vec<Filter>,
            /// Specification for the `catch_all` filter.
            catch_all: FilterSpec,
        },
        // /// Updates all the specs.
        // UpdateSpecs(BTMap<uid::Line, FilterSpec>),
    }
    impl FiltersMsg {
        /// Adds a filter.
        pub fn add(filter: filter::Filter) -> Msg {
            Self::Add(filter).into()
        }
        /// Adds a subfilter.
        pub fn add_sub(uid: uid::Filter, subfilter: filter::SubFilter) -> Msg {
            Self::AddSub(uid, subfilter).into()
        }

        /// Orders the client to revert all its filters.
        pub fn revert(everything: FilterSpec, filters: Vec<Filter>, catch_all: FilterSpec) -> Msg {
            Self::Revert {
                everything,
                filters,
                catch_all,
            }
            .into()
        }

        // /// Updates all the specs.
        // pub fn update_specs(specs: BTMap<uid::Line, FilterSpec>) -> Msg {
        //     Self::UpdateSpecs(specs).into()
        // }
    }

    /// A raw message from the server.
    #[derive(Debug, Clone)]
    pub enum RawMsg {
        /// Binary version.
        Binary(Result<Vec<u8>, String>),
    }

    base::implement! {
        impl RawMsg {
            From {
                from yew::format::Binary => |data| RawMsg::Binary(data.map_err(|e| e.to_string())),
                from yew::format::Text => |_| panic!(
                    "trying to decode a message from text, \
                    but only decoding from binary is supported"
                ),
            }

            Into {
                to Res<Msg> => |self| {
                    let res = match self {
                        RawMsg::Binary(res_bytes) => {
                            let bytes = res_bytes
                                .map_err(err::Error::from)
                                .chain_err(|| "while retrieving message from the server")?;
                            Msg::from_bytes(&bytes)
                        }
                    };
                    res.chain_err(|| "while parsing a message from the server")
                }
            }
        }
    }
}