1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Foundation
/// Rich text annotation (app.bsky.richtext.facet)
struct Facet: Codable, Sendable {
let index: ByteSlice
let features: [FacetFeature]
struct ByteSlice: Codable, Sendable {
let byteStart: Int
let byteEnd: Int
}
}
enum FacetFeature: Codable, Sendable {
case mention(did: String)
case link(uri: String)
case tag(tag: String)
enum CodingKeys: String, CodingKey {
case type = "$type"
case did
case uri
case tag
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type)
switch type {
case "app.bsky.richtext.facet#mention":
let did = try container.decode(String.self, forKey: .did)
self = .mention(did: did)
case "app.bsky.richtext.facet#link":
let uri = try container.decode(String.self, forKey: .uri)
self = .link(uri: uri)
case "app.bsky.richtext.facet#tag":
let tag = try container.decode(String.self, forKey: .tag)
self = .tag(tag: tag)
default:
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown facet type: \(type)")
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .mention(did):
try container.encode("app.bsky.richtext.facet#mention", forKey: .type)
try container.encode(did, forKey: .did)
case let .link(uri):
try container.encode("app.bsky.richtext.facet#link", forKey: .type)
try container.encode(uri, forKey: .uri)
case let .tag(tag):
try container.encode("app.bsky.richtext.facet#tag", forKey: .type)
try container.encode(tag, forKey: .tag)
}
}
}