From b9b11e4b8efe38c7ad2bc4e1312ca57b1e219dc7 Mon Sep 17 00:00:00 2001 From: Christiane Ruetten Date: Mon, 29 Feb 2016 15:01:40 +0100 Subject: [PATCH] Switching to `json!` macro --- src/adapters/philips_hue/mod.rs | 48 +++++++++++++++++++++------------ src/controller.rs | 3 +-- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/adapters/philips_hue/mod.rs b/src/adapters/philips_hue/mod.rs index 6ab94021..7a4f9866 100644 --- a/src/adapters/philips_hue/mod.rs +++ b/src/adapters/philips_hue/mod.rs @@ -73,10 +73,13 @@ impl ServiceAdapter for PhilipsHueAdapter { for _ in 0..120 { controller.send_event( EventData::AdapterNotification { - info: format!( - "{{\"adapter\": \"philips_hue\", \"message\": \"NeedsPairing\", \"hub\": \"{}\"}}", - hub.id) - }).unwrap(); + info: json!({ + adapter: "philips_hue", + message: "NeedsPairing", + hub: hub.id + }) + } + ).unwrap(); if hub.try_pairing() { break; } @@ -87,18 +90,24 @@ impl ServiceAdapter for PhilipsHueAdapter { info!("Paired with Philips Hue Bridge ID {}", hub.id); controller.send_event( EventData::AdapterNotification { - info: format!( - "{{\"adapter\": \"philips_hue\", \"message\": \"PairingSuccess\", \"hub\": \"{}\"}}", - hub.id) - }).unwrap(); + info: json!({ + adapter: "philips_hue", + message: "PairingSuccess", + hub: hub.id + }) + } + ).unwrap(); } else { warn!("Pairing timeout with Philips Hue Bridge ID {}", hub.id); controller.send_event( EventData::AdapterNotification { - info: format!( - "{{\"adapter\": \"philips_hue\", \"message\": \"PairingTimeout\", \"hub\": \"{}\"}}", - hub.id) - }).unwrap(); + info: json!({ + adapter: "philips_hue", + message: "PairingTimeout", + hub: hub.id + }) + } + ).unwrap(); // Giving up for this Hub. return; } @@ -165,11 +174,16 @@ impl HueLightService { // internal design change. let status = self.light.get_settings(); let light_state = self.light.get_state(); - let json = format!( - "{{\"type\": \"{}\", \"available\": {}, \"on\": {}, \"hue\": {}, \"sat\": {}, \"val\": {}}}", - "device/light/colorlight", - status.state.reachable, status.state.on, - light_state.hue, light_state.sat, light_state.val); + let json = json!( + { + type: "device/light/colorlight", + available: status.state.reachable, + on: status.state.on, + hue: light_state.hue, + sat: light_state.sat, + val: light_state.val + } + ); let mut response = Response::with(json); response.status = Some(Status::Ok); response.headers.set(ContentType::json()); diff --git a/src/controller.rs b/src/controller.rs index e08a2249..f5cb5d58 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -95,8 +95,7 @@ impl Controller for FoxBox { let services = self.services.lock().unwrap(); match services.get(&id) { None => { - let mut response = Response::with(format!( - "{{\"result\": \"error\", \"details\": \"No Such Service: {}\"}}", id)); + let mut response = Response::with(json!({ error: "NoSuchService", id: id })); response.status = Some(Status::BadRequest); response.headers.set(AccessControlAllowOrigin::Any); response.headers.set(ContentType::json());