#![deny(missing_docs)]
#[macro_use]
mod impl_macro;
use std::fmt::{Display, Formatter, Result};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Callpass(u16);
impl Callpass {
fn new(callsign: &String) -> Callpass {
Callpass({
let mut hash: u16 = 0x73e2; let mut i = true; for x in callsign.chars() {
let x = x.to_uppercase().next().unwrap() as u16;
match i {
true => hash = hash ^ x << 8,
false => hash = hash ^ x,
};
i = !i;
}
hash & 0x7fff
})
}
}
generate_impl_into!(u16, i16, u32, i32, u64, i64);
generate_impl_from!(u16, i16, u32, i32, u64, i64);
generate_impl_partialeq!(u16, i16, u32, i32, u64, i64);
impl Display for Callpass {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{:05}", self.0)
}
}
impl Into<String> for Callpass {
fn into(self) -> String {
self.0.to_string()
}
}
impl From<String> for Callpass {
fn from(callsign: String) -> Self {
Callpass::new(&callsign)
}
}
impl<'a> From<&'a String> for Callpass {
fn from(callsign: &'a String) -> Self {
Callpass::new(callsign)
}
}
impl<'a> From<&'a str> for Callpass {
fn from(callsign: &'a str) -> Self {
Callpass::new(&(callsign.to_string()))
}
}