Skip to content

Commit

Permalink
Move FeatureCodable out of struct
Browse files Browse the repository at this point in the history
This created an error, saying that protocols can't
be placed inside structs.
  • Loading branch information
MasterJ93 committed Jun 2, 2024
1 parent 9480c02 commit 6d5bb4c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

extension AppBskyLexicon.RichText {

/// The main data model definition for a facet.
///
/// - Note: According to the AT Protocol specifications: "Annotation of a sub-string within
Expand All @@ -18,37 +18,37 @@ extension AppBskyLexicon.RichText {
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
public struct Facet: Codable {

/// The range of characters related to the facet.
public let index: ByteSlice

/// The facet's feature type.
public let features: [ATUnion.FacetFeatureUnion]

public init(index: ByteSlice, features: [ATUnion.FacetFeatureUnion]) {
self.index = index
self.features = features
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.index = try container.decode(ByteSlice.self, forKey: .index)
self.features = try container.decode([ATUnion.FacetFeatureUnion].self, forKey: .features)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.index, forKey: .index)
try container.encode(self.features, forKey: .features)
}

enum CodingKeys: String, CodingKey {
case index
case features
}

// Enums
// Represents the ByteSlice
/// The data model definition for the byte slice.
Expand All @@ -63,51 +63,38 @@ extension AppBskyLexicon.RichText {
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
public struct ByteSlice: Codable {

/// The start index of the byte slice.
public let byteStart: Int

/// The end index of the byte slice.
public let byteEnd: Int

public init(byteStart: Int, byteEnd: Int) {
self.byteStart = byteStart
self.byteEnd = byteEnd
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.byteStart = try container.decode(Int.self, forKey: .byteStart)
self.byteEnd = try container.decode(Int.self, forKey: .byteEnd)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.byteStart, forKey: .byteStart)
try container.encode(self.byteEnd, forKey: .byteEnd)
}

enum CodingKeys: String, CodingKey {
case byteStart
case byteEnd
}
}

/// A data model protocol for Features.
///
/// - SeeAlso: This is based on the [`app.bsky.richtext.facet`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
internal protocol FeatureCodable: Codable {

/// The identifier of the lexicon.
///
/// - Warning: The value must not change.
static var type: String { get }
}


/// A data model for the Mention feature definition.
///
/// - Note: According to the AT Protocol specifications: "Facet feature for mention of
Expand All @@ -118,39 +105,39 @@ extension AppBskyLexicon.RichText {
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
public struct Mention: FeatureCodable {

/// The identifier of the lexicon.
///
/// - Warning: The value must not change.
public static var type: String = "app.bsky.richtext.facet#mention"

/// The decentralized identifier (DID) of the feature.
public let did: String

public init(did: String) {
self.did = did
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.did = try container.decode(String.self, forKey: .did)
Mention.type = try container.decode(String.self, forKey: .type)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.did, forKey: .did)
try container.encode(Mention.type, forKey: .type)
}

enum CodingKeys: String, CodingKey {
case type = "$type"
case did
}
}

/// A data model for the Link feature definition.
///
/// - Note: According to the AT Protocol specifications: "Facet feature for a URL. The text URL
Expand All @@ -160,39 +147,39 @@ extension AppBskyLexicon.RichText {
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
public struct Link: FeatureCodable {

/// The identifier of the lexicon.
///
/// - Warning: The value must not change.
public static var type: String = "app.bsky.richtext.facet#link"

/// The URI of the feature.
public let uri: String

public init(uri: String) {
self.uri = uri
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.uri = try container.decode(String.self, forKey: .uri)
Link.type = try container.decode(String.self, forKey: .type)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.uri, forKey: .uri)
try container.encode(Link.type, forKey: .type)
}

enum CodingKeys: String, CodingKey {
case type = "$type"
case uri
}
}

/// A data model for the Tag feature definition.
///
/// - Note: According to the AT Protocol specifications: "Facet feature for a hashtag. The text
Expand All @@ -203,33 +190,33 @@ extension AppBskyLexicon.RichText {
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
public struct Tag: FeatureCodable {

/// The identifier of the lexicon.
///
/// - Warning: The value must not change.
public static var type: String = "app.bsky.richtext.facet#tag"

/// The name of the tag.
public let tag: String

public init(tag: String) {
self.tag = tag
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.tag = try container.decode(String.self, forKey: .tag)
Tag.type = try container.decode(String.self, forKey: .type)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.tag, forKey: .tag)
try container.encode(Tag.type, forKey: .type)
}

enum CodingKeys: String, CodingKey {
case type = "$type"
case tag
Expand Down
13 changes: 13 additions & 0 deletions Sources/ATProtoKit/Utilities/ATFacetParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,16 @@ public class ATFacetParser {
return await facets.facets
}
}

/// A data model protocol for Features.
///
/// - SeeAlso: This is based on the [`app.bsky.richtext.facet`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/richtext/facet.json
internal protocol FeatureCodable: Codable {

/// The identifier of the lexicon.
///
/// - Warning: The value must not change.
static var type: String { get }
}

0 comments on commit 6d5bb4c

Please sign in to comment.