|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// A discrete SARIF severity (`result.level`). Distinct from the continuous ``SarifResult/rank``: |
| 4 | +/// `level` buckets, `rank` (0–100) carries the fine-grained score. `none` is emitted for a pass-like |
| 5 | +/// result; the scorers use `note`/`warning`/`error` only. |
| 6 | +public enum SarifLevel: String, Codable, Sendable, Equatable, CaseIterable { |
| 7 | + case none, note, warning, error |
| 8 | +} |
| 9 | + |
| 10 | +/// A **typed, emit-only** SARIF 2.1.0 log (F17). Companion to the tolerant reader ``SarifLog`` — this |
| 11 | +/// is the *writer*: skillet authors the shape, so there is no unknown-key round-tripping to preserve. |
| 12 | +/// Serialized by ``jsonString()`` with a **camelCase** encoder (the shared `SkilletJSON` is snake_case, |
| 13 | +/// which would emit `start_line`/`rule_id` and break GitHub/SonarQube ingestion). Frozen boundary |
| 14 | +/// format — golden-tested. |
| 15 | +public struct SarifDocument: Encodable, Sendable, Equatable { |
| 16 | + /// The advisory schema pointer. The community mirror (what the repo's reader tests already use); |
| 17 | + /// the OASIS canonical is the authoritative source: |
| 18 | + /// `https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json`. |
| 19 | + public static let schemaURI = "https://json.schemastore.org/sarif-2.1.0.json" |
| 20 | + public static let version = "2.1.0" |
| 21 | + |
| 22 | + public let runs: [SarifRun] |
| 23 | + public init(runs: [SarifRun]) { self.runs = runs } |
| 24 | + |
| 25 | + private enum CodingKeys: String, CodingKey { case schema = "$schema"; case version; case runs } |
| 26 | + public func encode(to encoder: Encoder) throws { |
| 27 | + var c = encoder.container(keyedBy: CodingKeys.self) |
| 28 | + try c.encode(Self.schemaURI, forKey: .schema) |
| 29 | + try c.encode(Self.version, forKey: .version) |
| 30 | + try c.encode(runs, forKey: .runs) |
| 31 | + } |
| 32 | + |
| 33 | + /// Serialize to camelCase SARIF 2.1.0 JSON (no trailing newline). Deterministic (`sortedKeys`), and |
| 34 | + /// **not** via `SkilletJSON` — the default key strategy keeps `startLine`/`ruleId`/`$schema` literal. |
| 35 | + public func jsonString() throws -> String { |
| 36 | + let encoder = JSONEncoder() |
| 37 | + encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] |
| 38 | + return String(decoding: try encoder.encode(self), as: UTF8.self) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +public struct SarifRun: Encodable, Sendable, Equatable { |
| 43 | + public let tool: SarifTool |
| 44 | + public let results: [SarifResult] |
| 45 | + public init(tool: SarifTool, results: [SarifResult]) { self.tool = tool; self.results = results } |
| 46 | +} |
| 47 | + |
| 48 | +public struct SarifTool: Encodable, Sendable, Equatable { |
| 49 | + public let driver: SarifDriver |
| 50 | + public init(driver: SarifDriver) { self.driver = driver } |
| 51 | +} |
| 52 | + |
| 53 | +/// The tool descriptor. `rules` is a **static catalog** — every rule the tool can emit, listed whether |
| 54 | +/// or not it fired this run (the SARIF-idiomatic "advertise your catalog" model). |
| 55 | +public struct SarifDriver: Encodable, Sendable, Equatable { |
| 56 | + public let name: String |
| 57 | + public let version: String |
| 58 | + public let rules: [SarifRule] |
| 59 | + public init(name: String, version: String, rules: [SarifRule]) { |
| 60 | + self.name = name; self.version = version; self.rules = rules |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public struct SarifRule: Encodable, Sendable, Equatable { |
| 65 | + public let id: String |
| 66 | + public let name: String |
| 67 | + public let shortDescription: SarifMessage |
| 68 | + public let properties: [String: JSONValue]? |
| 69 | + public init(id: String, name: String, shortDescription: String, properties: [String: JSONValue]? = nil) { |
| 70 | + self.id = id; self.name = name; self.shortDescription = SarifMessage(text: shortDescription) |
| 71 | + self.properties = (properties?.isEmpty ?? true) ? nil : properties |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public struct SarifResult: Encodable, Sendable, Equatable { |
| 76 | + public let ruleId: String |
| 77 | + public let level: SarifLevel |
| 78 | + public let rank: Double? |
| 79 | + public let message: SarifMessage |
| 80 | + public let locations: [SarifLocation] |
| 81 | + public let properties: [String: JSONValue]? |
| 82 | + |
| 83 | + public init(ruleId: String, level: SarifLevel, rank: Double?, message: String, |
| 84 | + locations: [SarifLocation], properties: [String: JSONValue]?) { |
| 85 | + self.ruleId = ruleId; self.level = level; self.rank = rank |
| 86 | + self.message = SarifMessage(text: message); self.locations = locations |
| 87 | + self.properties = (properties?.isEmpty ?? true) ? nil : properties |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +public struct SarifMessage: Encodable, Sendable, Equatable { |
| 92 | + public let text: String |
| 93 | + public init(text: String) { self.text = text } |
| 94 | +} |
| 95 | + |
| 96 | +public struct SarifLocation: Encodable, Sendable, Equatable { |
| 97 | + public let physicalLocation: SarifPhysicalLocation |
| 98 | + public init(physicalLocation: SarifPhysicalLocation) { self.physicalLocation = physicalLocation } |
| 99 | + /// A file-level location (categorical rules): `artifactLocation` only, no `region`. |
| 100 | + public static func file(_ uri: String) -> SarifLocation { |
| 101 | + SarifLocation(physicalLocation: SarifPhysicalLocation(artifactLocation: SarifArtifactLocation(uri: uri), region: nil)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +public struct SarifPhysicalLocation: Encodable, Sendable, Equatable { |
| 106 | + public let artifactLocation: SarifArtifactLocation |
| 107 | + public let region: SarifRegion? |
| 108 | + public init(artifactLocation: SarifArtifactLocation, region: SarifRegion?) { |
| 109 | + self.artifactLocation = artifactLocation; self.region = region |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +public struct SarifArtifactLocation: Encodable, Sendable, Equatable { |
| 114 | + public let uri: String |
| 115 | + public init(uri: String) { self.uri = uri } |
| 116 | +} |
| 117 | + |
| 118 | +/// A region within an artifact. Line/column are **1-based**; `charOffset`/`charLength` are **0-based**, |
| 119 | +/// per the SARIF spec. All counts are **Unicode scalars** (§4.3). `endColumn` is the column immediately |
| 120 | +/// after the region. |
| 121 | +public struct SarifRegion: Encodable, Sendable, Equatable { |
| 122 | + public let startLine: Int |
| 123 | + public let startColumn: Int |
| 124 | + public let endLine: Int |
| 125 | + public let endColumn: Int |
| 126 | + public let charOffset: Int |
| 127 | + public let charLength: Int |
| 128 | + public init(startLine: Int, startColumn: Int, endLine: Int, endColumn: Int, charOffset: Int, charLength: Int) { |
| 129 | + self.startLine = startLine; self.startColumn = startColumn |
| 130 | + self.endLine = endLine; self.endColumn = endColumn |
| 131 | + self.charOffset = charOffset; self.charLength = charLength |
| 132 | + } |
| 133 | +} |
0 commit comments