Either -> MsgType to support a Result type

This commit is contained in:
Graham Christensen 2018-03-10 18:27:47 -05:00
parent 94836b6e1a
commit a796200325
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C

View file

@ -1,7 +1,6 @@
extern crate amqp; extern crate amqp;
extern crate env_logger; extern crate env_logger;
use either::{Either, Left, Right};
use lru_cache::LruCache; use lru_cache::LruCache;
use serde_json; use serde_json;
use std::fs; use std::fs;
@ -25,10 +24,16 @@ pub struct LogMessageCollector {
log_root: PathBuf, log_root: PathBuf,
} }
#[derive(Debug)]
enum MsgType {
Start(BuildLogStart),
Msg(BuildLogMsg),
}
#[derive(Debug)] #[derive(Debug)]
pub struct LogMessage { pub struct LogMessage {
from: LogFrom, from: LogFrom,
message: Either<BuildLogStart, BuildLogMsg>, message: MsgType
} }
fn validate_path_segment(segment: &PathBuf) -> Result<(), String> { fn validate_path_segment(segment: &PathBuf) -> Result<(), String> {
@ -158,18 +163,18 @@ impl worker::SimpleWorker for LogMessageCollector {
body: &Vec<u8>, body: &Vec<u8>,
) -> Result<Self::J, String> { ) -> Result<Self::J, String> {
let message: Either<BuildLogStart, BuildLogMsg>; let message: MsgType;
let attempt_id: String; let attempt_id: String;
let decode_msg: Result<BuildLogMsg, _> = serde_json::from_slice(body); let decode_msg: Result<BuildLogMsg, _> = serde_json::from_slice(body);
if let Ok(msg) = decode_msg { if let Ok(msg) = decode_msg {
attempt_id = msg.attempt_id.clone(); attempt_id = msg.attempt_id.clone();
message = Right(msg); message = MsgType::Msg(msg);
} else { } else {
let decode_msg: Result<BuildLogStart, _> = serde_json::from_slice(body); let decode_msg: Result<BuildLogStart, _> = serde_json::from_slice(body);
if let Ok(msg) = decode_msg { if let Ok(msg) = decode_msg {
attempt_id = msg.attempt_id.clone(); attempt_id = msg.attempt_id.clone();
message = Left(msg); message = MsgType::Start(msg);
} else { } else {
return Err(format!("failed to decode job: {:?}", decode_msg)); return Err(format!("failed to decode job: {:?}", decode_msg));
} }
@ -186,10 +191,10 @@ impl worker::SimpleWorker for LogMessageCollector {
fn consumer(&mut self, job: &LogMessage) -> worker::Actions { fn consumer(&mut self, job: &LogMessage) -> worker::Actions {
match job.message { match job.message {
Left(ref start) => { MsgType::Start(ref start) => {
self.write_metadata(&job.from, &start).expect("failed to write metadata"); self.write_metadata(&job.from, &start).expect("failed to write metadata");
}, },
Right(ref message) => { MsgType::Msg(ref message) => {
let handle = self.handle_for(&job.from).unwrap(); let handle = self.handle_for(&job.from).unwrap();
handle.write_to_line((message.line_number - 1) as usize, handle.write_to_line((message.line_number - 1) as usize,