๐Ÿ€ ATproto library for gleam
atproto library gleam
35

Configure Feed

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

possum/at_uri: pattern match on string.split when parsing

kacaii.dev (Jul 11, 2026, 4:53 PM -0300) e718e9af d8ee28ad

+28 -22
+25 -19
src/possum/at_uri.gleam
··· 16 16 InvalidHandleAuthority(reason: handle.ParseError) 17 17 /// Authority not a valid DID 18 18 InvalidDidAuthority(reason: did.ParseError) 19 + /// Something else went wrong 20 + InvalidFormat(value: String) 19 21 } 20 22 21 23 pub type Authority { ··· 73 75 /// let string = "at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g" 74 76 /// let assert Ok(at_uri) = at_uri.parse(string) 75 77 pub fn parse(content: String) -> Result(AtUri, ParseError) { 76 - use rest <- result.try(case content { 78 + use value <- result.try(case content { 77 79 "at://" <> rest -> Ok(rest) 78 80 79 81 // Other schemes like "HTTP" and "HTTPS" 80 82 _ -> 81 83 case string.split_once(content, on: "://") { 82 - Ok(scheme) -> Error(InvalidScheme(scheme.0)) 83 - Error(_) -> Error(InvalidScheme("")) 84 + Ok(scheme) -> Error(InvalidScheme(value: scheme.0)) 85 + Error(_) -> Error(InvalidScheme(value: "")) 84 86 } 85 87 }) 86 88 87 - case string.split_once(rest, "/") { 89 + case string.split(value, on: "/") { 90 + // Honestly I dont know how this one could happen. 91 + [] -> Error(InvalidFormat(value:)) 92 + 88 93 // Contains only authority (DID or handle) 89 94 // ex: at://kacaii.dev 90 - Error(_) | Ok(#(_, "")) -> { 91 - use authority <- result.map(parse_authority(rest)) 95 + // ^^ authority 96 + [authority] -> { 97 + use authority <- result.map(parse_authority(authority)) 92 98 AtUri(authority:, collection: "", rkey: "") 93 99 } 94 100 95 101 // Contains at least authority and collection 96 102 // ex: at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post 97 - Ok(#(authority, rest)) -> { 103 + // ^^ authority ^^ collection 104 + [authority, collection] -> { 98 105 use authority <- result.map(parse_authority(authority)) 99 - 100 - case string.split_once(rest, "/") { 101 - // Contains only collection 102 - // ex: at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post 103 - Error(_) -> AtUri(authority:, collection: rest, rkey: "") 106 + AtUri(authority:, collection:, rkey: "") 107 + } 104 108 105 - // Contains collection and rkey 106 - // ex: at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g 107 - Ok(#(collection, rkey)) -> AtUri(authority:, collection:, rkey:) 108 - } 109 + // Contains authority, collection and rkey 110 + // ex: at://did:plc:vwzwgnygau7ed7b7wt5ux7y2/app.bsky.feed.post/3k5nobkf2w72g 111 + // ^^ authority ^^ collection ^^ rkey 112 + [authority, collection, rkey, ..] -> { 113 + use authority <- result.map(parse_authority(authority)) 114 + AtUri(authority:, collection:, rkey:) 109 115 } 110 116 } 111 117 } ··· 114 120 case string { 115 121 "did:" <> _rest -> 116 122 case did.parse(string) { 117 - Ok(value) -> Ok(Did(value)) 123 + Ok(value) -> Ok(Did(value:)) 118 124 Error(reason) -> Error(InvalidDidAuthority(reason:)) 119 125 } 120 126 121 127 _handle -> 122 128 case handle.parse(string) { 123 - Ok(value) -> Ok(Handle(value)) 129 + Ok(value) -> Ok(Handle(value:)) 124 130 Error(reason) -> Error(InvalidHandleAuthority(reason:)) 125 131 } 126 132 } ··· 148 154 let path = case self.collection, self.rkey { 149 155 "", _ -> "" 150 156 collection, "" -> "/" <> collection 151 - collection, rkey -> "/" <> string.join([collection, rkey], "/") 157 + collection, rkey -> "/" <> collection <> "/" <> rkey 152 158 } 153 159 154 160 "at://" <> host <> path
+1 -1
src/possum/nsid.gleam
··· 1 1 //// Namespaced Identifiers (NSIDs) are used to reference Lexicon schemas for 2 - //// records, XRPC endpoints, and more. For example, com.atproto.sync.getRecord. 2 + //// records, XRPC endpoints, and more. For example, `com.atproto.sync.getRecord`. 3 3 //// 4 4 //// The basic structure and semantics of an NSID are a fully-qualified hostname 5 5 //// in reverse domain-name order, followed by an additional name segment.
+2 -2
test/possum_test/at_uri_test.gleam
··· 13 13 "at://foo.com/com.example.foo/123" 14 14 |> at_uri.parse 15 15 16 - let assert Error(at_uri.InvalidHandleAuthority(_)) = 17 - "at://example.com:3000" 16 + let assert Error(at_uri.InvalidDidAuthority(_)) = 17 + "at://did:wibblewobble.com:3000" 18 18 |> at_uri.parse 19 19 20 20 let assert Error(at_uri.InvalidHandleAuthority(_)) =