Sources/RequestDL/Internals/Sources/Secure Connection/Trust Roots/Internals.TrustRoots.swift
/*
See LICENSE for this package's licensing information.
*/
import Foundation
import NIOSSL
extension Internals {
enum TrustRoots: Sendable, Hashable {
case file(String)
case bytes([UInt8])
case certificates([Internals.Certificate])
// MARK: - Inits
init() {
self = .certificates([])
}
// MARK: - Internal methods
mutating func append(_ certificate: Internals.Certificate) {
switch self {
case .file(let path):
self = .certificates([.init(path, format: .pem), certificate])
case .bytes(let bytes):
self = .certificates([.init(bytes, format: .pem), certificate])
case .certificates(let certificates):
self = .certificates(certificates + [certificate])
}
}
func build() throws -> NIOSSLTrustRoots {
switch self {
case .file(let file):
return .file(file)
case .bytes(let bytes):
return .certificates(try NIOSSLCertificate.fromPEMBytes(bytes))
case .certificates(let certificates):
return .certificates(try certificates.map {
try $0.build()
})
}
}
}
}