Cotton code
70 lines · 2.9 KB · SwiftRaw
// Podcasts' subscription list in and out (OPML; the coordinator, 7 Nov 2026).//// Both halves go through `object.act`. `import_opml { file }` takes the OPML// file's TEXT, read here from the file shared or picked and sent whole (as a// Goodreads export's CSV is); the core runs it on the phone, signed in or not// (owner, 12 Nov 2026: a signed-out phone is whole), reading every feed it// names that is not held, at most a hundred a run, into the phone's own copy.// `export_opml {}` asks the object to write the subscribed shows as a// document record the phone takes by sync and hands to the share sheet.// Every word either says is the core's own `said`.import Foundation/// The object's acts a subscription list goes through (`object.act`).public enum PodcastActs {    public static let importOpml = "import_opml"    public static let exportOpml = "export_opml"    /// The tool act on the Shows deck that runs `export_opml` (`tools.act`).    public static let exportOpmlTool = "podcasts_export_opml"}/// What one `import_opml` run answered: the shows it made, how many feeds/// it left for another run, and its words.public struct OpmlImported: Decodable, Equatable, Sendable {    public var added: Int    public var already: Int    /// Feeds the file names past this run's hundred, not yet asked.    public var more: Int    /// The shows this run made, in the file's order.    public var podcasts: [String]    public var said: String    public init(added: Int, already: Int, more: Int, podcasts: [String], said: String) {        self.added = added        self.already = already        self.more = more        self.podcasts = podcasts        self.said = said    }}/// The subscription list through the object.public struct PodcastSubscriptions: Sendable {    private let core: any CottonCalling    public init(core: any CottonCalling) {        self.core = core    }    /// One run of `import_opml` over the OPML file's text.    public func importOpml(_ text: String) async throws -> OpmlImported {        let request: [String: JSONValue] = ["act": .string(PodcastActs.importOpml), "args": .object(["file": .string(text)])]        return try await core.call(CottonMethod.objectAct, request, as: OpmlImported.self)    }    /// The whole list brought in: a run, and another while the last one left    /// feeds unasked and brought some in (a run that brings none in would    /// leave the same ones again). `said` is told after each run, in the    /// object's words; answers every run, the first first.    public func bring(_ text: String, said: (String) -> Void) async throws -> [OpmlImported] {        var runs = [try await importOpml(text)]        said(runs[0].said)        while let last = runs.last, last.more > 0, last.added > 0 {            let run = try await importOpml(text)            said(run.said)            runs.append(run)        }        return runs    }}