Cotton code
379 lines · 13.2 KB · SwiftRaw
// GENERATED by `cotton-gen` from `cotton-core — vault/payload.rs` — do not edit by hand.// Run `pnpm gen:design`; `gen:design:check` guards drift in CI.//// The 10 shapes a secret's sealed bytes can take.//// Read from `crates/cotton-core/src/vault/payload.rs`, which is where a kind is// declared; a kind is NEVER declared here. The plain columns of the row —// `kind`, `name`, `site`, `username`, `service` — are what a list shows and what// search indexes, and they are deliberately NOT repeated in here: a second copy// of a name inside the sealed bytes would be a second answer to what a secret is// called, and the sealed one is the one nothing can read to check.//// The wire form is FLAT and tagged on `kind`, so `{"kind":"login",// "password":"…"}` is one object rather than a nested one. Swift's synthesized// `Codable` for an enum with associated values writes a nested shape instead,// which is why `init(from:)` and `encode(to:)` are spelled out below.//// An unset optional is ABSENT rather than null, at both ends: Swift's// synthesized coding uses `encodeIfPresent`/`decodeIfPresent` for an optional// property, and the Rust side carries `skip_serializing_if`. Two spellings of// nothing would be two payloads that are the same secret.import Foundation/// What a secret IS — the same string the row's `kind` column holds.public enum SecretKind: String, Sendable, Equatable, CaseIterable, Codable {    case login = "login"    case apiKey = "api_key"    case note = "note"    case card = "card"    case identity = "identity"    case document = "document"    case code = "code"    case wifi = "wifi"    case address = "address"    case file = "file"}/// A `login` secret's payload.public struct SecretLogin: Sendable, Equatable, Codable {    /// The password itself.    public var password: String    /// Anything else worth keeping with it.    public var notes: String?    /// The otpauth secret a one-time code is generated from.    public var totp: String?    /// Other addresses it is used at, one a line.    public var urls: String?    public init(password: String, notes: String? = nil, totp: String? = nil, urls: String? = nil) {        self.password = password        self.notes = notes        self.totp = totp        self.urls = urls    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { password }}/// A `api_key` secret's payload.public struct SecretApiKey: Sendable, Equatable, Codable {    /// The key itself.    public var key: String    /// Anything else worth keeping with it.    public var notes: String?    public init(key: String, notes: String? = nil) {        self.key = key        self.notes = notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { key }}/// A `note` secret's payload.public struct SecretNote: Sendable, Equatable, Codable {    /// The note, whole.    public var body: String    public init(body: String) {        self.body = body    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { body }}/// A `card` secret's payload.public struct SecretCard: Sendable, Equatable, Codable {    /// The card number.    public var number: String    /// The name on the card.    public var holder: String?    /// MM/YY, as the card prints it.    public var expiry: String?    /// The verification digits.    public var cvv: String?    /// Anything else worth keeping with it.    public var notes: String?    public init(number: String, holder: String? = nil, expiry: String? = nil, cvv: String? = nil, notes: String? = nil) {        self.number = number        self.holder = holder        self.expiry = expiry        self.cvv = cvv        self.notes = notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { number }}/// A `identity` secret's payload.public struct SecretIdentity: Sendable, Equatable, Codable {    /// The document's number.    public var number: String    /// Passport, driving licence, tax id, and so on.    public var idType: String?    /// Whose it is.    public var holder: String?    /// The date it was issued.    public var issued: String?    /// The date it runs out.    public var expires: String?    /// Who issued it.    public var country: String?    /// Anything else worth keeping with it.    public var notes: String?    public init(number: String, idType: String? = nil, holder: String? = nil, issued: String? = nil, expires: String? = nil, country: String? = nil, notes: String? = nil) {        self.number = number        self.idType = idType        self.holder = holder        self.issued = issued        self.expires = expires        self.country = country        self.notes = notes    }    /// The JSON keys are the Rust declaration's own spelling.    private enum CodingKeys: String, CodingKey {        case number        case idType = "id_type"        case holder        case issued        case expires        case country        case notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { number }}/// A `document` secret's payload.public struct SecretDocument: Sendable, Equatable, Codable {    /// The password that unlocks a filed document.    public var password: String    public init(password: String) {        self.password = password    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { password }}/// A `code` secret's payload.public struct SecretCode: Sendable, Equatable, Codable {    /// The otpauth address (or bare base32 secret) the one-time code is made from.    public var secret: String    /// Anything else worth keeping with it.    public var notes: String?    public init(secret: String, notes: String? = nil) {        self.secret = secret        self.notes = notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { secret }}/// A `wifi` secret's payload.public struct SecretWifi: Sendable, Equatable, Codable {    /// The network's password.    public var password: String    /// WPA3, WPA2, WEP or none, as the router says.    public var security: String?    /// Anything else worth keeping with it.    public var notes: String?    public init(password: String, security: String? = nil, notes: String? = nil) {        self.password = password        self.security = security        self.notes = notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { password }}/// A `address` secret's payload.public struct SecretAddress: Sendable, Equatable, Codable {    /// The postal address, as it is written on an envelope.    public var address: String    /// The name it is for.    public var fullName: String?    /// An email address that goes with it.    public var email: String?    /// A phone number that goes with it.    public var phone: String?    /// Anything else worth keeping with it.    public var notes: String?    public init(address: String, fullName: String? = nil, email: String? = nil, phone: String? = nil, notes: String? = nil) {        self.address = address        self.fullName = fullName        self.email = email        self.phone = phone        self.notes = notes    }    /// The JSON keys are the Rust declaration's own spelling.    private enum CodingKeys: String, CodingKey {        case address        case fullName = "full_name"        case email        case phone        case notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { address }}/// A `file` secret's payload.public struct SecretFile: Sendable, Equatable, Codable {    /// The file's bytes, base64 (standard, padded).    public var content: String    /// The file's name, as it was chosen or shared.    public var fileName: String?    /// The password that opens a keystore (.jks, .p12).    public var storePassword: String?    /// The key's alias inside a keystore.    public var keyAlias: String?    /// The password of that key, when it differs from the store's.    public var keyPassword: String?    /// Anything else worth keeping with it.    public var notes: String?    public init(content: String, fileName: String? = nil, storePassword: String? = nil, keyAlias: String? = nil, keyPassword: String? = nil, notes: String? = nil) {        self.content = content        self.fileName = fileName        self.storePassword = storePassword        self.keyAlias = keyAlias        self.keyPassword = keyPassword        self.notes = notes    }    /// The JSON keys are the Rust declaration's own spelling.    private enum CodingKeys: String, CodingKey {        case content        case fileName = "file_name"        case storePassword = "store_password"        case keyAlias = "key_alias"        case keyPassword = "key_password"        case notes    }    /// The field a hint is taken from, and the one Copy copies.    public var primaryField: String { content }}/// One secret's payload — everything the person actually has to keep.public enum SecretPayload: Sendable, Equatable {    case login(SecretLogin)    case apiKey(SecretApiKey)    case note(SecretNote)    case card(SecretCard)    case identity(SecretIdentity)    case document(SecretDocument)    case code(SecretCode)    case wifi(SecretWifi)    case address(SecretAddress)    case file(SecretFile)    public var kind: SecretKind {        switch self {        case .login: return .login        case .apiKey: return .apiKey        case .note: return .note        case .card: return .card        case .identity: return .identity        case .document: return .document        case .code: return .code        case .wifi: return .wifi        case .address: return .address        case .file: return .file        }    }    /// The VALUE of the kind's primary field: what a hint is taken from, what a    /// `raw` placement writes, and what Copy copies.    public var primaryField: String {        switch self {        case .login(let payload): return payload.primaryField        case .apiKey(let payload): return payload.primaryField        case .note(let payload): return payload.primaryField        case .card(let payload): return payload.primaryField        case .identity(let payload): return payload.primaryField        case .document(let payload): return payload.primaryField        case .code(let payload): return payload.primaryField        case .wifi(let payload): return payload.primaryField        case .address(let payload): return payload.primaryField        case .file(let payload): return payload.primaryField        }    }    /// The last 4 characters of the primary field — what a list shows    /// INSTEAD of the secret. Characters, not bytes: a value ending outside    /// ASCII must not be cut through the middle of one.    public var hint: String { SecretPayload.hint(of: primaryField) }    public static func hint(of value: String) -> String {        String(value.suffix(4))    }    /// The sealed bytes, once they are open.    public static func decode(_ json: Data) throws -> SecretPayload {        try JSONDecoder().decode(SecretPayload.self, from: json)    }    /// The bytes to seal.    public func encoded() throws -> Data {        try JSONEncoder().encode(self)    }}extension SecretPayload: Codable {    private enum TagKey: String, CodingKey {        case kind    }    public init(from decoder: Decoder) throws {        let tag = try decoder.container(keyedBy: TagKey.self).decode(SecretKind.self, forKey: .kind)        switch tag {        case .login: self = .login(try SecretLogin(from: decoder))        case .apiKey: self = .apiKey(try SecretApiKey(from: decoder))        case .note: self = .note(try SecretNote(from: decoder))        case .card: self = .card(try SecretCard(from: decoder))        case .identity: self = .identity(try SecretIdentity(from: decoder))        case .document: self = .document(try SecretDocument(from: decoder))        case .code: self = .code(try SecretCode(from: decoder))        case .wifi: self = .wifi(try SecretWifi(from: decoder))        case .address: self = .address(try SecretAddress(from: decoder))        case .file: self = .file(try SecretFile(from: decoder))        }    }    public func encode(to encoder: Encoder) throws {        var tag = encoder.container(keyedBy: TagKey.self)        try tag.encode(kind, forKey: .kind)        switch self {        case .login(let payload): try payload.encode(to: encoder)        case .apiKey(let payload): try payload.encode(to: encoder)        case .note(let payload): try payload.encode(to: encoder)        case .card(let payload): try payload.encode(to: encoder)        case .identity(let payload): try payload.encode(to: encoder)        case .document(let payload): try payload.encode(to: encoder)        case .code(let payload): try payload.encode(to: encoder)        case .wifi(let payload): try payload.encode(to: encoder)        case .address(let payload): try payload.encode(to: encoder)        case .file(let payload): try payload.encode(to: encoder)        }    }}