rustlings

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

enums3.rs (2188B)


      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     color: (u8, u8, u8),
     20     quit: bool,
     21 }
     22 
     23 impl State {
     24     fn resize(&mut self, width: u64, height: u64) {
     25         self.width = width;
     26         self.height = height;
     27     }
     28 
     29     fn move_position(&mut self, point: Point) {
     30         self.position = point;
     31     }
     32 
     33     fn echo(&mut self, s: String) {
     34         self.message = s;
     35     }
     36 
     37     fn change_color(&mut self, red: u8, green: u8, blue: u8) {
     38         self.color = (red, green, blue);
     39     }
     40 
     41     fn quit(&mut self) {
     42         self.quit = true;
     43     }
     44 
     45     fn process(&mut self, message: Message) {
     46         match message {
     47             Message::Resize { width, height } => self.resize(width, height),
     48             Message::Move(point) => self.move_position(point),
     49             Message::Echo(string) => self.echo(string),
     50             Message::ChangeColor(red, green, blue) => self.change_color(red, green, blue),
     51             Message::Quit => self.quit(),
     52         }
     53     }
     54 }
     55 
     56 fn main() {
     57     // You can optionally experiment here.
     58 }
     59 
     60 #[cfg(test)]
     61 mod tests {
     62     use super::*;
     63 
     64     #[test]
     65     fn test_match_message_call() {
     66         let mut state = State {
     67             width: 0,
     68             height: 0,
     69             position: Point { x: 0, y: 0 },
     70             message: String::from("hello world"),
     71             color: (0, 0, 0),
     72             quit: false,
     73         };
     74 
     75         state.process(Message::Resize {
     76             width: 10,
     77             height: 30,
     78         });
     79         state.process(Message::Move(Point { x: 10, y: 15 }));
     80         state.process(Message::Echo(String::from("Hello world!")));
     81         state.process(Message::ChangeColor(255, 0, 255));
     82         state.process(Message::Quit);
     83 
     84         assert_eq!(state.width, 10);
     85         assert_eq!(state.height, 30);
     86         assert_eq!(state.position.x, 10);
     87         assert_eq!(state.position.y, 15);
     88         assert_eq!(state.message, "Hello world!");
     89         assert_eq!(state.color, (255, 0, 255));
     90         assert!(state.quit);
     91     }
     92 }