File: AvailabilityParser.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (82 lines) | stat: -rw-r--r-- 4,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See https://swift.org/LICENSE.txt for license information
 See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Foundation
import SymbolKit

/// A type that parses symbol-graph availability and provides convenient access to specific information.
struct AvailabilityParser {
    /// The availability to parse.
    let availability: SymbolGraph.Symbol.Availability
    /// Creates a new availability parser for a given symbol-graph availability.
    /// - Parameter availability: The availability to parse.
    init(_ availability: SymbolGraph.Symbol.Availability) {
        self.availability = availability
    }
    
    /// Determines whether the symbol is deprecated, either for a given platform or for all platforms.
    ///
    /// - Parameter platform: The platform to check. Pass `nil` to check if the symbol is deprecated on all platforms.
    /// - Returns: Whether or not the symbol is deprecated.
    func isDeprecated(platform: String? = nil) -> Bool {
        guard !availability.availability.isEmpty else { return false }
        
        // Check if a specific platform is deprecated
        if let platform {
            return availability.availability.contains(where: { return $0.domain?.rawValue == platform && ( $0.isUnconditionallyDeprecated || $0.deprecatedVersion != nil ) })
        }
        
        // Check if there's a "universal deprecation" in the listing
        if availability.availability.contains(where: { $0.domain == nil && ($0.isUnconditionallyDeprecated || $0.isUnconditionallyUnavailable) }) {
            return true
        }
        
        // Check if the symbol is unconditionally deprecated
        let isUnconditionallyDeprecated = availability.availability
            .allSatisfy { $0.isUnconditionallyDeprecated || $0.isUnconditionallyUnavailable || $0.deprecatedVersion != nil }
            // If a symbol is unavailable on all known platforms, it should not be marked unconditionally deprecated.
            && availability.availability.contains(where: { $0.isUnconditionallyDeprecated || $0.deprecatedVersion != nil })
        return isUnconditionallyDeprecated
    }
    
    /// Determines a symbol's deprecation message that either applies to a given platform or that applies to all platforms.
    ///
    /// - Parameter platform: The platform for which to determine the deprecation message, or `nil` for all platforms.
    /// - Returns: The deprecation message for this platform, or `nil` if the symbol is not deprecated or doesn't have a deprecation message.
    func deprecationMessage(platform: String? = nil) -> String? {
        guard !availability.availability.isEmpty else { return nil }
        
        if let platform {
            return availability.availability.mapFirst {
                guard $0.domain?.rawValue == platform && ( $0.isUnconditionallyDeprecated || $0.deprecatedVersion != nil ) else {
                    return nil
                }
                return $0.message
            }
        }
        
        // Check if there's a "universal deprecation" in the listing
        if let message = availability.availability.mapFirst(where: { item -> String? in
            guard item.domain == nil && (item.isUnconditionallyDeprecated || item.isUnconditionallyUnavailable) else { return nil }
            return item.message
        }) {
            return message
        }
        
        if availability.availability.allSatisfy({ $0.isUnconditionallyDeprecated || $0.isUnconditionallyUnavailable || $0.deprecatedVersion != nil }) {
            return availability.availability.mapFirst { item -> String? in
                guard item.isUnconditionallyDeprecated || (item.deprecatedVersion != nil) else { return nil }
                return item.message
            }
        }
        
        return nil
    }
}