rust/hedgewars-server/src/handlers.rs
changeset 15163 bcb98009ad39
parent 15113 7d6e519a88c5
child 15176 f6115638aa92
equal deleted inserted replaced
15162:7416f6319de9 15163:bcb98009ad39
     1 use mio;
     1 use mio;
     2 use std::{collections::HashMap, io, io::Write};
     2 use std::{
       
     3     cmp::PartialEq,
       
     4     collections::HashMap,
       
     5     fmt::{Formatter, LowerHex},
       
     6     iter::Iterator,
       
     7 };
     3 
     8 
     4 use self::{
     9 use self::{
     5     actions::{Destination, DestinationGroup, PendingMessage},
    10     actions::{Destination, DestinationGroup, PendingMessage},
     6     inanteroom::LoginResult,
    11     inanteroom::LoginResult,
     7 };
    12 };
    26 mod common;
    31 mod common;
    27 mod inanteroom;
    32 mod inanteroom;
    28 mod inlobby;
    33 mod inlobby;
    29 mod inroom;
    34 mod inroom;
    30 
    35 
    31 use std::fmt::{Formatter, LowerHex};
       
    32 
       
    33 #[derive(PartialEq, Debug)]
    36 #[derive(PartialEq, Debug)]
    34 pub struct Sha1Digest([u8; 20]);
    37 pub struct Sha1Digest([u8; 20]);
    35 
    38 
    36 impl Sha1Digest {
    39 impl Sha1Digest {
    37     pub fn new(digest: [u8; 20]) -> Self {
    40     pub fn new(digest: [u8; 20]) -> Self {
    43     fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
    46     fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
    44         for byte in &self.0 {
    47         for byte in &self.0 {
    45             write!(f, "{:02x}", byte)?;
    48             write!(f, "{:02x}", byte)?;
    46         }
    49         }
    47         Ok(())
    50         Ok(())
       
    51     }
       
    52 }
       
    53 
       
    54 impl PartialEq<&str> for Sha1Digest {
       
    55     fn eq(&self, other: &&str) -> bool {
       
    56         if other.len() != self.0.len() * 2 {
       
    57             false
       
    58         } else {
       
    59             #[inline]
       
    60             fn convert(c: u8) -> u8 {
       
    61                 if c > b'9' {
       
    62                     c.overflowing_sub(b'a').0.saturating_add(10)
       
    63                 } else {
       
    64                     c.overflowing_sub(b'0').0
       
    65                 }
       
    66             }
       
    67 
       
    68             other
       
    69                 .as_bytes()
       
    70                 .chunks_exact(2)
       
    71                 .zip(&self.0)
       
    72                 .all(|(chars, byte)| {
       
    73                     if let [hi, lo] = chars {
       
    74                         convert(*lo) == byte & 0x0f && convert(*hi) == (byte & 0xf0) >> 4
       
    75                     } else {
       
    76                         unreachable!()
       
    77                     }
       
    78                 })
       
    79         }
    48     }
    80     }
    49 }
    81 }
    50 
    82 
    51 #[derive(Debug)]
    83 #[derive(Debug)]
    52 pub struct AccountInfo {
    84 pub struct AccountInfo {
   409         IoResult::LoadRoom(_, None) => {
   441         IoResult::LoadRoom(_, None) => {
   410             response.add(Warning("Unable to load the room configs.".to_string()).send_self());
   442             response.add(Warning("Unable to load the room configs.".to_string()).send_self());
   411         }
   443         }
   412     }
   444     }
   413 }
   445 }
       
   446 
       
   447 #[cfg(test)]
       
   448 mod test {
       
   449     use super::Sha1Digest;
       
   450 
       
   451     #[test]
       
   452     fn hash_cmp_test() {
       
   453         let hash = Sha1Digest([
       
   454             0x37, 0xC4, 0x9F, 0x5C, 0xC3, 0xC9, 0xDB, 0xFC, 0x54, 0xAC, 0x22, 0x04, 0xF6, 0x12,
       
   455             0x9A, 0xED, 0x69, 0xB1, 0xC4, 0x5C,
       
   456         ]);
       
   457 
       
   458         assert_eq!(hash, &format!("{:x}", hash)[..]);
       
   459     }
       
   460 }