diff options
| author | mo8it <mo8it@proton.me> | 2024-07-05 14:11:03 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-07-05 14:11:03 +0200 |
| commit | 584164a6ffff87d2b10916f25e8172a3e5f1daa1 (patch) | |
| tree | 2bd1fad8c58aec5cec74833cf64a1f4d21884bf0 /solutions/08_enums/enums3.rs | |
| parent | e6f6d26d131e3a3ee89248c1fdaba22d8d1d79e2 (diff) | |
Adjust enums exercises
Diffstat (limited to 'solutions/08_enums/enums3.rs')
| -rw-r--r-- | solutions/08_enums/enums3.rs | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/solutions/08_enums/enums3.rs b/solutions/08_enums/enums3.rs index 8baa25c..8641fbd 100644 --- a/solutions/08_enums/enums3.rs +++ b/solutions/08_enums/enums3.rs @@ -1,44 +1,53 @@ +struct Point { + x: u64, + y: u64, +} + enum Message { - ChangeColor(u8, u8, u8), - Echo(String), + Resize { width: u64, height: u64 }, Move(Point), + Echo(String), + ChangeColor(u8, u8, u8), Quit, } -struct Point { - x: u8, - y: u8, -} - struct State { - color: (u8, u8, u8), + width: u64, + height: u64, position: Point, - quit: bool, message: String, + color: (u8, u8, u8), + quit: bool, } impl State { - fn change_color(&mut self, color: (u8, u8, u8)) { - self.color = color; + fn resize(&mut self, width: u64, height: u64) { + self.width = width; + self.height = height; } - fn quit(&mut self) { - self.quit = true; + fn move_position(&mut self, point: Point) { + self.position = point; } fn echo(&mut self, s: String) { self.message = s; } - fn move_position(&mut self, point: Point) { - self.position = point; + fn change_color(&mut self, color: (u8, u8, u8)) { + self.color = color; + } + + fn quit(&mut self) { + self.quit = true; } fn process(&mut self, message: Message) { match message { - Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), - Message::Echo(s) => self.echo(s), + Message::Resize { width, height } => self.resize(width, height), Message::Move(point) => self.move_position(point), + Message::Echo(s) => self.echo(s), + Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), Message::Quit => self.quit(), } } @@ -55,21 +64,29 @@ mod tests { #[test] fn test_match_message_call() { let mut state = State { - quit: false, + width: 0, + height: 0, position: Point { x: 0, y: 0 }, - color: (0, 0, 0), message: String::from("hello world"), + color: (0, 0, 0), + quit: false, }; - state.process(Message::ChangeColor(255, 0, 255)); - state.process(Message::Echo(String::from("Hello world!"))); + state.process(Message::Resize { + width: 10, + height: 30, + }); state.process(Message::Move(Point { x: 10, y: 15 })); + state.process(Message::Echo(String::from("Hello world!"))); + state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::Quit); - assert_eq!(state.color, (255, 0, 255)); + assert_eq!(state.width, 10); + assert_eq!(state.height, 30); assert_eq!(state.position.x, 10); assert_eq!(state.position.y, 15); - assert!(state.quit); assert_eq!(state.message, "Hello world!"); + assert_eq!(state.color, (255, 0, 255)); + assert!(state.quit); } } |
