-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniq-ids.rs
54 lines (47 loc) · 1.32 KB
/
uniq-ids.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use rustagen::*;
use std::io::StdoutLock;
use std::io::Write;
use anyhow::Context;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
enum Payload {
Generate,
GenerateOk {
#[serde(rename = "id")]
guid: String,
},
}
struct UniqNode {
node: String,
id: usize,
}
impl Node<(), Payload> for UniqNode {
fn from_init(_state: (), init: Init) -> anyhow::Result<Self>
where
Self: Sized,
{
Ok(UniqNode {
id: 1,
node: init.node_id,
})
}
fn step(&mut self, input: Message<Payload>, output: &mut StdoutLock) -> anyhow::Result<()> {
let mut reply = input.into_reply(Some(&mut self.id));
match reply.body.payload {
Payload::Generate => {
let guid = format!("{}-{}", self.node, self.id);
reply.body.payload = Payload::GenerateOk { guid };
serde_json::to_writer(&mut *output, &reply)
.context("serialize response to generate")?;
output.write_all(b"\n").context("write trailing newline")?;
}
Payload::GenerateOk { .. } => {}
}
Ok(())
}
}
fn main() -> anyhow::Result<()> {
main_loop::<_, UniqNode, _>(())
}