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
use {Observation, PutsSnapshot};
use instruments::*;
use snapshot::{ItemKind, Snapshot};
use util;
pub trait HandlesObservations: PutsSnapshot + Send + 'static {
type Label: Send + 'static;
fn handle_observation(&mut self, observation: &Observation<Self::Label>);
}
pub struct Cockpit<L> {
name: Option<String>,
title: Option<String>,
description: Option<String>,
panels: Vec<Panel<L>>,
value_scaling: Option<ValueScaling>,
}
impl<L> Cockpit<L>
where
L: Clone + Eq + Send + 'static,
{
pub fn new<T: Into<String>>(name: T, value_scaling: Option<ValueScaling>) -> Cockpit<L> {
let mut cockpit = Cockpit::default();
cockpit.name = Some(name.into());
cockpit.value_scaling = value_scaling;
cockpit
}
pub fn without_name(value_scaling: Option<ValueScaling>) -> Cockpit<L> {
let mut cockpit = Cockpit::default();
cockpit.value_scaling = value_scaling;
cockpit
}
pub fn with_value_scaling<T: Into<String>>(
&mut self,
name: T,
value_scaling: ValueScaling,
) -> Cockpit<L> {
Cockpit::new(name, Some(value_scaling))
}
pub fn without_value_scaling<T: Into<String>>(&mut self, name: T) -> Cockpit<L> {
Cockpit::new(name, None)
}
pub fn name(&self) -> Option<&str> {
self.name.as_ref().map(|n| &**n)
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = Some(name.into())
}
pub fn set_title<T: Into<String>>(&mut self, title: T) {
self.title = Some(title.into())
}
pub fn set_description<T: Into<String>>(&mut self, description: T) {
self.description = Some(description.into())
}
pub fn set_value_scaling(&mut self, value_scaling: ValueScaling) {
self.value_scaling = Some(value_scaling)
}
pub fn add_panel(&mut self, panel: Panel<L>) -> bool {
if self.panels
.iter()
.find(|x| x.label == panel.label)
.is_some()
{
false
} else {
self.panels.push(panel);
true
}
}
fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_default_descriptives(self, into, descriptive);
self.panels
.iter()
.for_each(|p| p.put_snapshot(into, descriptive))
}
}
impl<L> PutsSnapshot for Cockpit<L>
where
L: Clone + Eq + Send + 'static,
{
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
if let Some(ref name) = self.name {
let mut new_level = Snapshot::default();
self.put_values_into_snapshot(&mut new_level, descriptive);
into.items
.push((name.clone(), ItemKind::Snapshot(new_level)));
} else {
self.put_values_into_snapshot(into, descriptive);
}
}
}
impl<L> Default for Cockpit<L>
where
L: Clone + Eq + Send + 'static,
{
fn default() -> Cockpit<L> {
Cockpit {
name: None,
title: None,
description: None,
panels: Vec::new(),
value_scaling: None,
}
}
}
impl<L> HandlesObservations for Cockpit<L>
where
L: Clone + Eq + Send + 'static,
{
type Label = L;
fn handle_observation(&mut self, observation: &Observation<Self::Label>) {
let LabelAndUpdate(label, update) = observation.into();
let update = if let Some(scaling) = self.value_scaling {
update.scale(scaling)
} else {
update
};
self.panels
.iter_mut()
.filter(|p| p.label == label)
.for_each(|p| p.update(&update));
}
}
impl<L> ::Descriptive for Cockpit<L> {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}