Skip to content

Commit

Permalink
Remove reliance on rustc-serialize: #26. Still awaiting rust-mustache…
Browse files Browse the repository at this point in the history
… Serde support.
  • Loading branch information
petehayes102 committed Feb 27, 2017
1 parent 559c4e3 commit 1bb9892
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -26,9 +26,9 @@ mustache = "0.8"
regex = "0.2"
rustc-serialize = "0.3"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
tempfile = "2.1"
zdaemon = "0.0.2"
zfilexfer = "0.0.2"
hostname = "0.1"
czmq = { version = "0.1", optional = true }
Expand Down
22 changes: 0 additions & 22 deletions src/error.rs
Expand Up @@ -11,12 +11,10 @@ use czmq;
use libc::c_char;
use mustache;
use regex;
use rustc_serialize::json;
use serde_json;
use std::{convert, error, ffi, fmt, io, num, ptr, result, str, string};
use std::any::Any;
use std::ffi::CString;
use zdaemon;
#[cfg(feature = "remote-run")]
use zfilexfer;

Expand Down Expand Up @@ -70,8 +68,6 @@ pub enum Error {
InvalidFileDescriptor,
/// IO error
Io(io::Error),
/// JSON decoder error
JsonDecoder(json::DecoderError),
/// Mustache template error
Mustache(mustache::Error),
/// FFI null error
Expand All @@ -92,8 +88,6 @@ pub enum Error {
StrFromUtf8(str::Utf8Error),
/// Cast String
StringFromUtf8(string::FromUtf8Error),
// ZDaemon error
ZDaemon(zdaemon::Error),
#[cfg(feature = "remote-run")]
/// ZFileXfer error
ZFileXfer(zfilexfer::Error),
Expand Down Expand Up @@ -122,7 +116,6 @@ impl fmt::Display for Error {
Error::HostResponse => write!(f, "Invalid response from host"),
Error::InvalidFileDescriptor => write!(f, "Invalid file descriptor"),
Error::Io(ref e) => write!(f, "IO error: {}", e),
Error::JsonDecoder(ref e) => write!(f, "JSON decoder error: {}", e),
Error::Mustache(ref e) => write!(f, "Mustache error: {:?}", e),
Error::NulError(ref e) => write!(f, "Nul error: {}", e),
Error::NullPtr(ref e) => write!(f, "Received null when we expected a {} pointer", e),
Expand All @@ -133,7 +126,6 @@ impl fmt::Display for Error {
Error::SerdeJson(ref e) => write!(f, "Serde JSON error: {}", e),
Error::StrFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to str error: {}", e),
Error::StringFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to String error: {}", e),
Error::ZDaemon(ref e) => write!(f, "ZDaemon error: {}", e),
#[cfg(feature = "remote-run")]
Error::ZFileXfer(ref e) => write!(f, "ZFileXfer error: {}", e),
}
Expand Down Expand Up @@ -161,7 +153,6 @@ impl error::Error for Error {
Error::HostResponse => "Invalid response from host",
Error::InvalidFileDescriptor => "Invalid file descriptor",
Error::Io(ref e) => e.description(),
Error::JsonDecoder(ref e) => e.description(),
Error::Mustache(ref e) => e.description(),
Error::NulError(ref e) => e.description(),
Error::NullPtr(ref e) => e,
Expand All @@ -172,7 +163,6 @@ impl error::Error for Error {
Error::SerdeJson(ref e) => e.description(),
Error::StrFromUtf8(ref e) => e.description(),
Error::StringFromUtf8(ref e) => e.description(),
Error::ZDaemon(ref e) => e.description(),
#[cfg(feature = "remote-run")]
Error::ZFileXfer(ref e) => e.description(),
}
Expand Down Expand Up @@ -211,12 +201,6 @@ impl convert::From<io::Error> for Error {
}
}

impl convert::From<json::DecoderError> for Error {
fn from(err: json::DecoderError) -> Error {
Error::JsonDecoder(err)
}
}

#[cfg(feature = "remote-run")]
impl convert::From<MissingFrame> for Error {
fn from(err: MissingFrame) -> Error {
Expand Down Expand Up @@ -266,12 +250,6 @@ impl convert::From<num::ParseIntError> for Error {
}
}

impl convert::From<zdaemon::Error> for Error {
fn from(err: zdaemon::Error) -> Error {
Error::ZDaemon(err)
}
}

#[cfg(feature = "remote-run")]
impl convert::From<zfilexfer::Error> for Error {
fn from(err: zfilexfer::Error) -> Error {
Expand Down
31 changes: 27 additions & 4 deletions src/lib.rs
Expand Up @@ -30,11 +30,12 @@ extern crate regex;
extern crate rustc_serialize;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
#[cfg(test)]
extern crate tempdir;
extern crate tempfile;
extern crate zdaemon;
extern crate zfilexfer;
extern crate hostname;
extern crate pnet;
Expand Down Expand Up @@ -79,7 +80,14 @@ pub use template::{Template, ffi as template_ffi};
pub use zfilexfer::FileOptions;

#[cfg(feature = "remote-run")]
use zdaemon::ConfigFile;
use std::fs;
#[cfg(feature = "remote-run")]
use std::io::Read;
#[cfg(feature = "remote-run")]
#[cfg(test)]
use std::io::Write;
#[cfg(feature = "remote-run")]
use std::path::Path;

#[cfg(all(test, feature = "remote-run"))]
lazy_static! {
Expand All @@ -88,6 +96,21 @@ lazy_static! {

#[cfg(feature = "remote-run")]
lazy_static! {
static ref PROJECT_CONFIG: project::ProjectConfig = project::ProjectConfig::load("project.json")
.expect("Could not load project.json");
static ref PROJECT_CONFIG: project::ProjectConfig = read_conf("project.json").expect("Could not load project.json");
}

#[cfg(feature = "remote-run")]
fn read_conf<T: serde::Deserialize, P: AsRef<Path>>(path: P) -> error::Result<T> {
let mut fh = fs::File::open(&path)?;
let mut json = String::new();
fh.read_to_string(&mut json)?;
Ok(serde_json::from_str(&json)?)
}

#[cfg(all(test, feature = "remote-run"))]
fn write_conf<C: serde::Serialize, P: AsRef<Path>>(conf: C, path: P) -> error::Result<()> {
let json = serde_json::to_string(&conf)?;
let mut fh = fs::File::create(&path)?;
fh.write_all(json.as_bytes())?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/mock_env.rs
Expand Up @@ -11,7 +11,7 @@ use project::{Language, ProjectConfig};
use std::env::set_current_dir;
use std::thread::{JoinHandle, spawn};
use tempdir::TempDir;
use zdaemon::ConfigFile;
use write_conf;

pub struct MockEnv {
_auth_handler: JoinHandle<()>,
Expand Down Expand Up @@ -44,7 +44,7 @@ impl MockEnv {
auth_api_port: 0,
auth_update_port: port as u32,
};
config.save("project.json").unwrap();
write_conf(&config, "project.json").unwrap();

let handle = spawn(move|| MockEnv::auth_handler(sock));

Expand Down
5 changes: 1 addition & 4 deletions src/payload/config.rs
Expand Up @@ -7,14 +7,11 @@
// modified, or distributed except according to those terms.

use project::Language;
use zdaemon::ConfigFile;

#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub author: String,
pub repository: String,
pub language: Language,
pub dependencies: Option<Vec<String>>,
}

impl ConfigFile for Config {}
8 changes: 4 additions & 4 deletions src/payload/ffi.rs
Expand Up @@ -75,7 +75,7 @@ mod tests {
use std::{fs, ptr, thread};
use super::*;
use tempdir::TempDir;
use zdaemon::ConfigFile;
use write_conf;

#[test]
fn test_new() {
Expand All @@ -96,7 +96,7 @@ mod tests {
};

buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

let payload_artifact = CString::new(buf.to_str().unwrap()).unwrap();
Expand All @@ -120,7 +120,7 @@ mod tests {

let mut buf = tempdir.path().to_owned();
buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

let payload_artifact = CString::new(buf.to_str().unwrap()).unwrap();
Expand Down Expand Up @@ -159,7 +159,7 @@ mod tests {
};

buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

let payload_name = buf.into_os_string().into_string().unwrap();
Expand Down
12 changes: 6 additions & 6 deletions src/payload/mod.rs
Expand Up @@ -15,13 +15,13 @@ use czmq::{ZMsg, ZPoller, ZSock, SocketType, ZSys};
use error::{Error, Result};
use host::{Host,HostSendRecv};
use project::Language;
use read_conf;
use self::config::Config;
use serde_json;
use std::env::{current_dir, set_current_dir};
use std::process;
use std::path::PathBuf;
use std::thread;
use zdaemon::ConfigFile;

/// Payloads are self-contained projects that encapsulate a specific
/// feature or system function.
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Payload {
buf.push(payload);

buf.push("payload.json");
let config = try!(Config::load(&buf));
let config: Config = read_conf(&buf)?;
buf.pop();

// Check dependencies
Expand Down Expand Up @@ -350,7 +350,7 @@ mod tests {
use super::*;
use super::config::Config;
use tempdir::TempDir;
use zdaemon::ConfigFile;
use write_conf;

#[test]
fn test_new_nodeps() {
Expand All @@ -369,7 +369,7 @@ mod tests {
};

buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

assert!(Payload::new(buf.to_str().unwrap()).is_err());
Expand All @@ -393,7 +393,7 @@ mod tests {
};

buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

let payload = Payload::new(buf.to_str().unwrap()).unwrap();
Expand All @@ -417,7 +417,7 @@ mod tests {
};

buf.push("payload.json");
conf.save(&buf).unwrap();
write_conf(&conf, &buf).unwrap();
buf.pop();

let payload_name = buf.into_os_string().into_string().unwrap();
Expand Down
8 changes: 2 additions & 6 deletions src/project.rs
Expand Up @@ -6,23 +6,19 @@
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.

use zdaemon::ConfigFile;

#[repr(C)]
#[derive(Clone, Debug, PartialEq, RustcDecodable, RustcEncodable)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// The payload's programming language.
pub enum Language {
C,
Php,
Rust,
}

#[derive(Debug, RustcDecodable, RustcEncodable)]
#[derive(Debug, Serialize, Deserialize)]
pub struct ProjectConfig {
pub language: Language,
pub auth_server: String,
pub auth_api_port: u32,
pub auth_update_port: u32,
}

impl ConfigFile for ProjectConfig {}
1 change: 0 additions & 1 deletion src/target/unix_base.rs
Expand Up @@ -8,7 +8,6 @@

use error::{Error, Result};
use file::FileOwner;
use host::telemetry::Netif;
use regex::Regex;
use std::{process, str};
use std::path::Path;
Expand Down

0 comments on commit 1bb9892

Please sign in to comment.