Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created January 5, 2023 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/25389bed158679fc4768ffe14f4f08fe to your computer and use it in GitHub Desktop.
Save fitomad/25389bed158679fc4768ffe14f4f08fe to your computer and use it in GitHub Desktop.
final class VoyagerMessageHandler: MessageHandler {
var nextHandler: MessageHandler?
func process(_ message: String) throws {
let regex = #/^VYYR\s{1,4}(?<id>\d{1})\s{1,4}(?<distance>\d{1,}\.\d{1,3})\s{1,}/#
let match = try regex.firstMatch(in: message)
if let match {
print("🛸 La sonda Voyager \(match.id) se encuentra a \(match.distance) millones de kilómetros")
} else if let nextHandler {
try nextHandler.process(message)
} else {
throw MessageError.unknown
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment