From 99fc8b45d7ce71377fb992d2ec215eb19bc77ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20B=C3=BCsch?= Date: Mon, 24 Sep 2018 11:11:44 +1000 Subject: Migrate to serde_json --- Cargo.toml | 6 +-- src/builder.rs | 125 ++++++++++++++++++------------------------------------- src/errors.rs | 32 ++++---------- src/lib.rs | 125 +++++++++++++++++++++++++++---------------------------- src/rep.rs | 68 +++++++++++++++--------------- src/transport.rs | 6 +-- 6 files changed, 149 insertions(+), 213 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b97d6ad..031a67e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,12 @@ jed = "0.1" log = "0.3" mime = "0.3" openssl = "0.10" -rustc-serialize = "0.3" tar = "0.4" tokio = "0.1" url = "1.7" -serde = "1.0" -serde_derive = "1.0" +serde = "1" +serde_derive = "1" +serde_json = "1" [dev-dependencies] env_logger = "0.4.0" diff --git a/src/builder.rs b/src/builder.rs index ffc0cf1..3dfe1bf 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,13 +1,15 @@ //! Interfaces for building various structures use self::super::Result; -use rustc_serialize::json::{self, Json, ToJson}; +use serde::Serialize; +use serde_json::{self, Value, Number, map::Map}; use std::cmp::Eq; use std::collections::{BTreeMap, HashMap}; use std::hash::Hash; use std::iter::IntoIterator; use std::iter::Peekable; use url::form_urlencoded; +use errors::Error; #[derive(Default)] pub struct PullOptions { @@ -254,7 +256,7 @@ impl ContainerListOptionsBuilder { } // structure is a a json encoded object mapping string keys to a list // of string values - self.params.insert("filters", json::encode(¶m).unwrap()); + self.params.insert("filters", serde_json::to_string(¶m).unwrap()); self } @@ -284,42 +286,22 @@ impl ContainerListOptionsBuilder { } /// Interface for building a new docker container from an existing image +#[derive(Serialize)] pub struct ContainerOptions { pub name: Option, - params: HashMap<&'static str, Json>, + params: HashMap<&'static str, Value>, params_list: HashMap<&'static str, Vec>, params_hash: HashMap>>, } -impl ToJson for ContainerOptions { - fn to_json(&self) -> Json { - let mut body_members = BTreeMap::new(); - - // The HostConfig element gets initialized to an empty object, - // for backward compatibility. - body_members.insert( - "HostConfig".to_string(), - Json::Object(BTreeMap::new()), - ); - - let mut body = Json::Object(body_members); - - self.parse_from(&self.params, &mut body); - self.parse_from(&self.params_list, &mut body); - self.parse_from(&self.params_hash, &mut body); - - body - } -} - /// Function to insert a JSON value into a tree where the desired /// location of the value is given as a path of JSON keys. fn insert<'a, I, V>( key_path: &mut Peekable, value: &V, - parent_node: &mut Json, + parent_node: &mut Value, ) where - V: ToJson, + V: Serialize, I: Iterator, { let local_key = key_path.next().unwrap(); @@ -329,13 +311,13 @@ fn insert<'a, I, V>( .as_object_mut() .unwrap() .entry(local_key.to_string()) - .or_insert(Json::Object(BTreeMap::new())); + .or_insert(Value::Object(Map::new())); insert(key_path, value, node); } else { parent_node.as_object_mut().unwrap().insert( local_key.to_string(), - value.to_json(), + serde_json::to_value(value).unwrap(), ); } } @@ -347,18 +329,18 @@ impl ContainerOptions { } /// serialize options as a string. returns None if no options are defined - pub fn serialize(&self) -> Result { - Ok(json::encode(&self.to_json())?) - } + // pub fn serialize(&self) -> Result { + // Ok(serde_json::to_string(&self.to_json())?) + // } pub fn parse_from<'a, K, V>( &self, params: &'a HashMap, - body: &mut Json, + body: &mut Value, ) where &'a HashMap: IntoIterator, K: ToString + Eq + Hash, - V: ToJson, + V: Serialize, { for (k, v) in params.iter() { let key_string = k.to_string(); @@ -370,7 +352,7 @@ impl ContainerOptions { #[derive(Default)] pub struct ContainerOptionsBuilder { name: Option, - params: HashMap<&'static str, Json>, + params: HashMap<&'static str, Value>, params_list: HashMap<&'static str, Vec>, params_hash: HashMap>>, } @@ -381,7 +363,7 @@ impl ContainerOptionsBuilder { let params_list = HashMap::new(); let params_hash = HashMap::new(); - params.insert("Image", Json::String(image.to_owned())); + params.insert("Image", Value::String(image.to_owned())); ContainerOptionsBuilder { name: None, params: params, @@ -419,22 +401,22 @@ impl ContainerOptionsBuilder { } pub fn memory(&mut self, memory: u64) -> &mut ContainerOptionsBuilder { - self.params.insert("HostConfig.Memory", Json::U64(memory)); + self.params.insert("HostConfig.Memory", Value::Number(Number::from(memory))); self } pub fn labels(&mut self, labels: &HashMap<&str, &str>) -> &mut ContainerOptionsBuilder { - let mut json_labels : BTreeMap = BTreeMap::new(); + let mut json_labels = Map::new(); for (k, v) in labels { let key : &str = k.as_ref(); let value : &str = v.as_ref(); - json_labels.insert(key .to_owned(), Json::String(value.to_string())); + json_labels.insert(key .to_owned(), Value::String(value.to_string())); } self.params.insert( "Labels", - Json::Object(json_labels), + Value::Object(json_labels), ); self @@ -474,7 +456,7 @@ impl ContainerOptionsBuilder { if !network.is_empty() { self.params.insert( "HostConfig.NetworkMode", - Json::String(network.to_owned()), + Value::String(network.to_owned()), ); } self @@ -505,7 +487,7 @@ impl ContainerOptionsBuilder { if !entrypoint.is_empty() { self.params.insert( "Entrypoint", - Json::String(entrypoint.to_owned()), + Value::String(entrypoint.to_owned()), ); } self @@ -544,7 +526,7 @@ impl ContainerOptionsBuilder { if !log_driver.is_empty() { self.params.insert( "HostConfig.LogConfig.Type", - Json::String(log_driver.to_owned()), + Value::String(log_driver.to_owned()), ); } self @@ -558,13 +540,13 @@ impl ContainerOptionsBuilder { if !name.is_empty() { self.params.insert( "HostConfig.RestartPolicy.Name", - Json::String(name.to_owned()), + Value::String(name.to_owned()), ); } if name == "on-failure" { self.params.insert( "HostConfig.RestartPolicy.MaximumRetryCount", - Json::U64(maximum_retry_count), + Value::Number(Number::from(maximum_retry_count)), ); } self @@ -580,6 +562,7 @@ impl ContainerOptionsBuilder { } } +#[derive(Serialize)] pub struct ExecContainerOptions { params: HashMap<&'static str, Vec>, params_bool: HashMap<&'static str, bool>, @@ -593,17 +576,7 @@ impl ExecContainerOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - let mut body = BTreeMap::new(); - - for (k, v) in &self.params { - body.insert(k.to_string(), v.to_json()); - } - for (k, v) in &self.params_bool { - body.insert(k.to_string(), v.to_json()); - } - - let json_obj: Json = body.to_json(); - Ok(json::encode(&json_obj)?) + Ok(serde_json::to_string(self)?) } } @@ -794,7 +767,7 @@ impl EventsOptionsBuilder { }; } - self.params.insert("filters", json::encode(¶ms).unwrap()); + self.params.insert("filters", serde_json::to_string(¶ms).unwrap()); self } @@ -942,7 +915,7 @@ impl ImageListOptionsBuilder { } // structure is a a json encoded object mapping string keys to a list // of string values - self.params.insert("filters", json::encode(¶m).unwrap()); + self.params.insert("filters", serde_json::to_string(¶m).unwrap()); self } @@ -1020,23 +993,13 @@ impl NetworkListOptions { } /// Interface for creating new docker network +#[derive(Serialize)] pub struct NetworkCreateOptions { pub name: Option, params: HashMap<&'static str, String>, params_hash: HashMap>>, } -impl ToJson for NetworkCreateOptions { - fn to_json(&self) -> Json { - let mut body: BTreeMap = BTreeMap::new(); - - self.parse_from(&self.params, &mut body); - self.parse_from(&self.params_hash, &mut body); - - body.to_json() - } -} - impl NetworkCreateOptions { /// return a new instance of a builder for options pub fn builder(name: &str) -> NetworkCreateOptionsBuilder { @@ -1045,21 +1008,21 @@ impl NetworkCreateOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - Ok(json::encode(&self.to_json())?) + serde_json::to_string(self).map_err(Error::from) } pub fn parse_from<'a, K, V>( &self, params: &'a HashMap, - body: &mut BTreeMap, + body: &mut BTreeMap, ) where &'a HashMap: IntoIterator, K: ToString + Eq + Hash, - V: ToJson, + V: Serialize, { for (k, v) in params.iter() { let key = k.to_string(); - let value = v.to_json(); + let value = serde_json::to_value(v).unwrap(); body.insert(key, value); } @@ -1116,38 +1079,30 @@ impl NetworkCreateOptionsBuilder { } /// Interface for connect container to network +#[derive(Serialize)] pub struct ContainerConnectionOptions { pub Container: Option, params: HashMap<&'static str, String>, } -impl ToJson for ContainerConnectionOptions { - fn to_json(&self) -> Json { - let mut body: BTreeMap = BTreeMap::new(); - self.parse_from(&self.params, &mut body); - body.to_json() - } -} - - impl ContainerConnectionOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result { - Ok(json::encode(&self.to_json())?) + serde_json::to_string(self).map_err(Error::from) } pub fn parse_from<'a, K, V>( &self, params: &'a HashMap, - body: &mut BTreeMap, + body: &mut BTreeMap, ) where &'a HashMap: IntoIterator, K: ToString + Eq + Hash, - V: ToJson, + V: Serialize, { for (k, v) in params.iter() { let key = k.to_string(); - let value = v.to_json(); + let value = serde_json::to_value(v).unwrap(); body.insert(key, value); } diff --git a/src/errors.rs b/src/errors.rs index d4905a1..44be52a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -2,37 +2,23 @@ use http; use hyper::{self, StatusCode}; -use rustc_serialize::json::{DecoderError, EncoderError, ParserError}; +use serde_json::Error as SerdeError; use std::error::Error as ErrorTrait; use std::fmt; use std::io::Error as IoError; #[derive(Debug)] pub enum Error { - Decoding(DecoderError), - Encoding(EncoderError), - Parse(ParserError), + SerdeJsonError(SerdeError), Hyper(hyper::Error), Http(http::Error), IO(IoError), Fault { code: StatusCode, message: String }, } -impl From for Error { - fn from(error: ParserError) -> Error { - Error::Parse(error) - } -} - -impl From for Error { - fn from(error: DecoderError) -> Error { - Error::Decoding(error) - } -} - -impl From for Error { - fn from(error: EncoderError) -> Error { - Error::Encoding(error) +impl From for Error { + fn from(error: SerdeError) -> Error { + Error::SerdeJsonError(error) } } @@ -58,9 +44,7 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Docker Error: ")?; match self { - &Error::Decoding(ref err) => return err.fmt(f), - &Error::Encoding(ref err) => return err.fmt(f), - &Error::Parse(ref err) => return err.fmt(f), + &Error::SerdeJsonError(ref err) => return err.fmt(f), &Error::Http(ref err) => return err.fmt(f), &Error::Hyper(ref err) => return err.fmt(f), &Error::IO(ref err) => return err.fmt(f), @@ -76,9 +60,7 @@ impl ErrorTrait for Error { fn cause(&self) -> Option<&ErrorTrait> { match self { - &Error::Decoding(ref err) => Some(err), - &Error::Encoding(ref err) => Some(err), - &Error::Parse(ref err) => Some(err), + &Error::SerdeJsonError(ref err) => Some(err), &Error::Http(ref err) => Some(err), &Error::IO(ref err) => Some(err), _ => None, diff --git a/src/lib.rs b/src/lib.rs index c2f7807..60c655f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,12 +25,12 @@ extern crate hyperlocal; extern crate jed; extern crate mime; extern crate openssl; -extern crate rustc_serialize; extern crate tar; extern crate url; #[macro_use] extern crate serde_derive; extern crate serde; +extern crate serde_json; extern crate tokio; pub mod builder; @@ -63,7 +63,7 @@ use rep::{ Version, }; use rep::{NetworkCreateInfo, NetworkDetails as NetworkInfo}; -use rustc_serialize::json::{self, Json}; +use serde_json::Value; use std::borrow::Cow; use std::cell::RefCell; use std::env; @@ -106,7 +106,7 @@ impl<'a, 'b> Image<'a, 'b> { let raw = self .docker .get(&format!("/images/{}/json", self.name)[..])?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Lists the history of the images set of changes @@ -114,25 +114,25 @@ impl<'a, 'b> Image<'a, 'b> { let raw = self .docker .get(&format!("/images/{}/history", self.name)[..])?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Delete's an image pub fn delete(&self) -> Result> { let raw = self.docker.delete(&format!("/images/{}", self.name)[..])?; - Ok(match Json::from_str(&raw)? { - Json::Array(ref xs) => xs.iter().map(|j| { + Ok(match serde_json::from_str(&raw)? { + Value::Array(ref xs) => xs.iter().map(|j| { let obj = j.as_object().expect("expected json object"); obj.get("Untagged") .map(|sha| { Status::Untagged( - sha.as_string() + sha.as_str() .expect("expected Untagged to be a string") .to_owned(), ) }).or(obj.get("Deleted").map(|sha| { Status::Deleted( - sha.as_string() + sha.as_str() .expect("expected Deleted to be a string") .to_owned(), ) @@ -164,7 +164,7 @@ impl<'a> Images<'a> { pub fn build( &self, opts: &BuildOptions, - ) -> Result>> { + ) -> Result> { let mut path = vec!["/build".to_owned()]; if let Some(query) = opts.serialize() { path.push(query) @@ -174,11 +174,12 @@ impl<'a> Images<'a> { tarball::dir(&mut bytes, &opts.path[..])?; - let raw = self + self .docker - .stream_post(&path.join("?"), Some((Body::from(bytes), tar())))?; - let it = jed::Iter::new(raw).into_iter(); - Ok(Box::new(it)) + .stream_post(&path.join("?"), Some((Body::from(bytes), tar()))) + .and_then(|r| { + serde_json::from_reader::<_, Vec>(r).map_err(Error::from) + }) } /// Lists the docker images on the current docker host @@ -188,7 +189,7 @@ impl<'a> Images<'a> { path.push(query); } let raw = self.docker.get(&path.join("?"))?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Returns a reference to a set of operations available for a named image @@ -202,21 +203,23 @@ impl<'a> Images<'a> { .append_pair("term", term) .finish(); let raw = self.docker.get(&format!("/images/search?{}", query)[..])?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Pull and create a new docker images from an existing image pub fn pull( &self, opts: &PullOptions, - ) -> Result>> { + ) -> Result> { let mut path = vec!["/images/create".to_owned()]; if let Some(query) = opts.serialize() { path.push(query); } - let raw = self.docker.stream_post::(&path.join("?"), None)?; - let it = jed::Iter::new(raw).into_iter(); - Ok(Box::new(it)) + self.docker + .stream_post::(&path.join("?"), None) + .and_then(|r| { + serde_json::from_reader::<_, Vec>(r).map_err(Error::from) + }) } /// exports a collection of named images, @@ -263,7 +266,7 @@ impl<'a, 'b> Container<'a, 'b> { let raw = self .docker .get(&format!("/containers/{}/json", self.id)[..])?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Returns a `top` view of information about the container process @@ -277,7 +280,7 @@ impl<'a, 'b> Container<'a, 'b> { } let raw = self.docker.get(&path.join("?"))?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Returns a stream of logs emitted but the container instance @@ -294,7 +297,7 @@ impl<'a, 'b> Container<'a, 'b> { let raw = self .docker .get(&format!("/containers/{}/changes", self.id)[..])?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Exports the current docker container into a tarball @@ -304,17 +307,13 @@ impl<'a, 'b> Container<'a, 'b> { } /// Returns a stream of stats specific to this container instance - pub fn stats(&self) -> Result>> { - let raw = self + pub fn stats(&self) -> Result> { + self .docker - .stream_get(&format!("/containers/{}/stats", self.id)[..])?; - let it = jed::Iter::new(raw).into_iter().map(|j| { - // fixme: better error handling - debug!("{:?}", j); - let s = json::encode(&j).unwrap(); - json::decode::(&s).unwrap() - }); - Ok(Box::new(it)) + .stream_get(&format!("/containers/{}/stats", self.id)[..]) + .and_then(|r|{ + serde_json::from_reader::<_, Vec>(r).map_err(Error::from) + }) } /// Start the container instance @@ -392,7 +391,7 @@ impl<'a, 'b> Container<'a, 'b> { let raw = self .docker .post::(&format!("/containers/{}/wait", self.id)[..], None)?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Delete the container instance @@ -426,19 +425,25 @@ impl<'a, 'b> Container<'a, 'b> { Ok(res) => { let data = "{}"; let mut bytes = data.as_bytes(); - self.docker - .stream_post( - &format!( - "/exec/{}/start", - Json::from_str(res.as_str()) - .unwrap() - .search("Id") - .unwrap() - .as_string() - .unwrap() - )[..], - Some((bytes, mime::APPLICATION_JSON)), - ).map(|stream| Tty::new(stream)) + let json: Value = serde_json::from_str(res.as_str())?; + + if let Value::Object(ref obj) = json { + self.docker + .stream_post( + &format!( + "/exec/{}/start", + obj + .get("Id") + .unwrap() + .as_str() + .unwrap() + )[..], + Some((bytes, mime::APPLICATION_JSON)), + ).map(|stream| Tty::new(stream)) + } else { + // TODO + panic!() + } } } } @@ -467,7 +472,7 @@ impl<'a> Containers<'a> { path.push(query) } let raw = self.docker.get(&path.join("?"))?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Returns a reference to a set of operations available to a specific container instance @@ -480,7 +485,7 @@ impl<'a> Containers<'a> { &'a self, opts: &ContainerOptions, ) -> Result { - let data = opts.serialize()?; + let data = serde_json::to_string(opts)?; let bytes = data.into_bytes(); let mut path = vec!["/containers/create".to_owned()]; @@ -495,7 +500,7 @@ impl<'a> Containers<'a> { let raw = self .docker .post(&path.join("?"), Some((bytes, mime::APPLICATION_JSON)))?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } } @@ -517,7 +522,7 @@ impl<'a> Networks<'a> { path.push(query); } let raw = self.docker.get(&path.join("?"))?; - Ok(json::decode::>(&raw)?) + Ok(serde_json::from_str::>(&raw)?) } /// Returns a reference to a set of operations available to a specific network instance @@ -536,7 +541,7 @@ impl<'a> Networks<'a> { let raw = self .docker .post(&path.join("?"), Some((bytes, mime::APPLICATION_JSON)))?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } } @@ -566,7 +571,7 @@ impl<'a, 'b> Network<'a, 'b> { /// Inspects the current docker network instance's details pub fn inspect(&self) -> Result { let raw = self.docker.get(&format!("/networks/{}", self.id)[..])?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Delete the network instance @@ -729,13 +734,13 @@ impl Docker { /// Returns version information associated with the docker daemon pub fn version(&self) -> Result { let raw = self.get("/version")?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Returns information associated with the docker daemon pub fn info(&self) -> Result { let raw = self.get("/info")?; - Ok(json::decode::(&raw)?) + Ok(serde_json::from_str::(&raw)?) } /// Returns a simple ping response indicating the docker daemon is accessible @@ -747,19 +752,13 @@ impl Docker { pub fn events( &self, opts: &EventsOptions, - ) -> Result>> { + ) -> Result> { let mut path = vec!["/events".to_owned()]; if let Some(query) = opts.serialize() { path.push(query); } - let raw = self.stream_get(&path.join("?")[..])?; - let it = jed::Iter::new(raw).into_iter().map(|j| { - debug!("{:?}", j); - // fixme: better error handling - let s = json::encode(&j).unwrap(); - json::decode::(&s).unwrap() - }); - Ok(Box::new(it)) + self.stream_get(&path.join("?")[..]) + .and_then(|r| serde_json::from_reader::<_, Vec>(r).map_err(Error::from)) } fn get(&self, endpoint: &str) -> Result { diff --git a/src/rep.rs b/src/rep.rs index a19c4c8..0fe1fd3 100644 --- a/src/rep.rs +++ b/src/rep.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct SearchResult { pub description: String, pub is_official: bool, @@ -11,7 +11,7 @@ pub struct SearchResult { pub star_count: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Image { pub Created: u64, @@ -23,7 +23,7 @@ pub struct Image { pub VirtualSize: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct ImageDetails { pub Architecture: String, @@ -39,7 +39,7 @@ pub struct ImageDetails { pub VirtualSize: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Container { pub Created: u64, @@ -54,7 +54,7 @@ pub struct Container { pub SizeRootFs: Option, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct ContainerDetails { pub AppArmorProfile: String, @@ -80,7 +80,7 @@ pub struct ContainerDetails { pub Mounts: Vec, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Mount { pub Source: String, @@ -89,7 +89,7 @@ pub struct Mount { pub RW: bool, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct State { pub Error: String, @@ -103,7 +103,7 @@ pub struct State { pub StartedAt: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct NetworkSettings { pub Bridge: String, @@ -115,7 +115,7 @@ pub struct NetworkSettings { pub Networks: HashMap } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct NetworkEntry { pub NetworkID: String, @@ -129,7 +129,7 @@ pub struct NetworkEntry { pub MacAddress: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct HostConfig { pub CgroupParent: Option, @@ -149,7 +149,7 @@ pub struct HostConfig { * pub VolumesFrom: Option */ } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Config { pub AttachStderr: bool, @@ -189,7 +189,7 @@ impl Config { } } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Port { pub IP: Option, @@ -198,7 +198,7 @@ pub struct Port { pub Type: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Stats { pub read: String, pub networks: HashMap, @@ -207,7 +207,7 @@ pub struct Stats { pub cpu_stats: CpuStats, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Network { pub rx_dropped: u64, pub rx_bytes: u64, @@ -219,7 +219,7 @@ pub struct Network { pub tx_bytes: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct IPAM { pub Driver: String, @@ -227,7 +227,7 @@ pub struct IPAM { pub Options: Option>, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct NetworkDetails { pub Name: String, @@ -243,7 +243,7 @@ pub struct NetworkDetails { pub Labels: Option>, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct NetworkContainerDetails { pub EndpointID: String, @@ -252,14 +252,14 @@ pub struct NetworkContainerDetails { pub IPv6Address: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct NetworkCreateInfo { pub Id: String, pub Warning: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct MemoryStats { pub max_usage: u64, pub usage: u64, @@ -268,7 +268,7 @@ pub struct MemoryStats { pub stats: MemoryStat, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct MemoryStat { pub total_pgmajfault: u64, pub cache: u64, @@ -302,14 +302,14 @@ pub struct MemoryStat { pub total_pgpgin: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct CpuStats { pub cpu_usage: CpuUsage, pub system_cpu_usage: u64, pub throttling_data: ThrottlingData, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct CpuUsage { pub percpu_usage: Vec, pub usage_in_usermode: u64, @@ -317,14 +317,14 @@ pub struct CpuUsage { pub usage_in_kernelmode: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct ThrottlingData { pub periods: u64, pub throttled_periods: u64, pub throttled_time: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct BlkioStats { pub io_service_bytes_recursive: Vec, pub io_serviced_recursive: Vec, @@ -336,7 +336,7 @@ pub struct BlkioStats { pub sectors_recursive: Vec, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct BlkioStat { pub major: u64, pub minor: u64, @@ -344,7 +344,7 @@ pub struct BlkioStat { pub value: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Change { pub Kind: u64, @@ -352,14 +352,14 @@ pub struct Change { } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Top { pub Titles: Vec, pub Processes: Vec>, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Version { pub ApiVersion: String, @@ -368,7 +368,7 @@ pub struct Version { pub GoVersion: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Info { pub Containers: u64, @@ -391,14 +391,14 @@ pub struct Info { pub SystemTime: Option, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct ContainerCreateInfo { pub Id: String, pub Warnings: Option>, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct History { pub Id: String, @@ -406,13 +406,13 @@ pub struct History { pub CreatedBy: String, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Exit { pub StatusCode: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Event { pub Type: String, @@ -425,7 +425,7 @@ pub struct Event { pub timeNano: u64, } -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[allow(non_snake_case)] pub struct Actor { pub ID: String, diff --git a/src/transport.rs b/src/transport.rs index 4cd0a96..0b52b60 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -16,7 +16,7 @@ use hyperlocal::UnixConnector; #[cfg(feature = "unix-socket")] use hyperlocal::Uri as DomainUri; use mime::Mime; -use rustc_serialize::json; +use serde_json::{self, Value}; use std::cell::{RefCell, RefMut}; use std::fmt; use std::io::Read; @@ -214,12 +214,12 @@ impl Transport { match String::from_utf8(chunk.into_iter().collect()) { Ok(output) => { - let json_response = json::Json::from_str(output.as_str()).ok(); + let json_response = serde_json::from_str::(output.as_str()).ok(); let message = json_response .as_ref() .and_then(|x| x.as_object()) .and_then(|x| x.get("message")) - .and_then(|x| x.as_string()) + .and_then(|x| x.as_str()) .map(|x| x.to_owned()); message -- cgit v1.2.3