SwiftLint/Source/SwiftLintFramework/Models/RuleStorage.swift

34 lines
1.3 KiB
Swift

import Dispatch
/// A storage mechanism for aggregating the results of `CollectingRule`s.
public class RuleStorage {
private var storage: [ObjectIdentifier: [SwiftLintFile: Any]]
private let access = DispatchQueue(label: "io.realm.swiftlint.ruleStorageAccess", attributes: .concurrent)
/// Creates a `RuleStorage` with no initial stored data.
public init() {
storage = [:]
}
/// Collects file info for a given rule into the storage.s
///
/// - parameter info: The file information to store.
/// - parameter file: The file for which this information pertains to.
/// - parameter rule: The SwiftLint rule that generated this info.
func collect<R: CollectingRule>(info: R.FileInfo, for file: SwiftLintFile, in rule: R) {
let key = ObjectIdentifier(R.self)
access.sync(flags: .barrier) {
storage[key, default: [:]][file] = info
}
}
/// Retrieves all file information for a given rule that was collected via `collect(...)`.
///
/// - parameter rule: The rule whose collected information should be retrieved.
func collectedInfo<R: CollectingRule>(for rule: R) -> [SwiftLintFile: R.FileInfo]? {
return access.sync {
storage[ObjectIdentifier(R.self)] as? [SwiftLintFile: R.FileInfo]
}
}
}