~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
106 lines · 5.5 KB Raw History
//! Space references, `at:space/{spaceDid}/{spaceType}/{skey}`, and the
//! URIs of records within a space,
//! `at:space/{spaceDid}/{spaceType}/{skey}/{authorDid}/{collection}/{rkey}`.
//! Neither the space DID nor the author may be a handle.

const std = @import("std");
const Did = @import("../syntax/did.zig").Did;
const Nsid = @import("../syntax/nsid.zig").Nsid;
const Rkey = @import("../syntax/rkey.zig").Rkey;

pub const SpaceUri = struct {
    const prefix = "at:space/";

    space_did: []const u8,
    space_type: []const u8,
    skey: []const u8,
    author_did: ?[]const u8 = null,
    collection: ?[]const u8 = null,
    rkey: ?[]const u8 = null,

    /// Whether this names a space rather than a record within one.
    pub fn isSpaceRef(self: SpaceUri) bool {
        return self.author_did == null;
    }

    pub fn parse(uri: []const u8) ?SpaceUri {
        const rest = std.mem.cutPrefix(u8, uri, prefix) orelse return null;
        var parts = std.mem.splitScalar(u8, rest, '/');
        const space_did = parts.next() orelse return null;
        if (Did.parse(space_did) == null) return null;
        const space_type = parts.next() orelse return null;
        if (Nsid.parse(space_type) == null) return null;
        const skey = parts.next() orelse return null;
        if (Rkey.parse(skey) == null) return null;
        const author_did = parts.next() orelse return .{ .space_did = space_did, .space_type = space_type, .skey = skey };
        if (Did.parse(author_did) == null) return null;
        const collection = parts.next() orelse return null;
        if (Nsid.parse(collection) == null) return null;
        const rkey = parts.next() orelse return null;
        if (Rkey.parse(rkey) == null or parts.next() != null) return null;
        return .{ .space_did = space_did, .space_type = space_type, .skey = skey, .author_did = author_did, .collection = collection, .rkey = rkey };
    }

    /// A space ref: the three-part form only.
    pub fn parseRef(uri: []const u8) ?SpaceUri {
        const parsed = parse(uri) orelse return null;
        return if (parsed.isSpaceRef()) parsed else null;
    }

    pub fn formatRef(allocator: std.mem.Allocator, space_did: []const u8, space_type: []const u8, skey: []const u8) ![]u8 {
        if (Did.parse(space_did) == null) return error.InvalidDid;
        if (Nsid.parse(space_type) == null) return error.InvalidNsid;
        if (Rkey.parse(skey) == null) return error.InvalidRecordKey;
        return std.fmt.allocPrint(allocator, prefix ++ "{s}/{s}/{s}", .{ space_did, space_type, skey });
    }

    /// The URI of a record within the space `space_ref`.
    pub fn formatRecord(allocator: std.mem.Allocator, space_ref: []const u8, author_did: []const u8, collection: []const u8, rkey: []const u8) ![]u8 {
        _ = parseRef(space_ref) orelse return error.InvalidSpaceUri;
        if (Did.parse(author_did) == null) return error.InvalidDid;
        if (Nsid.parse(collection) == null) return error.InvalidNsid;
        if (Rkey.parse(rkey) == null) return error.InvalidRecordKey;
        return std.fmt.allocPrint(allocator, "{s}/{s}/{s}/{s}", .{ space_ref, author_did, collection, rkey });
    }
};

test "parses space refs and record uris" {
    const ref = SpaceUri.parseRef("at:space/did:plc:abc/fm.plyr.privateMedia/self").?;
    try std.testing.expectEqualStrings("did:plc:abc", ref.space_did);
    try std.testing.expectEqualStrings("fm.plyr.privateMedia", ref.space_type);
    try std.testing.expectEqualStrings("self", ref.skey);
    const record = SpaceUri.parse("at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author/fm.plyr.track/key").?;
    try std.testing.expectEqualStrings("did:plc:author", record.author_did.?);
    try std.testing.expectEqualStrings("fm.plyr.track", record.collection.?);
    try std.testing.expectEqualStrings("key", record.rkey.?);
    try std.testing.expect(SpaceUri.parseRef("at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author/fm.plyr.track/key") == null);
}

test "formats space refs and record uris" {
    const a = std.testing.allocator;
    const ref = try SpaceUri.formatRef(a, "did:plc:abc", "fm.plyr.privateMedia", "self");
    defer a.free(ref);
    const uri = try SpaceUri.formatRecord(a, ref, "did:plc:author", "fm.plyr.track", "key");
    defer a.free(uri);
    try std.testing.expectEqualStrings("at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author/fm.plyr.track/key", uri);
    try std.testing.expectError(error.InvalidSpaceUri, SpaceUri.formatRecord(a, uri, "did:plc:author", "fm.plyr.track", "key"));
    try std.testing.expectError(error.InvalidDid, SpaceUri.formatRef(a, "alice.example.com", "fm.plyr.privateMedia", "self"));
}

test "refuses handles, legacy and malformed uris" {
    const bad = [_][]const u8{
        "at://did:plc:abc/space/fm.plyr.privateMedia/self",
        "ats://did:plc:abc/fm.plyr.privateMedia/self",
        "at://did:plc:abc/fm.plyr.privateMedia/self",
        "at:space/alice.example.com/fm.plyr.privateMedia/self",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author/fm.plyr.track",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self/alice.example.com/fm.plyr.track/key",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self/did:plc:author/fm.plyr.track/key/extra",
        "at:space/did:plc:abc/notansid/self",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self/",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self?key=value",
        "at:space/did:plc:abc/fm.plyr.privateMedia/self#/member",
    };
    for (bad) |uri| try std.testing.expect(SpaceUri.parse(uri) == null);
}