From 3186c08f32aefe7b76a8e061cca7d3af332871cd Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 17 May 2024 16:26:02 -0700 Subject: [PATCH] finished with generating unique ids --- Cargo.lock | 6 ++++++ gg-uid/Cargo.toml | 12 ++++++++++++ gg-uid/src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 gg-uid/Cargo.toml create mode 100644 gg-uid/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 4795bcb..3985187 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,12 @@ dependencies = [ [[package]] name = "gg-uid" version = "0.0.1" +dependencies = [ + "async-trait", + "maelstrom-node", + "serde_json", + "tokio", +] [[package]] name = "gimli" diff --git a/gg-uid/Cargo.toml b/gg-uid/Cargo.toml new file mode 100644 index 0000000..d9a46e8 --- /dev/null +++ b/gg-uid/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "gg-uid" +edition = "2021" +version.workspace = true +authors.workspace = true +license-file.workspace = true + +[dependencies] +async-trait.workspace = true +maelstrom-node.workspace = true +serde_json = "1.0.117" +tokio.workspace = true diff --git a/gg-uid/src/main.rs b/gg-uid/src/main.rs new file mode 100644 index 0000000..53eee63 --- /dev/null +++ b/gg-uid/src/main.rs @@ -0,0 +1,37 @@ +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; + +#[tokio::main] +async fn main() { + let handler = Arc::new(Handler); + let _ = Runtime::new() + .with_handler(handler) + .run() + .await + .unwrap_or_default(); +} + +#[derive(Clone, Default)] +struct Handler; + +#[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())] + .into_iter() + .collect(); + let body = MessageBody::from_extra(extra) + .with_type("generate_ok") + .and_msg_id(mid); + return runtime.reply(req, body).await; + } + + done(runtime, req) + } +}