rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

enums3.rs (2367B)


      1 struct Point {
      2     x: u64,
      3     y: u64,
      4 }
      5 
      6 enum Message {
      7     Resize { width: u64, height: u64 },
      8     Move(Point),
      9     Echo(String),
     10     ChangeColor(u8, u8, u8),
     11     Quit,
     12 }
     13 
     14 struct State {
     15     width: u64,
     16     height: u64,
     17     position: Point,
     18     message: String,
     19     // RGB color composed of red, green and blue.
     20     color: (u8, u8, u8),
     21     quit: bool,
     22 }
     23 
     24 impl State {
     25     fn resize(&mut self, width: u64, height: u64) {
     26         self.width = width;
     27         self.height = height;
     28     }
     29 
     30     fn move_position(&mut self, point: Point) {
     31         self.position = point;
     32     }
     33 
     34     fn echo(&mut self, s: String) {
     35         self.message = s;
     36     }
     37 
     38     fn change_color(&mut self, red: u8, green: u8, blue: u8) {
     39         self.color = (red, green, blue);
     40     }
     41 
     42     fn quit(&mut self) {
     43         self.quit = true;
     44     }
     45 
     46     fn process(&mut self, message: Message) {
     47         // TODO: Create a match expression to process the different message
     48         // variants using the methods defined above.
     49         match message {
     50             Message::Resize { width, height } => self.resize(width, height),
     51             Message::Move(point) => self.move_position(point),
     52             Message::Echo(string) => self.echo(string),
     53             Message::ChangeColor(red, green, blue) => self.change_color(red, green, blue),
     54             Message::Quit => self.quit(),
     55         }
     56     }
     57 }
     58 
     59 fn main() {
     60     // You can optionally experiment here.
     61 }
     62 
     63 #[cfg(test)]
     64 mod tests {
     65     use super::*;
     66 
     67     #[test]
     68     fn test_match_message_call() {
     69         let mut state = State {
     70             width: 0,
     71             height: 0,
     72             position: Point { x: 0, y: 0 },
     73             message: String::from("hello world"),
     74             color: (0, 0, 0),
     75             quit: false,
     76         };
     77 
     78         state.process(Message::Resize {
     79             width: 10,
     80             height: 30,
     81         });
     82         state.process(Message::Move(Point { x: 10, y: 15 }));
     83         state.process(Message::Echo(String::from("Hello world!")));
     84         state.process(Message::ChangeColor(255, 0, 255));
     85         state.process(Message::Quit);
     86 
     87         assert_eq!(state.width, 10);
     88         assert_eq!(state.height, 30);
     89         assert_eq!(state.position.x, 10);
     90         assert_eq!(state.position.y, 15);
     91         assert_eq!(state.message, "Hello world!");
     92         assert_eq!(state.color, (255, 0, 255));
     93         assert!(state.quit);
     94     }
     95 }