diff --git a/Sources/X509/Verifier/AllOfPolicies.swift b/Sources/X509/Verifier/AllOfPolicies.swift index ea396a2..a055113 100644 --- a/Sources/X509/Verifier/AllOfPolicies.swift +++ b/Sources/X509/Verifier/AllOfPolicies.swift @@ -36,6 +36,11 @@ public struct AllOfPolicies: VerifierPolicy { @usableFromInline var policy: Policy + @inlinable + public init(@PolicyBuilder policy: () throws -> Policy) throws { + self.policy = try policy() + } + @inlinable public init(@PolicyBuilder policy: () -> Policy) { self.policy = policy() diff --git a/Sources/X509/Verifier/OneOfPolicies.swift b/Sources/X509/Verifier/OneOfPolicies.swift index a5b1c72..4ea04f4 100644 --- a/Sources/X509/Verifier/OneOfPolicies.swift +++ b/Sources/X509/Verifier/OneOfPolicies.swift @@ -176,6 +176,11 @@ public struct OneOfPolicies: VerifierPolicy { @usableFromInline var policy: Policy + @inlinable + public init(@OneOfPolicyBuilder policy: () throws -> Policy) throws { + self.policy = try policy() + } + @inlinable public init(@OneOfPolicyBuilder policy: () -> Policy) { self.policy = policy() diff --git a/Tests/X509Tests/PolicyBuilderTests.swift b/Tests/X509Tests/PolicyBuilderTests.swift index 45047d1..4142f34 100644 --- a/Tests/X509Tests/PolicyBuilderTests.swift +++ b/Tests/X509Tests/PolicyBuilderTests.swift @@ -576,4 +576,52 @@ final class PolicyBuilderTests: XCTestCase { } } } + + func testAllOfPoliciesThrowing() { + // Creating a AllOfPolicies which throws an error inside will itself throw + struct TestError: Error {} + func throwingPolicyBuilder() throws -> Policy { + throw TestError() + } + + XCTAssertThrowsError( + try AllOfPolicies { + try throwingPolicyBuilder() + } + ) { error in + XCTAssertTrue(error is TestError) + } + } + + func testAllOfPoliciesNonThrowing() { + // creating a AllOfPolicies with a non-throwing closure can't throw + // This is tested at compile time (lack of `try` keyword) + _ = AllOfPolicies { + Policy(result: .meetsPolicy) + } + } + + func testOneOfPoliciesThrowing() { + // Creating a OneOfPolicies which throws an error inside will itself throw + struct TestError: Error {} + func throwingPolicyBuilder() throws -> Policy { + throw TestError() + } + + XCTAssertThrowsError( + try OneOfPolicies { + try throwingPolicyBuilder() + } + ) { error in + XCTAssertTrue(error is TestError) + } + } + + func testOneOfPoliciesNonThrowing() { + // creating a OneOfPolicies with a non-throwing closure can't throw + // This is tested at compile time (lack of `try` keyword) + _ = OneOfPolicies { + Policy(result: .meetsPolicy) + } + } }