From e189bb3ec7a355e314895a77d3ed25c393cf042b Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Oct 2023 14:03:01 +0100 Subject: refactoring --- common/src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/src/lib.rs b/common/src/lib.rs index d0db5d1..616dfcc 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,7 +1,8 @@ -use std::io::{Stdout, Write}; +use std::io::{Read, Write}; -use msg::{MaelstromBodyOr, Message, MessageHeader}; +use msg::{MaelstromBody, MaelstromBodyOr, Message, MessageHeader}; use serde::{Deserialize, Serialize}; +use serde_json::Deserializer; pub mod msg; @@ -13,7 +14,7 @@ pub trait Handler { &mut self, header: MessageHeader, body: Self::Body, - writer: &mut MsgWriter, + writer: &mut MsgWriter, ) -> (); } @@ -39,3 +40,55 @@ impl MsgWriter { self.writer.write(&[b'\n']).unwrap(); } } + +pub fn run_with(mut reader: impl Read, mut writer: impl Write) { + let (mut handler, mut msg_writer) = init_handler::(&mut reader, &mut writer); + + let deser = Deserializer::from_reader(reader); + for msg in deser.into_iter::>() { + let msg = msg.unwrap(); + match msg.body { + MaelstromBodyOr::Other { inner } => { + handler.handle(msg.header, inner, &mut msg_writer); + } + _ => todo!(), + }; + } +} + +pub fn init_handler(reader: R, writer: W) -> (T, MsgWriter) { + let deser = Deserializer::from_reader(reader); + let mut deser = deser.into_iter::>(); + let Some(msg) = deser.next() else { + panic!("stream ended before init message"); + }; + let Ok(msg) = msg else { + panic!("{}", msg.unwrap_err()); + }; + + let (node_id, node_ids, msg_id) = match msg.body { + MaelstromBodyOr::MaelstromBody { + inner: + MaelstromBody::Init { + node_id, + node_ids, + msg_id, + }, + } => (node_id, node_ids, msg_id), + _ => { + panic!("expected init message to be first message"); + } + }; + + let mut writer = MsgWriter::new(node_id.clone(), writer); + + writer.write( + msg.header.src, + &MaelstromBody::InitOk { + msg_id: 0, + in_reply_to: msg_id, + }, + ); + + (T::init(node_id, node_ids, msg_id), writer) +} -- cgit v1.2.3