-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.roc
More file actions
54 lines (42 loc) · 1.1 KB
/
Copy pathHelpers.roc
File metadata and controls
54 lines (42 loc) · 1.1 KB
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
import pf.Stdin
import pf.Stdout
import pf.Stderr
Helpers :: [].{
encode_json : a -> Str where [a.encoder_for : _]
encode_json = |a| Json.to_str(a)
decode_json : Str -> a where [a.parser_for : _]
decode_json = |str| {
match Json.parse(str) {
Ok(p) => p
Err(_) => {
crash "Json.parse(str)"
}
}
}
NodeState(a) : { node_id : Str, msg_id : U64, ..a }
reply! = |state, msg, body| {
send_message!(state, msg.src, { ..body, in_reply_to: msg.body.msg_id })
}
send_message! = |state, dest, body| {
payload = encode_json({ src: state.node_id, dest, body: { ..body, msg_id: state.msg_id } })
Stdout.line!(payload)?
Ok({ ..state, msg_id: state.msg_id + 1 })
}
Payload(body) : {
src : Str,
dest : Str,
body : body,
}
LoopState(a) : [WaitingForInit, ..a]
run! = |handle_input!| {
initial_state : LoopState(_)
initial_state = WaitingForInit
run_internal!(handle_input!, initial_state)
}
run_internal! = |handle_input!, state| {
line_str = Stdin.line!()?
Stderr.line!(line_str)?
new_state = handle_input!(line_str, state)?
run_internal!(handle_input!, new_state)
}
}