From 417f01d4844e2a2c1e57ed0b7fdb0b3c3ea3750c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 20 May 2024 12:55:12 -0700 Subject: [PATCH] port gg-uid to nebkor-maelstrom --- gg-uid/Cargo.toml | 4 +--- gg-uid/src/main.rs | 52 ++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/gg-uid/Cargo.toml b/gg-uid/Cargo.toml index d3d06ac..87102f9 100644 --- a/gg-uid/Cargo.toml +++ b/gg-uid/Cargo.toml @@ -6,7 +6,5 @@ authors.workspace = true license-file.workspace = true [dependencies] -async-trait.workspace = true -maelstrom-node.workspace = true serde_json.workspace = true -tokio.workspace = true +nebkor-maelstrom = { path = "../nebkor-maelstrom" } diff --git a/gg-uid/src/main.rs b/gg-uid/src/main.rs index 53eee63..2a6168a 100644 --- a/gg-uid/src/main.rs +++ b/gg-uid/src/main.rs @@ -1,37 +1,35 @@ -use async_trait::async_trait; -use maelstrom::protocol::Message; -use maelstrom::{done, protocol::MessageBody, Node, Result, Runtime}; -use serde_json::{Map, Value}; -use std::sync::Arc; +use std::{rc::Rc, sync::Mutex}; -#[tokio::main] -async fn main() { - let handler = Arc::new(Handler); - let _ = Runtime::new() - .with_handler(handler) - .run() - .await - .unwrap_or_default(); +use nebkor_maelstrom::{protocol::Payload, Body, Message, Node, Runner}; + +fn main() { + let out = std::io::stdout().lock(); + let input = std::io::stdin().lock(); + + let node = GenUid; + let node = Rc::new(Mutex::new(node)); + + let runner = Runner::new(out, node); + + runner.run(input); } #[derive(Clone, Default)] -struct Handler; +struct GenUid; -#[async_trait] -impl Node for Handler { - async fn process(&self, runtime: Runtime, req: Message) -> Result<()> { - let id = runtime.node_id(); - let mid = runtime.next_msg_id(); - if req.get_type() == "generate" { - let extra: Map = [("id".to_string(), format!("{id}{mid}").into())] +impl Node for GenUid { + fn handle(&mut self, runner: &Runner, msg: &Message) { + let id = runner.node_id(); + let mid = runner.next_msg_id(); + if msg.body.typ == "generate" { + let payload: Payload = [("id".to_string(), format!("{id}{mid}").into())] .into_iter() .collect(); - let body = MessageBody::from_extra(extra) - .with_type("generate_ok") - .and_msg_id(mid); - return runtime.reply(req, body).await; - } + let body = Body::from_type("generate_ok") + .with_msg_id(mid) + .with_payload(payload); - done(runtime, req) + runner.reply(msg, body); + } } }