Skip to content

Commit b07b2e6

Browse files
committed
Ported Telemetry from rustc-serialize to serde
1 parent 1bb9892 commit b07b2e6

File tree

9 files changed

+29
-85
lines changed

9 files changed

+29
-85
lines changed

src/host/telemetry.rs

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use target::Target;
1515
#[cfg(feature = "local-run")]
1616

1717
#[cfg(feature = "local-run")]
18-
#[derive(Debug, RustcEncodable)]
18+
#[derive(Debug, Serialize)]
1919
pub struct Telemetry {
2020
pub cpu: Cpu,
2121
pub fs: Vec<FsMount>,
@@ -55,70 +55,14 @@ impl Telemetry {
5555
pub fn init(host: &mut Host) -> Result<Value> {
5656
Ok(try!(Target::telemetry_init(host)))
5757
}
58-
59-
// XXX While Macros 1.1 are unstable, we can't use Serde to
60-
// convert Telemetry => Value, so we have to roll our own.
61-
// (https://github.com/rust-lang/rust/issues/35900)
62-
#[cfg(feature = "local-run")]
63-
pub fn into_value(self) -> Value {
64-
let mut cpu: Map<String, Value> = Map::new();
65-
cpu.insert("vendor".into(), json!(self.cpu.vendor));
66-
cpu.insert("brand_string".into(), json!(self.cpu.brand_string));
67-
cpu.insert("cores".into(), json!(self.cpu.cores));
68-
69-
let mut fs = Vec::new();
70-
for mount in self.fs {
71-
let mut map: Map<String, Value> = Map::new();
72-
map.insert("filesystem".into(), json!(mount.filesystem));
73-
map.insert("mountpoint".into(), json!(mount.mountpoint));
74-
map.insert("size".into(), json!(mount.size));
75-
map.insert("used".into(), json!(mount.used));
76-
map.insert("available".into(), json!(mount.available));
77-
map.insert("capacity".into(), json!(mount.capacity));
78-
fs.push(map);
79-
}
80-
81-
let mut net = Vec::new();
82-
for netif in self.net {
83-
let mut map: Map<String, Value> = Map::new();
84-
map.insert("name".into(), json!(netif.name));
85-
map.insert("index".into(), json!(netif.index));
86-
if let Some(mac) = netif.mac {
87-
map.insert("mac".into(), json!(mac));
88-
}
89-
if let Some(ips) = netif.ips {
90-
map.insert("ips".into(), json!(ips));
91-
}
92-
map.insert("flags".into(), json!(netif.flags));
93-
net.push(map);
94-
}
95-
96-
let mut os: Map<String, Value> = Map::new();
97-
os.insert("arch".into(), json!(self.os.arch));
98-
os.insert("family".into(), json!(self.os.family));
99-
os.insert("platform".into(), json!(self.os.platform));
100-
os.insert("version_str".into(), json!(self.os.version_str));
101-
os.insert("version_maj".into(), json!(self.os.version_maj));
102-
os.insert("version_min".into(), json!(self.os.version_min));
103-
os.insert("version_patch".into(), json!(self.os.version_patch));
104-
105-
let mut telemetry: Map<String, Value> = Map::new();
106-
telemetry.insert("cpu".into(), json!(cpu));
107-
telemetry.insert("fs".into(), json!(fs));
108-
telemetry.insert("hostname".into(), json!(self.hostname));
109-
telemetry.insert("memory".into(), json!(self.memory));
110-
telemetry.insert("net".into(), json!(net));
111-
telemetry.insert("os".into(), json!(os));
112-
json!(telemetry)
113-
}
11458
}
11559

11660
pub trait TelemetryTarget {
11761
fn telemetry_init(host: &mut Host) -> Result<Value>;
11862
}
11963

12064
#[cfg(feature = "local-run")]
121-
#[derive(Debug, RustcEncodable)]
65+
#[derive(Debug, Serialize)]
12266
pub struct Cpu {
12367
pub vendor: String,
12468
pub brand_string: String,
@@ -137,7 +81,7 @@ impl Cpu {
13781
}
13882

13983
#[cfg(feature = "local-run")]
140-
#[derive(Debug, RustcEncodable)]
84+
#[derive(Debug, Serialize)]
14185
pub struct FsMount {
14286
pub filesystem: String,
14387
pub mountpoint: String,
@@ -148,7 +92,7 @@ pub struct FsMount {
14892
}
14993

15094
#[cfg(feature = "local-run")]
151-
#[derive(Debug, RustcEncodable)]
95+
#[derive(Debug, Serialize)]
15296
pub struct Netif {
15397
pub name: String,
15498
pub index: u32,
@@ -158,7 +102,7 @@ pub struct Netif {
158102
}
159103

160104
#[cfg(feature = "local-run")]
161-
#[derive(Debug, RustcEncodable)]
105+
#[derive(Debug, Serialize)]
162106
pub struct Os {
163107
pub arch: String,
164108
pub family: String,

src/target/centos.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use file::{FileTarget, FileOwner};
1313
use host::Host;
1414
use package::PackageTarget;
1515
use package::providers::Providers;
16-
use serde_json::Value;
16+
use serde_json;
1717
use service::ServiceTarget;
1818
use std::env;
1919
use std::path::Path;
@@ -166,7 +166,7 @@ impl ServiceTarget for CentosTarget {
166166

167167
impl TelemetryTarget for CentosTarget {
168168
#[allow(unused_variables)]
169-
fn telemetry_init(host: &mut Host) -> Result<Value> {
169+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
170170
let cpu_vendor = try!(linux::cpu_vendor());
171171
let cpu_brand = try!(linux::cpu_brand_string());
172172
let hostname = try!(default::hostname());
@@ -193,6 +193,6 @@ impl TelemetryTarget for CentosTarget {
193193
),
194194
);
195195

196-
Ok(telemetry.into_value())
196+
Ok(serde_json::to_value(telemetry)?)
197197
}
198198
}

src/target/debian.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use host::Host;
1414
use host::telemetry::{Cpu, Os, Telemetry, TelemetryTarget};
1515
use package::PackageTarget;
1616
use package::providers::Providers;
17-
use serde_json::Value;
17+
use serde_json;
1818
use service::ServiceTarget;
1919
use std::{env, str};
2020
use std::path::Path;
@@ -167,7 +167,7 @@ impl ServiceTarget for DebianTarget {
167167

168168
impl TelemetryTarget for DebianTarget {
169169
#[allow(unused_variables)]
170-
fn telemetry_init(host: &mut Host) -> Result<Value> {
170+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
171171
let cpu_vendor = try!(linux::cpu_vendor());
172172
let cpu_brand = try!(linux::cpu_brand_string());
173173
let hostname = try!(default::hostname());
@@ -186,7 +186,7 @@ impl TelemetryTarget for DebianTarget {
186186
Os::new(env::consts::ARCH, "debian", "debian", &version_str, version_maj, version_min, 0), // No known patch version
187187
);
188188

189-
Ok(telemetry.into_value())
189+
Ok(serde_json::to_value(telemetry)?)
190190
}
191191
}
192192

src/target/fedora.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use file::{FileTarget, FileOwner};
1313
use host::Host;
1414
use package::PackageTarget;
1515
use package::providers::Providers;
16-
use serde_json::Value;
16+
use serde_json;
1717
use service::ServiceTarget;
1818
use std::env;
1919
use std::path::Path;
@@ -166,7 +166,7 @@ impl ServiceTarget for FedoraTarget {
166166

167167
impl TelemetryTarget for FedoraTarget {
168168
#[allow(unused_variables)]
169-
fn telemetry_init(host: &mut Host) -> Result<Value> {
169+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
170170
let cpu_vendor = try!(linux::cpu_vendor());
171171
let cpu_brand = try!(linux::cpu_brand_string());
172172
let hostname = try!(default::hostname());
@@ -193,6 +193,6 @@ impl TelemetryTarget for FedoraTarget {
193193
),
194194
);
195195

196-
Ok(telemetry.into_value())
196+
Ok(serde_json::to_value(telemetry)?)
197197
}
198198
}

src/target/freebsd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use host::telemetry::{Cpu, Os, Telemetry, TelemetryTarget};
1515
use package::PackageTarget;
1616
use package::providers::Providers;
1717
use regex::Regex;
18-
use serde_json::Value;
18+
use serde_json;
1919
use service::ServiceTarget;
2020
use std::env;
2121
use std::fs::{File, OpenOptions};
@@ -206,7 +206,7 @@ impl ServiceTarget for Target {
206206

207207
impl TelemetryTarget for Target {
208208
#[allow(unused_variables)]
209-
fn telemetry_init(host: &mut Host) -> Result<Value> {
209+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
210210
let cpu_vendor = try!(telemetry_cpu_vendor());
211211
let cpu_brand = try!(unix::get_sysctl_item("hw\\.model"));
212212
let hostname = try!(default::hostname());
@@ -225,7 +225,7 @@ impl TelemetryTarget for Target {
225225
Os::new(env::consts::ARCH, "unix", "freebsd", &version_str, version_maj, version_min, 0),
226226
);
227227

228-
Ok(telemetry.into_value())
228+
Ok(serde_json::to_value(telemetry)?)
229229
}
230230
}
231231

src/target/macos.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use host::Host;
1414
use host::telemetry::{Cpu, Os, Telemetry, TelemetryTarget};
1515
use package::PackageTarget;
1616
use package::providers::Providers;
17-
use serde_json::Value;
17+
use serde_json;
1818
use service::ServiceTarget;
1919
use std::{env, process, str};
2020
use std::path::Path;
@@ -197,7 +197,7 @@ impl ServiceTarget for Target {
197197

198198
impl TelemetryTarget for Target {
199199
#[allow(unused_variables)]
200-
fn telemetry_init(host: &mut Host) -> Result<Value> {
200+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
201201
let cpu_vendor = try!(unix::get_sysctl_item("machdep\\.cpu\\.vendor"));
202202
let cpu_brand = try!(unix::get_sysctl_item("machdep\\.cpu\\.brand_string"));
203203
let hostname = try!(default::hostname());
@@ -226,7 +226,7 @@ impl TelemetryTarget for Target {
226226
Os::new(env::consts::ARCH, "unix", "macos", &version_str, version_maj, version_min, version_patch),
227227
);
228228

229-
Ok(telemetry.into_value())
229+
Ok(serde_json::to_value(telemetry)?)
230230
}
231231
}
232232

src/target/nixos.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use file::{FileTarget, FileOwner};
1313
use host::Host;
1414
use package::PackageTarget;
1515
use package::providers::Providers;
16-
use serde_json::Value;
16+
use serde_json;
1717
use service::ServiceTarget;
1818
use std::{env, process, str};
1919
use std::path::Path;
@@ -162,7 +162,7 @@ impl ServiceTarget for NixOsTarget {
162162

163163
impl TelemetryTarget for NixOsTarget {
164164
#[allow(unused_variables)]
165-
fn telemetry_init(host: &mut Host) -> Result<Value> {
165+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
166166
let cpu_vendor = try!(linux::cpu_vendor());
167167
let cpu_brand = try!(linux::cpu_brand_string());
168168
let hostname = try!(default::hostname());
@@ -189,7 +189,7 @@ impl TelemetryTarget for NixOsTarget {
189189
),
190190
);
191191

192-
Ok(telemetry.into_value())
192+
Ok(serde_json::to_value(telemetry)?)
193193
}
194194
}
195195

src/target/redhat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use file::{FileTarget, FileOwner};
1313
use host::Host;
1414
use package::PackageTarget;
1515
use package::providers::Providers;
16-
use serde_json::Value;
16+
use serde_json;
1717
use service::ServiceTarget;
1818
use std::{env, str};
1919
use std::path::Path;
@@ -166,7 +166,7 @@ impl ServiceTarget for RedhatTarget {
166166

167167
impl TelemetryTarget for RedhatTarget {
168168
#[allow(unused_variables)]
169-
fn telemetry_init(host: &mut Host) -> Result<Value> {
169+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
170170
let cpu_vendor = try!(linux::cpu_vendor());
171171
let cpu_brand = try!(linux::cpu_brand_string());
172172
let hostname = try!(default::hostname());
@@ -193,6 +193,6 @@ impl TelemetryTarget for RedhatTarget {
193193
),
194194
);
195195

196-
Ok(telemetry.into_value())
196+
Ok(serde_json::to_value(telemetry)?)
197197
}
198198
}

src/target/ubuntu.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use host::telemetry::{Cpu, Os, Telemetry, TelemetryTarget};
1515
use package::PackageTarget;
1616
use package::providers::Providers;
1717
use regex::Regex;
18-
use serde_json::Value;
18+
use serde_json;
1919
use service::ServiceTarget;
2020
use std::env;
2121
use std::path::Path;
@@ -168,7 +168,7 @@ impl ServiceTarget for UbuntuTarget {
168168

169169
impl TelemetryTarget for UbuntuTarget {
170170
#[allow(unused_variables)]
171-
fn telemetry_init(host: &mut Host) -> Result<Value> {
171+
fn telemetry_init(host: &mut Host) -> Result<serde_json::Value> {
172172
let cpu_vendor = try!(linux::cpu_vendor());
173173
let cpu_brand = try!(linux::cpu_brand_string());
174174
let hostname = try!(default::hostname());
@@ -187,7 +187,7 @@ impl TelemetryTarget for UbuntuTarget {
187187
Os::new(env::consts::ARCH, "debian", "ubuntu", &version_str, version_maj, version_min, version_patch),
188188
);
189189

190-
Ok(telemetry.into_value())
190+
Ok(serde_json::to_value(telemetry)?)
191191
}
192192
}
193193

0 commit comments

Comments
 (0)