use std::fmt;
use std::hash::BuildHasherDefault;
use std::{collections::HashMap, u32};
pub type Tags<'a> = HashMap<&'a str, TagValue<'a>, BuildHasherDefault<hash::TagsHasher>>;
mod hash {
use std::hash::Hasher;
pub struct TagsHasher(u64);
impl Default for TagsHasher {
#[inline]
fn default() -> TagsHasher {
TagsHasher(14695981039346656037)
}
}
impl Hasher for TagsHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
let TagsHasher(mut hash) = *self;
for byte in bytes.iter() {
hash ^= *byte as u64;
hash = hash.wrapping_mul(1099511628211);
}
*self = TagsHasher(hash);
}
}
}
#[derive(Debug, PartialEq)]
pub enum TagValue<'a> {
Color(u32),
Number(u32),
Timestamp(u64),
Boolean(bool),
String(&'a str),
None,
}
impl<'a> TagValue<'a> {
pub fn new(val: &'a str) -> TagValue<'a> {
match val {
"" => TagValue::None,
"0" => TagValue::Boolean(false),
"1" => TagValue::Boolean(true),
_ => {
if let Ok(num) = val.parse::<u32>() {
TagValue::Number(num)
} else if let Ok(tm) = val.parse::<u64>() {
TagValue::Timestamp(tm)
} else if val.starts_with('#') {
if let Ok(num) = u32::from_str_radix(&val[1..], 16) {
TagValue::Color(num)
} else {
TagValue::String(val)
}
} else {
TagValue::String(val)
}
}
}
}
}
impl<'a> fmt::Display for TagValue<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TagValue::Color(num) => write!(f, "#{:06X}", num),
TagValue::Number(num) => write!(f, "{}", num),
TagValue::Timestamp(num) => write!(f, "{}", num),
TagValue::Boolean(false) => write!(f, "0"),
TagValue::Boolean(true) => write!(f, "1"),
TagValue::String(val) => write!(f, "{}", val),
TagValue::None => write!(f, ""),
}
}
}