Cotton code
195 lines · 8.6 KB · SwiftRaw
// What a capture's chips can actually be written to — the two decisions, and// the two holes they name.//// its Rust twin `crates/cotton-core/src/rules/capture_links.rs`// (243 lines), whose comments the sentences below are taken from. The Rust is// the live implementation and the server's filer runs it; **this is a re-statement,// not a call**, and that is the one thing about this file worth explaining.//// The reason it cannot be a call: the ABI is six C functions and a capability is// a METHOD NAME inside `cotton_call` (see `Boundary.swift`'s header). There is// no `engine.*` method — the boundary answers reads, writes and the join, and a// pure decision about which table a kind may be written to is none of those. So// asking the core would mean inventing a method name the crate does not answer,// which is exactly the `unknown_method` the boundary rule forbids. What CAN be// borrowed from Rust is the gate rather than the code: both `switch`es below are// total over `RouteTargetKind` with no `default`, so a kind added to the schema// fails the BUILD here exactly as it fails `match` there. The TypeScript needed a// test for that; Swift and Rust need only the compiler.//// TWO link tables, and NEITHER covers every kind a capture can become:////   • `cotton_project_links` accepts `journal` but NOT `person_note`.//   • `cotton_links` accepts `person_note` but NOT `journal`.//// Both are DB CHECK constraints, and widening either is a migration. So the two// holes are named here, once, with the decision written down — because a kind// missing from a registry is silently invisible, and a silent fallback is the// thing this repo forbids outright.//// Both maps refuse `mood` (v26 — a mood is about the PERSON, so neither table has// anywhere honest to put it). Beyond that refusal the only edge anyone declines// is a SINGLE one: a person_note's own person, which its row already states.import Foundation/// Every kind a capture can be filed as.////// `cotton_core::constants::RouteTargetKind`, said again — nine cases and no/// tenth. It is its own enum rather than a slice of ``RecordKind`` because the/// point of it is the EXHAUSTIVENESS: the two decisions below switch over this/// and nothing else, so a tenth kind is a compile error in two places rather/// than a chip that quietly stops being written.public enum RouteTargetKind: String, CaseIterable, Sendable, Equatable {    case todo    case event    case note    case journal    case location    case page    case transaction    case personNote = "person_note"    case mood}/// Which id a project link hangs on: the record itself, or the person behind it.public enum LinkIdFrom: Equatable, Sendable {    case record    case personId}/// Where a project link points, or the written reason there is none.public enum ProjectLinkDecision: Equatable, Sendable {    case link(kind: RecordKind, idFrom: LinkIdFrom)    /// A refusal written as a sentence rather than a missing entry, which is the    /// whole point.    case skip(String)}/// Whether a record earns a person edge, and as what.public enum PersonLinkDecision: Equatable, Sendable {    case link(RecordKind)    case skip(String)}/// A link row: what kind, and which id it hangs on.////// The kind is a ``RecordKind`` for both tables rather than two narrower enums,/// because that type already exists, is already total, and its raw values are/// already the wire strings both tables store. What NARROWS it to each table's/// legal set is the two decisions below — which is exactly how the Rust does it/// too: `ProjectLinkKind` and `LinkKind` are the outputs of the two `match`es,/// never a thing a caller picks.public struct LinkRef: Equatable, Sendable {    public var kind: RecordKind    public var id: String    public init(kind: RecordKind, id: String) {        self.kind = kind        self.id = id    }}/// The project-link decision for every kind a capture can become.public func projectLinkFor(_ kind: RouteTargetKind) -> ProjectLinkDecision {    switch kind {    case .todo: return .link(kind: .todo, idFrom: .record)    case .event: return .link(kind: .event, idFrom: .record)    case .note: return .link(kind: .note, idFrom: .record)    case .journal: return .link(kind: .journal, idFrom: .record)    case .location: return .link(kind: .location, idFrom: .record)    case .page: return .link(kind: .page, idFrom: .record)    case .transaction: return .link(kind: .transaction, idFrom: .record)    // `person_note` is not a project-link kind and never will be: context about    // a human belongs to the human, so the membership lands on the PERSON.    case .personNote: return .link(kind: .person, idFrom: .personId)    // The FIRST refusal this decision has ever carried, and the reason it is a    // `skip` case rather than a missing branch: a mood is about the person, not    // about a piece of work. `#project` on "feeling low" would file how someone    // felt into a work container — a claim about the mood nobody made.    case .mood:        return .skip(            "a mood is about the person, not a piece of work — filing how someone felt into a project would claim a connection nobody stated"        )    }}/// The person-edge decision for every kind a capture can become.public func personLinkFor(_ kind: RouteTargetKind) -> PersonLinkDecision {    switch kind {    case .todo: return .link(.todo)    case .event: return .link(.event)    case .note: return .link(.note)    // A journal entry IS a notes row — same table, same row id, split only by    // the row's own `kind` column. `cotton_links` has no `journal` kind, so the    // edge is written as `note` against that same id. Nothing is dropped, and no    // CHECK is widened.    case .journal: return .link(.note)    case .location: return .link(.location)    case .page: return .link(.page)    case .transaction: return .link(.transaction)    // A person_note's own person is already named by its own row — but a note    // written on Ravi's page can mention MEERA, and `@Meera` there is a real fact    // about a real person that nothing else records. The edge is legal and it is    // written as itself. What every writer must skip is the SELF edge, below.    case .personNote: return .link(.personNote)    case .mood:        return .skip(            "a mood is the owner's own reading of their own moment — @Ravi in 'rough call with Ravi' names who was there, not who the feeling belongs to, and a person edge would file the owner's mood on Ravi's page"        )    }}/// Does writing person edges for this kind REQUIRE knowing the record's own/// person first?////// True for a `person_note`, and the reason is ``isSelfPersonEdge``'s blind/// spot: with an unknown own-person it answers false for every person, so a/// writer that does not know it would let through exactly the self edge the rule/// exists to skip. A writer that cannot find that person therefore writes NO/// person edge for the record.public func personEdgeNeedsOwnPerson(_ kind: RouteTargetKind) -> Bool {    kind == .personNote}/// Is this the edge a person_note's own row already states?////// The one edge no writer may write. Kept here, beside the decision that makes/// the kind writable at all, so a third writer cannot appear without meeting the/// rule — the alternative is each writer re-deriving "except its own person", and/// one of them getting it wrong.public func isSelfPersonEdge(    _ kind: RouteTargetKind,    _ personId: String,    _ notesOwnPersonId: String?) -> Bool {    kind == .personNote && notesOwnPersonId == personId}/// The project-link ref for a created record, or `nil` when there is none to/// write.public func projectLinkRefFor(    _ kind: RouteTargetKind,    _ recordId: String,    _ personId: String?) -> LinkRef? {    switch projectLinkFor(kind) {    case .skip:        return nil    case .link(let linkKind, .personId):        // No person means no row to hang the membership on. Answering `nil` is        // not a silent fallback: the alternative is an INSERT that fails the        // table's own CHECK at write time, which is a worse place to find out.        return personId.map { LinkRef(kind: linkKind, id: $0) }    case .link(let linkKind, .record):        return LinkRef(kind: linkKind, id: recordId)    }}/// The person-link ref for a created record, or `nil` when there is none.public func personLinkRefFor(_ kind: RouteTargetKind, _ recordId: String) -> LinkRef? {    switch personLinkFor(kind) {    case .skip: return nil    case .link(let linkKind): return LinkRef(kind: linkKind, id: recordId)    }}