11export PlatformAvailability, UnavailableError
22
3- # Each platform tuple has a symbol representing the constructor, a pretty name for errors,
4- # and a symbol of the function used to check the version for that platform
5- const SUPPORTED_PLATFORMS = [(:macos , " macOS" , :macos_version ), (:darwin , " Darwin" , :darwin_version )]
3+ # Each platform tuple has a symbol representing the constructor, a pretty_name name for errors,
4+ # a symbol of the function used to check the version for that platform, and the function that
5+ # returns whether that statement applies for this device
6+ const SUPPORTED_PLATFORMS = Dict (
7+ :macos => (" macOS" , :macos_version , Sys. isapple),
8+ :darwin => (" Darwin" , :darwin_version , Sys. isapple),
9+ :test => (" Never applicable" , :error , () -> false )
10+ )
611
712# Based off of Clang's `CXPlatformAvailability`
813"""
@@ -21,20 +26,20 @@ struct PlatformAvailability{P}
2126 obsoleted:: Union{Nothing, VersionNumber}
2227 unavailable:: Bool
2328
24- function PlatformAvailability (platform :: Symbol , introduced, deprecated = nothing , obsoleted = nothing , unavailable = false )
25- platform in first . (SUPPORTED_PLATFORMS) || throw (ArgumentError (lazy " `:$platform ` is not a supported platform for `PlatformAvailability`, see `?PlatformAvailability` for more information." ))
26- return new {platform } (introduced, deprecated, obsoleted, unavailable)
29+ function PlatformAvailability (p :: Symbol , introduced, deprecated = nothing , obsoleted = nothing , unavailable = false )
30+ haskey (SUPPORTED_PLATFORMS, p ) || throw (ArgumentError (lazy " `:$p ` is not a supported platform for `PlatformAvailability`, see `?PlatformAvailability` for more information." ))
31+ return new {p } (introduced, deprecated, obsoleted, unavailable)
2732 end
2833end
2934PlatformAvailability (platform; introduced = nothing , deprecated = nothing , obsoleted = nothing , unavailable = false ) =
3035 PlatformAvailability (platform, introduced, deprecated, obsoleted, unavailable)
3136
32- function is_unavailable (f:: Function , avail:: PlatformAvailability )
33- return avail. unavailable ||
34- (! isnothing (avail. obsoleted) && f () >= avail. obsoleted) ||
35- (! isnothing (avail. introduced) && f () < avail. introduced)
37+ function is_available (f:: Function , avail:: PlatformAvailability )
38+ return ! avail. unavailable &&
39+ (isnothing (avail. obsoleted) || f () < avail. obsoleted) &&
40+ (isnothing (avail. introduced) || f () >= avail. introduced)
3641end
37- is_unavailable (avails:: Vector{<:PlatformAvailability} ) = any ( is_unavailable .(avails))
42+ is_available (avails:: Vector{<:PlatformAvailability} ) = all ( is_available .(avails))
3843
3944"""
4045 UnavailableError(symbol::Symbol, minver::VersionNumber)
@@ -59,7 +64,7 @@ function UnavailableError(f::Function, symbol::Symbol, platform::String, avail::
5964 return UnavailableError (symbol, msg)
6065end
6166function UnavailableError (symbol:: Symbol , avails:: Vector{<:PlatformAvailability} )
62- firsterror = findfirst (is_unavailable , avails)
67+ firsterror = findfirst (! is_available , avails)
6368 return UnavailableError (symbol, avails[firsterror])
6469end
6570
@@ -69,11 +74,11 @@ function Base.showerror(io::IO, e::UnavailableError)
6974end
7075
7176# Platform-specific definitions
72- for (name, pretty_name, version_function ) in SUPPORTED_PLATFORMS
77+ for (name, ( pretty_name, ver_func, plat_func) ) in SUPPORTED_PLATFORMS
7378 quotname = Meta. quot (name)
7479 @eval begin
75- is_unavailable (avail:: PlatformAvailability{$quotname} ) = is_unavailable ( $ version_function , avail)
76- UnavailableError (symbol:: Symbol , avail:: PlatformAvailability{$quotname} ) = UnavailableError ($ version_function , symbol, $ pretty_name, avail)
80+ is_available (avail:: PlatformAvailability{$quotname} ) = ! $ plat_func () || is_available ( $ ver_func , avail)
81+ UnavailableError (symbol:: Symbol , avail:: PlatformAvailability{$quotname} ) = UnavailableError ($ ver_func , symbol, $ pretty_name, avail)
7782 end
7883end
7984
0 commit comments