// 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.importFoundationextensionCottonMethod {/// `{args: {tables, where?, now?}}` → ``PackageExported``.publicstaticlet packageExport = "package.export"/// `{args: {path}}` → ``PackageImported``.publicstaticlet packageImport = "package.import"/// `{args: {kit, dir, media?, now?}}` → ``OpenBundleExported``.publicstaticlet packageExportOpen = "package.exportOpen"}/// What `package.export` wrote: where the file is, and what is in it.publicstructPackageExported: Decodable, Equatable, Sendable {publicstructTable: Decodable, Equatable, Sendable {publicvar table: Stringpublicvar rows: Int }publicvar path: Stringpublicvar fileName: Stringpublicvar sizeBytes: Int64publicvar tables: [Table]/// The keys whose bytes were not on this device, so not in the package.publicvar 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.publicstructOpenBundleExported: Decodable, Equatable, Sendable {publicvar dir: Stringpublicvar files: Intpublicvar missingMedia: [String]/// `books`, `music`, `podcasts` or `films`.publicvar kit: Stringpublicvar exportedAt: String}/// What `package.import` did, per stored table.publicstructPackageImported: Decodable, Equatable, Sendable {publicstructRefused: Decodable, Equatable, Sendable {publicvar table: Stringpublicvar id: Stringpublicvar reason: String }publicvar created: [String: Int]publicvar changed: [String: Int]publicvar unchanged: [String: Int]publicvar refused: [Refused]publicvar mediaWritten: Int/// Every record the import made or changed — what the person brought in.publicvar brought: Int { created.values.reduce(0, +) + changed.values.reduce(0, +) }}/// The two package calls, over a core.publicstructPackages: Sendable {privatelet core: any CottonCallingpublicinit(core: any CottonCalling) {self.core = core }/// Package these tables — ids as tools name them (`todo`, `person`) — and/// answer where the file is.publicfunc export(tables: [String]) asyncthrows -> PackageExported {tryawait 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.publicfunc exportOpen(kit: String, dir: String, media: Bool = true) asyncthrows -> OpenBundleExported {tryawait core.call(CottonMethod.packageExportOpen, ExportOpenRequest(kit: kit, dir: dir, media: media), as: OpenBundleExported.self) }/// Import the package at `path` into this device's database.publicfunc `import`(path: String) asyncthrows -> PackageImported {tryawait core.call(CottonMethod.packageImport, ImportRequest(path: path), as: PackageImported.self) }/// Both read `request["args"]`, the way every `read.*` does.privatestructExportRequest: Encodable {structArgs: Encodable { let tables: [String] }let args: Argsinit(tables: [String]) { args = Args(tables: tables) } }privatestructExportOpenRequest: Encodable {structArgs: Encodable { let kit: String; let dir: String; let media: Bool }let args: Argsinit(kit: String, dir: String, media: Bool) { args = Args(kit: kit, dir: dir, media: media) } }privatestructImportRequest: Encodable {structArgs: Encodable { let path: String }let args: Argsinit(path: String) { args = Args(path: path) } }}