Skip to content

Commit

Permalink
Added check methods for UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 29, 2023
1 parent 2b3a2bd commit cefde3c
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/Bytes/ByteIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ extension IteratorProtocol where Element == Byte {
_ byte: UInt8
) throws {
let value = next()
if value != byte {
throw BytesError.checkedSequenceNotFound
}

guard value == byte
else { throw BytesError.checkedSequenceNotFound }
}

/// Advances by the specified bytes if found, or throws if the next bytes in the iterator do not match.
Expand Down Expand Up @@ -262,9 +262,9 @@ extension IteratorProtocol where Element == Byte {
_ byte: UInt8
) throws -> Bool {
guard let value = next() else { return false }
if value != byte {
throw BytesError.checkedSequenceNotFound
}

guard value == byte
else { throw BytesError.checkedSequenceNotFound }

return true
}
Expand Down
136 changes: 136 additions & 0 deletions Sources/Bytes/UUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,74 @@ extension IteratorProtocol where Element == Byte {
public mutating func nextIfPresent(string type: UUID.Type) throws -> UUID? {
try nextIfPresent(Bytes.self, count: MemoryLayout<UUIDTextualBytes>.size).map { try UUID(stringBytes: $0) }
}

/// Advances by the specified binary UUID if found, or throws if the next bytes in the iterator do not match.
///
/// Use this method when you expect a binary UUID to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
public mutating func check(
_ uuid: UUID
) throws {
try check(uuid.bytes)
}

/// Advances by the specified UUID String if found, or throws if the next bytes in the iterator do not match.
///
/// Use this method when you expect a UUID String to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
public mutating func check(
string uuid: UUID
) throws {
let value = try next(string: UUID.self)

guard value == uuid
else { throw BytesError.checkedSequenceNotFound }
}

/// Advances by the specified binary UUID if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended.
///
/// Use this method when you expect a binary UUID to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Returns: `true` if the string was found, or `false` if the sequence finished.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
@discardableResult
public mutating func checkIfPresent(
_ uuid: UUID
) throws -> Bool {
try checkIfPresent(uuid.bytes)
}

/// Advances by the specified UUID String if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended.
///
/// Use this method when you expect a UUID String to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Returns: `true` if the string was found, or `false` if the sequence finished.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
@discardableResult
public mutating func checkIfPresent(
string uuid: UUID
) throws -> Bool {
guard let value = try nextIfPresent(string: UUID.self) else { return false }

guard value == uuid
else { throw BytesError.checkedSequenceNotFound }

return true
}
}


Expand Down Expand Up @@ -220,6 +288,74 @@ extension AsyncIteratorProtocol where Element == Byte {
public mutating func nextIfPresent(string type: UUID.Type) async throws -> UUID? {
try await nextIfPresent(Bytes.self, count: MemoryLayout<UUIDTextualBytes>.size).map { try UUID(stringBytes: $0) }
}

/// Asynchronously advances by the specified binary UUID if found, or throws if the next bytes in the iterator do not match.
///
/// Use this method when you expect a binary UUID to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
public mutating func check(
_ uuid: UUID
) async throws {
try await check(uuid.bytes)
}

/// Asynchronously advances by the specified UUID String if found, or throws if the next bytes in the iterator do not match.
///
/// Use this method when you expect a UUID String to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
public mutating func check(
string uuid: UUID
) async throws {
let value = try await next(string: UUID.self)

guard value == uuid
else { throw BytesError.checkedSequenceNotFound }
}

/// Asynchronously advances by the specified binary UUID if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended.
///
/// Use this method when you expect a binary UUID to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Returns: `true` if the string was found, or `false` if the sequence finished.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
@discardableResult
public mutating func checkIfPresent(
_ uuid: UUID
) async throws -> Bool {
try await checkIfPresent(uuid.bytes)
}

/// Asynchronously advances by the specified UUID String if found, throws if the next bytes in the iterator do not match, or returns false if the sequence ended.
///
/// Use this method when you expect a UUID String to be next in the sequence, and it would be an error if something else were encountered.
///
/// **Learn More:** [Integration with AsyncSequenceReader](https://github.com/mochidev/AsyncSequenceReader#integration-with-bytes)
/// - Parameter uuid: The UUID to check for.
/// - Returns: `true` if the string was found, or `false` if the sequence finished.
/// - Throws: ``BytesError/checkedSequenceNotFound`` if the string could not be identified.
@inlinable
@discardableResult
public mutating func checkIfPresent(
string uuid: UUID
) async throws -> Bool {
guard let value = try await nextIfPresent(string: UUID.self) else { return false }

guard value == uuid
else { throw BytesError.checkedSequenceNotFound }

return true
}
}

#endif

0 comments on commit cefde3c

Please sign in to comment.