import Foundation /// What a review session is ABOUT, recorded once per session and printed on every /// PDF page plus the PDF's document info, so an annotated page that comes back /// weeks later still says which lane, build and commit it referred to, who took it /// and when. /// /// Every field is optional in practice (empty string = not recorded); nothing here /// is invented when the user leaves it blank. `note` is free text. public struct ReviewContext: Codable, Sendable, Equatable { /// Hard cap per field: this text ends up on every PDF page, so an unbounded /// paste must not be able to break the layout. public static let maxFieldLength = 120 public var lane: String public var deployedVersion: String public var commit: String public var reviewer: String public var note: String public init( lane: String = "", deployedVersion: String = "", commit: String = "", reviewer: String = "", note: String = "" ) { self.lane = Self.cleaned(lane) self.deployedVersion = Self.cleaned(deployedVersion) self.commit = Self.cleaned(commit) self.reviewer = Self.cleaned(reviewer) self.note = Self.cleaned(note) } public static let empty = ReviewContext() /// The macOS account's own name, used to pre-fill `reviewer` on a fresh session. /// `NSFullUserName()` is empty on a host with no full name set, in which case the /// short login name identifies the same person. public static func currentUser() -> String { let full = NSFullUserName().trimmingCharacters(in: .whitespacesAndNewlines) if !full.isEmpty { return cleaned(full) } return cleaned(NSUserName()) } public var isEmpty: Bool { lane.isEmpty && deployedVersion.isEmpty && commit.isEmpty && reviewer.isEmpty && note.isEmpty } /// "DEV03 · v1.4.2 · a1b2c3d · Ben Hippler" — empty parts are dropped, never /// replaced with a placeholder. public var summaryLine: String { summaryParts.joined(separator: " · ") } /// The individual summary parts in display order, empty ones dropped. public var summaryParts: [String] { var parts: [String] = [] if !lane.isEmpty { parts.append(lane) } if !deployedVersion.isEmpty { parts.append(deployedVersion) } if !commit.isEmpty { parts.append(commit) } if !reviewer.isEmpty { parts.append(reviewer) } return parts } /// JSON kept tolerant on purpose: an older manifest has no context at all, and a /// newer one may add fields. Any unknown key is ignored; every known field is /// optional. public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.init( lane: try container.decodeIfPresent(String.self, forKey: .lane) ?? "", deployedVersion: try container.decodeIfPresent(String.self, forKey: .deployedVersion) ?? "", commit: try container.decodeIfPresent(String.self, forKey: .commit) ?? "", reviewer: try container.decodeIfPresent(String.self, forKey: .reviewer) ?? "", note: try container.decodeIfPresent(String.self, forKey: .note) ?? "" ) } /// Trims, collapses line breaks (the value is drawn on one line) and caps length. static func cleaned(_ raw: String) -> String { let collapsed = raw .replacingOccurrences(of: "\r\n", with: " ") .replacingOccurrences(of: "\n", with: " ") .replacingOccurrences(of: "\r", with: " ") .trimmingCharacters(in: .whitespaces) guard collapsed.count > maxFieldLength else { return collapsed } return String(collapsed.prefix(maxFieldLength)) } }