Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to lazily parse a SET from a ASN1Node #32

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Sources/SwiftASN1/ASN1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,30 @@ extension DER {
/// - rootNode: The ``ASN1Node`` to parse
/// - returns: An array of elements representing the elements in the sequence.
@inlinable
public static func set<T: DERParseable>(of: T.Type = T.self, identifier: ASN1Identifier, rootNode: ASN1Node) throws -> [T] {
public static func set<T: DERParseable>(of type: T.Type = T.self, identifier: ASN1Identifier, rootNode: ASN1Node) throws -> [T] {
try self.lazySet(of: type, identifier: identifier, rootNode: rootNode).map { try $0.get() }
}

/// Parse the node as an ASN.1 SET OF lazily.
///
/// Constructs a Sequence of `T` elements that will be lazily parsed from the set.
///
/// - parameters:
/// - of: An optional parameter to express the type to decode.
/// - identifier: The ``ASN1Identifier`` that the SET OF is expected to have.
/// - rootNode: The ``ASN1Node`` to parse
/// - returns: A `Sequence` of elements representing the `Result` of parsing the elements in the sequence.
@inlinable
public static func lazySet<T: DERParseable>(of: T.Type = T.self, identifier: ASN1Identifier, rootNode: ASN1Node) throws -> some Sequence<Result<T, Error>> {
guard rootNode.identifier == identifier, case .constructed(let nodes) = rootNode.content else {
throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}

guard nodes.isOrderedAccordingToSetOfSemantics() else {
throw ASN1Error.invalidASN1Object(reason: "SET OF fields are not lexicographically ordered")
}

return try nodes.map { try T(derEncoded: $0) }
return nodes.lazy.map { node in Result { try T(derEncoded: node) } }
}
}

Expand Down