gitbay/Snippets/SnippetModels.swift
40 lines · 1180 bytes
1import Foundation
2
3/// One file of a snippet. `content` rides only on `snippet show`; a list
4/// row carries the name and size.
5nonisolated struct SnippetFile: Decodable, Sendable, Hashable, Identifiable {
6 let name: String
7 let size: Int
8 let content: String?
9
10 var id: String { name }
11}
12
13/// One row of `snippet list`, and the whole of `snippet show`.
14nonisolated struct Snippet: Decodable, Sendable, Hashable, Identifiable {
15 let id: String
16 let url: String?
17 let owner: String
18 /// Omitted by the server when empty.
19 let description: String?
20 /// public, unlisted or private.
21 let visibility: String
22 let createdAt: Date
23 let updatedAt: Date
24 let files: [SnippetFile]
25
26 enum CodingKeys: String, CodingKey {
27 case id, url, owner, description, visibility, files
28 case createdAt = "created_at"
29 case updatedAt = "updated_at"
30 }
31
32 /// The web titles a snippet by its description and falls back to
33 /// the id.
34 var title: String {
35 if let description, !description.isEmpty { return description }
36 return id
37 }
38
39 var fileNames: String { files.map(\.name).joined(separator: ", ") }
40}