this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

jetstream: create event type

+42 -28
+11 -27
src/did.zig
··· 5 5 pub const DidError = error{ 6 6 InvalidDid, 7 7 UnsupportedMethod, 8 - } || Allocator.Error; 8 + }; 9 9 10 10 pub const Method = enum { 11 11 plc, ··· 16 16 plc: [24]u8, 17 17 web: Web, 18 18 19 - pub fn init(gpa: Allocator, bytes: []const u8) DidError!Did { 19 + pub fn init(bytes: []const u8) DidError!Did { 20 20 var iter = std.mem.splitScalar(u8, bytes, ':'); 21 21 if (!std.mem.eql(u8, iter.first(), "did")) { 22 22 return error.InvalidDid; ··· 51 51 break :blk .{ id[0..end], port }; 52 52 }; 53 53 54 - const path = iter.rest(); 55 - 56 54 return .{ .web = .{ 57 - .host = try gpa.dupe(u8, host), 55 + .host = host, 58 56 .port = port, 59 - .path = try gpa.dupe(u8, path), 57 + .path = iter.rest(), 60 58 } }; 61 59 }, 62 60 } 63 61 } 64 62 65 - pub fn deinit(self: Did, gpa: Allocator) void { 66 - switch (self) { 67 - .plc => return, 68 - .web => |web| { 69 - gpa.free(web.host); 70 - gpa.free(web.path); 71 - }, 72 - } 73 - } 74 - 75 63 pub fn format( 76 64 self: Did, 77 65 comptime _: []const u8, ··· 111 99 112 100 test "did:plc: valid" { 113 101 const id = "aBcDejfkllalsjlkllsjk234"; 114 - const did = try Did.init(undefined, "did:plc:" ++ id); 102 + const did = try Did.init("did:plc:" ++ id); 115 103 var out: [24]u8 = undefined; 116 104 try std.testing.expectEqualStrings(std.ascii.lowerString(&out, id), &did.plc); 117 105 } 118 106 119 107 test "did:plc: invalid character" { 120 108 const did = "did:plc:0BcDejfkllalsjlkllsjk234"; 121 - try std.testing.expectError(DidError.InvalidDid, Did.init(undefined, did)); 109 + try std.testing.expectError(DidError.InvalidDid, Did.init(did)); 122 110 } 123 111 124 112 test "did:plc: invalid length" { 125 113 const did = "did:plc:kllsjk234"; 126 - try std.testing.expectError(DidError.InvalidDid, Did.init(undefined, did)); 114 + try std.testing.expectError(DidError.InvalidDid, Did.init(did)); 127 115 } 128 116 129 117 test "did:web: host only" { 130 118 const did = "did:web:rockorager.dev"; 131 - const actual = try Did.init(std.testing.allocator, did); 132 - defer actual.deinit(std.testing.allocator); 119 + const actual = try Did.init(did); 133 120 try std.testing.expectEqualStrings("rockorager.dev", actual.web.host); 134 121 try std.testing.expectEqual(443, actual.web.port); 135 122 try std.testing.expectEqualStrings("", actual.web.path); ··· 141 128 142 129 test "did:web: host + port" { 143 130 const did = "did:web:rockorager.dev%3a80"; 144 - const actual = try Did.init(std.testing.allocator, did); 145 - defer actual.deinit(std.testing.allocator); 131 + const actual = try Did.init(did); 146 132 try std.testing.expectEqualStrings("rockorager.dev", actual.web.host); 147 133 try std.testing.expectEqual(80, actual.web.port); 148 134 try std.testing.expectEqualStrings("", actual.web.path); ··· 154 140 155 141 test "did:web: host + path" { 156 142 const did = "did:web:rockorager.dev:user:alice"; 157 - const actual = try Did.init(std.testing.allocator, did); 158 - defer actual.deinit(std.testing.allocator); 143 + const actual = try Did.init(did); 159 144 try std.testing.expectEqualStrings("rockorager.dev", actual.web.host); 160 145 try std.testing.expectEqual(443, actual.web.port); 161 146 try std.testing.expectEqualStrings("user:alice", actual.web.path); ··· 167 152 168 153 test "did:web: host + port + path" { 169 154 const did = "did:web:rockorager.dev%3A80:user:alice"; 170 - const actual = try Did.init(std.testing.allocator, did); 171 - defer actual.deinit(std.testing.allocator); 155 + const actual = try Did.init(did); 172 156 try std.testing.expectEqualStrings("rockorager.dev", actual.web.host); 173 157 try std.testing.expectEqual(80, actual.web.port); 174 158 try std.testing.expectEqualStrings("user:alice", actual.web.path);
+29
src/jetstream.zig
··· 2 2 const stda = @import("stda"); 3 3 const ourio = @import("ourio"); 4 4 5 + const atproto = @import("root.zig"); 6 + 5 7 const Allocator = std.mem.Allocator; 6 8 const dns = stda.net.dns; 9 + const json = std.json; 7 10 const posix = std.posix; 8 11 9 12 /// A stream of events from the jetstream ··· 315 318 } 316 319 317 320 self.buffer.replaceRangeAssumeCapacity(0, iter.idx, ""); 321 + } 322 + }; 323 + 324 + pub const Event = struct { 325 + raw: []const u8, 326 + value: json.Value, 327 + 328 + pub const Kind = enum { 329 + commit, 330 + identity, 331 + account, 332 + }; 333 + 334 + pub fn did(self: Event) atproto.Did { 335 + const value = self.value.object.get("did").?; 336 + return atproto.Did.init(value.string) catch unreachable; 337 + } 338 + 339 + pub fn time(self: Event) i64 { 340 + const value = self.value.object.get("time_us").?; 341 + return value.integer; 342 + } 343 + 344 + pub fn kind(self: Event) Kind { 345 + const value = self.value.object.get("kind").?; 346 + return std.meta.stringToEnum(Kind, value.string); 318 347 } 319 348 }; 320 349
+2 -1
src/root.zig
··· 1 1 const std = @import("std"); 2 2 const testing = std.testing; 3 3 4 - const did = @import("did.zig"); 4 + pub const did = @import("did.zig"); 5 5 const jetstream = @import("jetstream.zig"); 6 6 7 + pub const Did = did.Did; 7 8 pub const Jetstream = jetstream.Stream; 8 9 9 10 test {