// The Cotton package (`.cottonpkg`) across the boundary: `package.export` and // `package.import` (`cotton-ffi/src/phone/package.rs`, lane package). Both take // and answer a PATH, never bytes — a package carries pictures, and a body that // size crossing the FFI as a string is the jetsam kill build 106 paid for. // // `package.exportOpen` lays one kit's open bundle as a folder (the phone zips // it for the share sheet: `LibraryExport`). // // The calls sit behind ONE small type, `Packages`, so the doors that use // them — Settings › Export, and a package arriving through the share sheet or // Settings › Bring in — never spell a method or a request themselves. import Foundation extension CottonMethod { /// `{args: {tables, where?, now?}}` → ``PackageExported``. public static let packageExport = "package.export" /// `{args: {path}}` → ``PackageImported``. public static let packageImport = "package.import" /// `{args: {kit, dir, media?, now?}}` → ``OpenBundleExported``. public static let packageExportOpen = "package.exportOpen" } /// What `package.export` wrote: where the file is, and what is in it. public struct PackageExported: Decodable, Equatable, Sendable { public struct Table: Decodable, Equatable, Sendable { public var table: String public var rows: Int } public var path: String public var fileName: String public var sizeBytes: Int64 public var tables: [Table] /// The keys whose bytes were not on this device, so not in the package. public var missingMedia: [String] } /// What `package.exportOpen` laid: one kit's open bundle (Kodi, Plex, OPDS, /// M3U8, OPML trees) in the folder asked for, how many files it wrote, and /// the media keys whose bytes this phone does not keep, so not in it. public struct OpenBundleExported: Decodable, Equatable, Sendable { public var dir: String public var files: Int public var missingMedia: [String] /// `books`, `music`, `podcasts` or `films`. public var kit: String public var exportedAt: String } /// What `package.import` did, per stored table. public struct PackageImported: Decodable, Equatable, Sendable { public struct Refused: Decodable, Equatable, Sendable { public var table: String public var id: String public var reason: String } public var created: [String: Int] public var changed: [String: Int] public var unchanged: [String: Int] public var refused: [Refused] public var mediaWritten: Int /// Every record the import made or changed — what the person brought in. public var brought: Int { created.values.reduce(0, +) + changed.values.reduce(0, +) } } /// The two package calls, over a core. public struct Packages: Sendable { private let core: any CottonCalling public init(core: any CottonCalling) { self.core = core } /// Package these tables — ids as tools name them (`todo`, `person`) — and /// answer where the file is. public func export(tables: [String]) async throws -> PackageExported { try await core.call(CottonMethod.packageExport, ExportRequest(tables: tables), as: PackageExported.self) } /// Lay the open bundle of `kit` (`books`, `music`, `podcasts`, `films`) /// into `dir`, an empty folder or one not there yet; `media: false` lays /// the records alone. The answer is the folder, never a zip. public func exportOpen(kit: String, dir: String, media: Bool = true) async throws -> OpenBundleExported { try await core.call(CottonMethod.packageExportOpen, ExportOpenRequest(kit: kit, dir: dir, media: media), as: OpenBundleExported.self) } /// Import the package at `path` into this device's database. public func `import`(path: String) async throws -> PackageImported { try await core.call(CottonMethod.packageImport, ImportRequest(path: path), as: PackageImported.self) } /// Both read `request["args"]`, the way every `read.*` does. private struct ExportRequest: Encodable { struct Args: Encodable { let tables: [String] } let args: Args init(tables: [String]) { args = Args(tables: tables) } } private struct ExportOpenRequest: Encodable { struct Args: Encodable { let kit: String; let dir: String; let media: Bool } let args: Args init(kit: String, dir: String, media: Bool) { args = Args(kit: kit, dir: dir, media: media) } } private struct ImportRequest: Encodable { struct Args: Encodable { let path: String } let args: Args init(path: String) { args = Args(path: path) } } }