Adjust enums exercises

This commit is contained in:
mo8it 2024-07-05 14:11:03 +02:00
parent e6f6d26d13
commit 584164a6ff
6 changed files with 109 additions and 71 deletions

View file

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

View file

@ -1,13 +1,14 @@
#[allow(dead_code)] #![allow(dead_code)]
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
}
#[derive(Debug)] #[derive(Debug)]
struct Point { struct Point {
x: u8, x: u64,
y: u8, y: u64,
}
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
} }
impl Message { impl Message {
@ -18,7 +19,10 @@ impl Message {
fn main() { fn main() {
let messages = [ let messages = [
Message::Resize { w: 10, h: 30 }, Message::Resize {
width: 10,
height: 30,
},
Message::Move(Point { x: 10, y: 15 }), 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),

View file

@ -1,35 +1,23 @@
struct Point {
x: u64,
y: u64,
}
enum Message { enum Message {
// TODO: Implement the message variant types based on their usage below. // TODO: Implement the message variant types based on their usage below.
} }
struct Point {
x: u8,
y: u8,
}
struct State { struct State {
color: (u8, u8, u8), width: u64,
width: u8, height: u64,
height: u8,
position: Point, position: Point,
quit: bool,
message: String, message: String,
color: (u8, u8, u8),
quit: bool,
} }
impl State { impl State {
fn change_color(&mut self, color: (u8, u8, u8)) { fn resize(&mut self, width: u64, height: u64) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn resize(&mut self, width: u8, height: u8) {
self.width = width; self.width = width;
self.height = height; self.height = height;
} }
@ -38,6 +26,18 @@ impl State {
self.position = point; self.position = point;
} }
fn echo(&mut self, s: String) {
self.message = s;
}
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) { fn process(&mut self, message: Message) {
// TODO: Create a match expression to process the different message variants. // TODO: Create a match expression to process the different message variants.
// Remember: When passing a tuple as a function argument, you'll need extra parentheses: // Remember: When passing a tuple as a function argument, you'll need extra parentheses:
@ -56,26 +56,29 @@ mod tests {
#[test] #[test]
fn test_match_message_call() { fn test_match_message_call() {
let mut state = State { let mut state = State {
quit: false,
width: 0, width: 0,
height: 0, height: 0,
position: Point { x: 0, y: 0 }, position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"), message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
}; };
state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::Resize {
state.process(Message::Echo(String::from("Hello world!"))); width: 10,
state.process(Message::Resize { w: 10, h: 30 }); height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 })); 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); state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.width, 10); assert_eq!(state.width, 10);
assert_eq!(state.height, 30); 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_eq!(state.message, "Hello world!"); assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
} }
} }

View file

@ -1,14 +1,16 @@
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
Quit, Resize,
Echo,
Move, Move,
Echo,
ChangeColor, ChangeColor,
Quit,
} }
fn main() { fn main() {
println!("{:?}", Message::Quit); println!("{:?}", Message::Resize);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move); println!("{:?}", Message::Move);
println!("{:?}", Message::Echo);
println!("{:?}", Message::ChangeColor); println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Quit);
} }

View file

@ -1,7 +1,15 @@
#[allow(dead_code)] #![allow(dead_code)]
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
Move { x: i64, y: i64 }, Resize { width: u64, height: u64 },
Move(Point),
Echo(String), Echo(String),
ChangeColor(u8, u8, u8), ChangeColor(u8, u8, u8),
Quit, Quit,
@ -15,7 +23,11 @@ impl Message {
fn main() { fn main() {
let messages = [ let messages = [
Message::Move { x: 10, y: 30 }, Message::Resize {
width: 10,
height: 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

@ -1,23 +1,39 @@
struct Point {
x: u64,
y: u64,
}
enum Message { enum Message {
ChangeColor(u8, u8, u8), Resize { width: u64, height: u64 },
Echo(String),
Move(Point), Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit, Quit,
} }
struct Point {
x: u8,
y: u8,
}
struct State { struct State {
color: (u8, u8, u8), width: u64,
height: u64,
position: Point, position: Point,
quit: bool,
message: String, message: String,
color: (u8, u8, u8),
quit: bool,
} }
impl State { impl State {
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn change_color(&mut self, color: (u8, u8, u8)) { fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color; self.color = color;
} }
@ -26,19 +42,12 @@ impl State {
self.quit = true; self.quit = true;
} }
fn echo(&mut self, s: String) {
self.message = s;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn process(&mut self, message: Message) { fn process(&mut self, message: Message) {
match message { match message {
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)), Message::Resize { width, height } => self.resize(width, height),
Message::Echo(s) => self.echo(s),
Message::Move(point) => self.move_position(point), 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(), Message::Quit => self.quit(),
} }
} }
@ -55,21 +64,29 @@ mod tests {
#[test] #[test]
fn test_match_message_call() { fn test_match_message_call() {
let mut state = State { let mut state = State {
quit: false, width: 0,
height: 0,
position: Point { x: 0, y: 0 }, position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"), message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
}; };
state.process(Message::ChangeColor(255, 0, 255)); state.process(Message::Resize {
state.process(Message::Echo(String::from("Hello world!"))); width: 10,
height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 })); 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); 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.x, 10);
assert_eq!(state.position.y, 15); assert_eq!(state.position.y, 15);
assert!(state.quit);
assert_eq!(state.message, "Hello world!"); assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
} }
} }