A command-line interface (CLI) application that converts SecBase.h OSStatus codes into a Swift enum called OSStatusError.
The resulting OSStatusError
will conform to the Swift protocols: Error
,
RawRepresentable
, CustomStringConvertible
and
CustomDebugStringConvertible
.
This application makes use of Swift's new Regex
capabilities and therefore
is only available on macOS 13.x (Ventura) or later.
The source is provided as a Swift package. Therefore, it can be quickly complied
by simply using swift build -c release
. The resulting application should be
named osstatus-generator
.
After doing that calling osstatus-generator
will look at the default location
for a SecBase.h
file and output the resulting OSStatusError.swift to standard
out.
Provide a path to SecBase.h
or use the default. This application will find all
cases of the OSStatus enum along with their codes and possible description. The
comments that appear after the cases of the OSStatus enum are used to create the
descriptions.
For example, assuming these lines are found in SecBase.h:
CF_ENUM(OSStatus)
{
errSecSuccess = 0, /* No error. */
errSecUnimplemented = -4, /* Function or operation not implemented. */
errSecDiskFull = -34, /* The disk is full. */
errSecDskFull __attribute__((deprecated("use errSecDiskFull"))) = errSecDiskFull,
...
}
Then an OSStatusError.swift
will be generated that looks like the following:
//
// OSStatusError.swift
//
//
// Created by OSStatusGenerator on \(date)
// ⚠️ This file is automatically generated and should not be edited by hand. ⚠️
//
import Security
enum OSStatusError {
/// No error.
case errSecSuccess
/// Function or operation not implemented.
case errSecUnimplemented
/// The disk is full.
case errSecDiskFull
/// Unknown OSStatus to SecBase.h
case unknown(OSStatus)
init(status: OSStatus) {
if let known = OSStatusError(rawValue: status) {
self = known
} else {
self = .unknown(status)
}
}
}
// MARK: - RawRepresentable
extension OSStatusError: RawRepresentable {
init?(rawValue: OSStatus) {
switch rawValue {
case 0:
self = .errSecSuccess
case -4:
self = .errSecUnimplemented
case -34:
self = .errSecDiskFull
default:
self = .unknown(rawValue)
}
}
var rawValue: OSStatus {
switch self {
case .errSecSuccess:
return 0
case .errSecUnimplemented:
return -4
case .errSecDiskFull:
return -34
case .unknown(let rawValue):
return rawValue
}
}
}
// MARK: - CustomStringConvertible
extension OSStatusError: CustomStringConvertible {
/// A textual representation of this instance.
///
/// A
var description: String {
switch self {
case .errSecSuccess:
return "No error."
case .errSecUnimplemented:
return "Function or operation not implemented."
case .errSecDiskFull:
return "The disk is full."
case .unknown(let rawValue):
return "The code, \(rawValue), is an unknown OSStatus to SecBase.h"
}
}
}
// MARK: - CustomDebugStringConvertible
extension OSStatusError: CustomDebugStringConvertible {
/// A textual representation of this instance.
///
/// A
var debugDescription: String {
switch self {
case .errSecSuccess:
return "No error. <OSStatusError.errSecSuccess: 0>"
case .errSecUnimplemented:
return "Function or operation not implemented. <OSStatusError.errSecUnimplemented: -4>"
case .errSecDiskFull:
return "The disk is full. <OSStatusError.errSecDiskFull: -34>"
case .unknown(let rawValue):
return "Unknown OSStatus to SecBase.h <OSStatusError.unknown: \(rawValue)>"
}
}
}
// MARK: - Error
extension OSStatusError: Error {
var localizedDescription: String {
switch self {
case .errSecSuccess:
return "No error. <OSStatusError.errSecSuccess: 0>"
case .errSecUnimplemented:
return "Function or operation not implemented. <OSStatusError.errSecUnimplemented: -4>"
case .errSecDiskFull:
return "The disk is full. <OSStatusError.errSecDiskFull: -34>"
case .unknown(let rawValue):
return "Unknown OSStatus to SecBase.h <OSStatusError.unknown: \(rawValue)>"
}
}
}