Skip to content

Commit

Permalink
Migrate from rustc_serialize to serde
Browse files Browse the repository at this point in the history
serde is Rust's next-generation serialisation framework that will
eventually supersede rustc_serialize.

Convert our JSON and TOML serialisation to use serde. We still depend on
rustc_serialize because docopt currently requires it.

Closes: #39 ("migrate from rustc_serialize to serde")
Signed-off-by: Andrew Donnellan <[email protected]>
  • Loading branch information
ajdlinux committed Mar 20, 2017
1 parent 25fab3c commit 317f682
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 67 deletions.
99 changes: 95 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ hyper-openssl = "0.2"
git2 = "0.6"
rustc-serialize = "0.3"
url = "1.4"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
mime = "0.2"
toml = "0.2"
toml = "0.3"
tempdir = "0.3"
docopt = "0.7"
log = "0.3"
Expand Down
26 changes: 13 additions & 13 deletions src/jenkins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

extern crate hyper;
extern crate url;
extern crate rustc_serialize;

use std::io::Read;
use std::time::Duration;
Expand All @@ -33,7 +32,7 @@ use std::collections::BTreeMap;
use hyper::Client;
use hyper::client::{IntoUrl, RequestBuilder};
use hyper::header::{Headers, Basic, Authorization, Location};
use rustc_serialize::json::Json;
use serde_json::{self, Value};

use patchwork::TestState;

Expand Down Expand Up @@ -106,15 +105,16 @@ impl JenkinsBackend {
self.hyper_client.post(url).headers(self.headers())
}

fn get_api_json_object(&self, base_url: &str) -> rustc_serialize::json::Object {
fn get_api_json_object(&self, base_url: &str) -> Value {
// TODO: Don't panic on failure, fail more gracefully
let url = format!("{}api/json", base_url);
let mut resp = self.get(&url).send().expect("HTTP request error");
let mut result_str = String::new();
resp.read_to_string(&mut result_str)
.unwrap_or_else(|err| panic!("Couldn't read from server: {}", err));
let json = Json::from_str(&result_str).unwrap_or_else(|err| panic!("Couldn't parse JSON from Jenkins: {}", err));
json.as_object().unwrap().clone()
serde_json::from_str(&result_str).unwrap_or_else(
|err| panic!("Couldn't parse JSON from Jenkins: {}", err)
)
}

pub fn get_build_url(&self, build_queue_entry: &str) -> Option<String> {
Expand All @@ -124,9 +124,9 @@ impl JenkinsBackend {
Some(exec) => return Some(exec
.as_object() // Option<BTreeMap>
.unwrap() // BTreeMap
.get("url") // Option<&str> ?
.get("url") // Option<&str>
.unwrap() // &str ?
.as_string()
.as_str()
.unwrap()
.to_string()),
None => sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)),
Expand All @@ -135,7 +135,7 @@ impl JenkinsBackend {
}

pub fn get_build_status(&self, build_url: &str) -> JenkinsBuildStatus {
if self.get_api_json_object(build_url)["building"].as_boolean().unwrap() {
if self.get_api_json_object(build_url)["building"].as_bool().unwrap() {
JenkinsBuildStatus::Running
} else {
JenkinsBuildStatus::Done
Expand All @@ -144,13 +144,13 @@ impl JenkinsBackend {

pub fn get_build_result(&self, build_url: &str) -> Option<TestState> {
match self.get_api_json_object(build_url).get("result").unwrap()
.as_string() {
.as_str() {
None => None,
Some(result) => match result { // TODO: Improve this...
"SUCCESS" => Some(TestState::success),
"FAILURE" => Some(TestState::failure),
"UNSTABLE" => Some(TestState::warning),
_ => Some(TestState::pending),
"SUCCESS" => Some(TestState::Success),
"FAILURE" => Some(TestState::Fail),
"UNSTABLE" => Some(TestState::Warning),
_ => Some(TestState::Pending),
},
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ extern crate hyper;
extern crate hyper_openssl;
extern crate rustc_serialize;
extern crate git2;
extern crate toml;
extern crate tempdir;
extern crate docopt;
extern crate url;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate toml;

use git2::{BranchType, RemoteCallbacks, PushOptions};

Expand Down Expand Up @@ -200,7 +204,7 @@ fn test_patch(settings: &Config, client: &Arc<Client>, project: &Project, path:
successfully_applied = true;
results.push(TestResult {
test_name: "apply_patch".to_string(),
state: TestState::success,
state: TestState::Success,
url: None,
summary: Some(format!("Successfully applied to branch {}", branch_name)),
});
Expand All @@ -209,7 +213,7 @@ fn test_patch(settings: &Config, client: &Arc<Client>, project: &Project, path:
// It didn't apply. No need to bother testing.
results.push(TestResult {
test_name: "apply_patch".to_string(),
state: TestState::warning,
state: TestState::Warning,
url: None,
summary: Some(format!("Failed to apply to branch {}", branch_name)),
});
Expand All @@ -235,7 +239,7 @@ fn test_patch(settings: &Config, client: &Arc<Client>, project: &Project, path:
if !successfully_applied {
results.push(TestResult {
test_name: "apply_patch".to_string(),
state: TestState::failure,
state: TestState::Fail,
url: None,
summary: Some("Failed to apply to any branch".to_string()),
});
Expand All @@ -260,7 +264,7 @@ fn main() {
.and_then(|d| d.version(Some(version)).decode())
.unwrap_or_else(|e| e.exit());

let settings = settings::parse(args.arg_config_file);
let settings = settings::parse(&args.arg_config_file);

// The HTTP client we'll use to access the APIs
// TODO: HTTPS support, not yet implemented in Hyper as of 0.9.6
Expand Down
Loading

0 comments on commit 317f682

Please sign in to comment.