1package syntax
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7)
8
9// Parses an atproto repo path string in to "collection" (NSID) and record key parts.
10//
11// Does not return partial success: either both collection and record key are complete (and error is nil), or both are empty string (and error is not nil)
12func ParseRepoPath(raw string) (NSID, RecordKey, error) {
13 parts := strings.SplitN(raw, "/", 3)
14 if len(parts) != 2 {
15 return "", "", errors.New("expected path to have two parts, separated by single slash")
16 }
17 nsid, err := ParseNSID(parts[0])
18 if err != nil {
19 return "", "", fmt.Errorf("collection part of path not a valid NSID: %w", err)
20 }
21 rkey, err := ParseRecordKey(parts[1])
22 if err != nil {
23 return "", "", fmt.Errorf("record key part of path not valid: %w", err)
24 }
25 return nsid, rkey, nil
26}