Merge pull request #1774 from matthri/fix-enum-variant-inconsistency

Make enum variants more consistent between exercises
This commit is contained in:
Mo 2024-07-05 13:43:25 +02:00 committed by GitHub
commit 67d8d5848c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 2 deletions

View file

@ -6,6 +6,7 @@ enum Message {
fn main() { fn main() {
println!("{:?}", Message::Quit); println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo); println!("{:?}", Message::Echo);
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move); println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor); println!("{:?}", Message::ChangeColor);
} }

View file

@ -4,6 +4,12 @@ enum Message {
// TODO: Define the different variants used below. // TODO: Define the different variants used below.
} }
#[derive(Debug)]
struct Point {
x: u8,
y: u8,
}
impl Message { impl Message {
fn call(&self) { fn call(&self) {
println!("{self:?}"); println!("{self:?}");
@ -12,7 +18,8 @@ impl Message {
fn main() { fn main() {
let messages = [ let messages = [
Message::Move { x: 10, y: 30 }, Message::Resize { w: 10, h: 30 },
Message::Move(Point { x: 10, y: 15 }),
Message::Echo(String::from("hello world")), Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255), Message::ChangeColor(200, 255, 255),
Message::Quit, Message::Quit,

View file

@ -9,6 +9,8 @@ struct Point {
struct State { struct State {
color: (u8, u8, u8), color: (u8, u8, u8),
width: u8,
height: u8,
position: Point, position: Point,
quit: bool, quit: bool,
message: String, message: String,
@ -27,6 +29,11 @@ impl State {
self.message = s; self.message = s;
} }
fn resize(&mut self, width: u8, height: u8) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) { fn move_position(&mut self, point: Point) {
self.position = point; self.position = point;
} }
@ -50,6 +57,8 @@ mod tests {
fn test_match_message_call() { fn test_match_message_call() {
let mut state = State { let mut state = State {
quit: false, quit: false,
width: 0,
height: 0,
position: Point { x: 0, y: 0 }, position: Point { x: 0, y: 0 },
color: (0, 0, 0), color: (0, 0, 0),
message: String::from("hello world"), message: String::from("hello world"),
@ -57,10 +66,13 @@ mod tests {
state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!"))); state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Resize { w: 10, h: 30 });
state.process(Message::Move(Point { x: 10, y: 15 })); state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit); state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255)); 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.x, 10);
assert_eq!(state.position.y, 15); assert_eq!(state.position.y, 15);
assert!(state.quit); assert!(state.quit);

View file

@ -445,7 +445,7 @@ dir = "08_enums"
test = false test = false
hint = """ hint = """
You can create enumerations that have different variants with different types You can create enumerations that have different variants with different types
such as no data, anonymous structs, a single string, tuples, etc.""" such as anonymous structs, structs, a single string, tuples, no data, etc."""
[[exercises]] [[exercises]]
name = "enums3" name = "enums3"