Guest User

Untitled

a guest
Dec 4th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.74 KB | None | 0 0
  1. #[derive(Debug, Hash, Copy, Clone)]
  2. enum Action {
  3.     Begin(NaiveDateTime, usize),
  4.     Asleep(NaiveDateTime),
  5.     Wake(NaiveDateTime),
  6. }
  7.  
  8. impl FromStr for Action {
  9.     type Err = ();
  10.  
  11.     fn from_str(s: &str) -> Result<Self, Self::Err> {
  12.         let (date, action) = s.split_at(18);
  13.         let date = NaiveDateTime::parse_from_str(date, "[%Y-%m-%d %H:%M]").unwrap();
  14.  
  15.         Ok(match &action[1..] {
  16.             "falls asleep" => Action::Asleep(date),
  17.             "wakes up" => Action::Wake(date),
  18.             _ => {
  19.                 let guard_id = action.split_whitespace().nth(1).unwrap()[1..]
  20.                     .parse()
  21.                     .unwrap();
  22.                 Action::Begin(date, guard_id)
  23.             }
  24.         })
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment