2024-12-11 10:54:12 +00:00
|
|
|
/// A URL or a user mention link in a rant or comment.
|
2024-12-22 16:24:48 +00:00
|
|
|
public struct Link: Hashable, Sendable {
|
|
|
|
public enum Kind: String, Sendable {
|
|
|
|
case url = "url"
|
|
|
|
case userMention = "mention"
|
2024-12-11 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public let kind: Kind
|
|
|
|
|
|
|
|
/// The full URL.
|
|
|
|
public let url: String
|
|
|
|
|
2024-12-23 15:25:34 +00:00
|
|
|
/// No idea what this is and what it is supposed to be used for.
|
|
|
|
public let shortURL: String?
|
2024-12-11 10:54:12 +00:00
|
|
|
|
|
|
|
/// The url as it is visible in the text of the rant or comment.
|
|
|
|
public let title: String
|
|
|
|
|
|
|
|
/// The starting position of the link in the overall text of the rant or comment.
|
|
|
|
/// - Important: The devRant API returns offsets for links in byte offsets and not in normalized character offsets. Please take this into account when using these offsets.
|
|
|
|
public let start: Int?
|
|
|
|
|
|
|
|
/// The ending position of the link in the overall text of the rant or comment.
|
|
|
|
/// - Important: The devRant API returns offsets for links in byte offsets and not in normalized character offsets. Please take this into account when using these offsets.
|
|
|
|
public let end: Int?
|
|
|
|
|
|
|
|
public init(kind: Link.Kind, url: String, shortURL: String?, title: String, start: Int?, end: Int?) {
|
|
|
|
self.kind = kind
|
|
|
|
self.url = url
|
|
|
|
self.shortURL = shortURL
|
|
|
|
self.title = title
|
|
|
|
self.start = start
|
|
|
|
self.end = end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Link {
|
|
|
|
struct CodingData: Codable {
|
|
|
|
let type: String
|
|
|
|
let url: String
|
|
|
|
let short_url: String
|
|
|
|
let title: String
|
|
|
|
let start: Int?
|
|
|
|
let end: Int?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Link.CodingData {
|
|
|
|
var decoded: Link {
|
|
|
|
.init(
|
2024-12-22 16:24:48 +00:00
|
|
|
kind: .init(rawValue: type) ?? .url,
|
2024-12-11 10:54:12 +00:00
|
|
|
url: url,
|
|
|
|
shortURL: short_url,
|
|
|
|
title: title,
|
|
|
|
start: start,
|
|
|
|
end: end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|