Created
February 23, 2021 01:12
-
-
Save tanner0101/018242695c96b0ac08ff583e17e0a0e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum FujiMessage { | |
case credentials(username: String, password: String) | |
case didCreatePart(barcode: String) | |
case success(message: String) | |
case error(message: String) | |
} | |
protocol FujiConnection { | |
func send(message: FujiMessage) | |
} | |
class FujiHandler { | |
enum State { | |
case authenticating | |
case ready | |
} | |
var state: State = .authenticating | |
func handle(message: FujiMessage, connection: FujiConnection) { | |
switch self.state { | |
case .authenticating: | |
guard case .credentials(let username, let password) = message else { | |
connection.send(message: .error("invalid message")) | |
return | |
} | |
// do login | |
self.state = .ready | |
case .ready: | |
switch message { | |
case .didCreatePart(let barcode): | |
connection.send(message: .success("did create part! \(barcode)")) | |
default: | |
connection.send(message: .error("invalid message")) | |
return | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment