chatty-catties/gg-uid/src/main.rs
2024-05-20 12:55:12 -07:00

35 lines
899 B
Rust

use std::{rc::Rc, sync::Mutex};
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 GenUid;
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 = Body::from_type("generate_ok")
.with_msg_id(mid)
.with_payload(payload);
runner.reply(msg, body);
}
}
}