Files
SwiftLint/Source/SwiftLintCore/Models/SeverityConfiguration.swift
T
Danny MöschandGitHub 3f039f26d5 Connect configs with their referencing rules to have some context in error logging (#5017)
With the binding of configurations to their associated rule types
"unknown configuration" errors can be made more specific mentioning
also the rule's identifier in the printed message.
2023-05-19 20:58:24 +02:00

35 lines
1.3 KiB
Swift

/// A rule configuration that allows specifying the desired severity level for violations.
public struct SeverityConfiguration<Parent: Rule>: SeverityBasedRuleConfiguration, Equatable {
/// Configuration with a warning severity.
public static var error: Self { Self(.error) }
/// Configuration with an error severity.
public static var warning: Self { Self(.warning) }
public var consoleDescription: String {
return severity.rawValue
}
var severity: ViolationSeverity
public var severityConfiguration: SeverityConfiguration {
self
}
/// Create a `SeverityConfiguration` with the specified severity.
///
/// - parameter severity: The severity that should be used when emitting violations.
public init(_ severity: ViolationSeverity) {
self.severity = severity
}
public mutating func apply(configuration: Any) throws {
let configString = configuration as? String
let configDict = configuration as? [String: Any]
guard let severityString: String = configString ?? configDict?["severity"] as? String,
let severity = ViolationSeverity(rawValue: severityString.lowercased()) else {
throw Issue.unknownConfiguration(ruleID: Parent.description.identifier)
}
self.severity = severity
}
}