atproto pds in zig
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647const std = @import("std");const zat = @import("zat");
pub fn writeAlloc(allocator: std.mem.Allocator, value: zat.cbor.Value) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); defer out.deinit(); try writeValue(allocator, &out.writer, value); return out.toOwnedSlice();}
fn writeValue(allocator: std.mem.Allocator, writer: anytype, value: zat.cbor.Value) !void { switch (value) { .unsigned => |v| try writer.print("{d}", .{v}), .negative => |v| try writer.print("{d}", .{v}), .bytes => |bytes| { const encoded = try allocator.alloc(u8, std.base64.standard.Encoder.calcSize(bytes.len)); defer allocator.free(encoded); _ = std.base64.standard.Encoder.encode(encoded, bytes); try writer.print("{{\"$bytes\":{f}}}", .{std.json.fmt(encoded, .{})}); }, .text => |text| try writer.print("{f}", .{std.json.fmt(text, .{})}), .array => |items| { try writer.writeByte('['); for (items, 0..) |item, idx| { if (idx != 0) try writer.writeByte(','); try writeValue(allocator, writer, item); } try writer.writeByte(']'); }, .map => |entries| { try writer.writeByte('{'); for (entries, 0..) |entry, idx| { if (idx != 0) try writer.writeByte(','); try writer.print("{f}:", .{std.json.fmt(entry.key, .{})}); try writeValue(allocator, writer, entry.value); } try writer.writeByte('}'); }, .boolean => |v| try writer.print("{}", .{v}), .null => try writer.writeAll("null"), .cid => |cid| try writer.print( "{{\"$link\":{f}}}", .{std.json.fmt(try zat.multibase.base32lower.encode(allocator, cid.raw), .{})}, ), }}