-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.rs
More file actions
121 lines (100 loc) · 3.46 KB
/
symbol.rs
File metadata and controls
121 lines (100 loc) · 3.46 KB
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
// Based on defmt
use std::ops::RangeInclusive;
use crate::cargo;
pub struct MetricsSymbol {
/// Name of the Cargo package in which the symbol is being instantiated. Used for avoiding
/// symbol name collisions.
package: String,
/// Unique identifier that disambiguates otherwise equivalent invocations in the same crate.
disambiguator: u64,
/// Underlaying data type
ty: String,
/// Variable name
name: String,
/// Expression used to calculate the value to plot
expression_string: String,
/// Crate name obtained via CARGO_CRATE_NAME (added since a Cargo package can contain many crates).
crate_name: String,
}
impl MetricsSymbol {
pub fn new(ty: String, name: String, expr: String) -> Self {
Self {
// `CARGO_PKG_NAME` is set to the invoking package's name.
package: cargo::package_name(),
disambiguator: super::crate_local_disambiguator(),
ty,
name,
expression_string: expr,
crate_name: cargo::crate_name(),
}
}
pub fn mangle(&self) -> String {
format!(
r#"{{"type":"Metric","package":"{}","ty":"{}","name":"{}","expr":"{}","disambiguator":"{}","crate_name":"{}"}}"#,
json_escape(&self.package),
json_escape(&self.ty),
json_escape(&self.name),
json_escape(&self.expression_string),
self.disambiguator,
json_escape(&self.crate_name),
)
}
}
pub struct SettingSymbol {
/// Name of the Cargo package in which the symbol is being instantiated. Used for avoiding
/// symbol name collisions.
package: String,
/// Unique identifier that disambiguates otherwise equivalent invocations in the same crate.
disambiguator: u64,
/// Underlaying data type
ty: String,
/// Variable name
name: String,
/// Range of valid values
range: RangeInclusive<f64>,
/// Step size
step_size: f64,
/// Crate name obtained via CARGO_CRATE_NAME (added since a Cargo package can contain many crates).
crate_name: String,
}
impl SettingSymbol {
pub fn new(ty: String, name: String, range: RangeInclusive<f64>, step_size: f64) -> Self {
Self {
// `CARGO_PKG_NAME` is set to the invoking package's name.
package: cargo::package_name(),
disambiguator: super::crate_local_disambiguator(),
ty,
name,
range,
step_size,
crate_name: cargo::crate_name(),
}
}
pub fn mangle(&self) -> String {
format!(
r#"{{"type":"Setting","package":"{}","ty":"{}","name":"{}","range":{{"start":{},"end":{}}},"step_size":{},"disambiguator":"{}","crate_name":"{}"}}"#,
json_escape(&self.package),
json_escape(&self.ty),
json_escape(&self.name),
self.range.start(),
self.range.end(),
self.step_size,
self.disambiguator,
json_escape(&self.crate_name),
)
}
}
fn json_escape(string: &str) -> String {
use std::fmt::Write;
let mut escaped = String::new();
for c in string.chars() {
match c {
'\\' => escaped.push_str("\\\\"),
'\"' => escaped.push_str("\\\""),
'\n' => escaped.push_str("\\n"),
c if c.is_control() || c == '@' => write!(escaped, "\\u{:04x}", c as u32).unwrap(),
c => escaped.push(c),
}
}
escaped
}