finished with generating unique ids

This commit is contained in:
Joe Ardent 2024-05-17 16:26:02 -07:00
parent d07f852e7c
commit 3186c08f32
3 changed files with 55 additions and 0 deletions

6
Cargo.lock generated
View file

@ -196,6 +196,12 @@ dependencies = [
[[package]]
name = "gg-uid"
version = "0.0.1"
dependencies = [
"async-trait",
"maelstrom-node",
"serde_json",
"tokio",
]
[[package]]
name = "gimli"

12
gg-uid/Cargo.toml Normal file
View file

@ -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

37
gg-uid/src/main.rs Normal file
View file

@ -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<String, Value> = [("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)
}
}