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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! sync types - com.atproto.sync.subscribeRepos
//!
//! enums for firehose/event stream consumption.
//! see: https://atproto.com/specs/event-stream
const std = @import("std");
/// repo operation action (create/update/delete)
///
/// from com.atproto.sync.subscribeRepos#repoOp
/// used in firehose commit messages to indicate what happened to a record.
pub const CommitAction = enum {
create,
update,
delete,
/// parse from string (for manual parsing)
pub fn parse(s: []const u8) ?CommitAction {
return std.meta.stringToEnum(CommitAction, s);
}
};
/// event stream message types
///
/// from com.atproto.sync.subscribeRepos message union
/// the top-level discriminator for firehose messages.
pub const EventKind = enum {
commit,
sync,
identity,
account,
info,
pub fn parse(s: []const u8) ?EventKind {
return std.meta.stringToEnum(EventKind, s);
}
};
/// account status reasons
///
/// from com.atproto.sync.subscribeRepos#account status field
/// indicates why an account is inactive.
pub const AccountStatus = enum {
takendown,
suspended,
deleted,
deactivated,
desynchronized,
throttled,
pub fn parse(s: []const u8) ?AccountStatus {
return std.meta.stringToEnum(AccountStatus, s);
}
};
// === tests ===
test "CommitAction parse" {
try std.testing.expectEqual(CommitAction.create, CommitAction.parse("create").?);
try std.testing.expectEqual(CommitAction.update, CommitAction.parse("update").?);
try std.testing.expectEqual(CommitAction.delete, CommitAction.parse("delete").?);
try std.testing.expect(CommitAction.parse("invalid") == null);
}
test "CommitAction json parsing" {
const json_str =
\\{"action": "create", "path": "app.bsky.feed.post/abc"}
;
const Op = struct {
action: CommitAction,
path: []const u8,
};
const parsed = try std.json.parseFromSlice(Op, std.testing.allocator, json_str, .{});
defer parsed.deinit();
try std.testing.expectEqual(CommitAction.create, parsed.value.action);
}
test "EventKind parse" {
try std.testing.expectEqual(EventKind.commit, EventKind.parse("commit").?);
try std.testing.expectEqual(EventKind.identity, EventKind.parse("identity").?);
try std.testing.expect(EventKind.parse("unknown") == null);
}
test "AccountStatus parse" {
try std.testing.expectEqual(AccountStatus.takendown, AccountStatus.parse("takendown").?);
try std.testing.expectEqual(AccountStatus.suspended, AccountStatus.parse("suspended").?);
}