use std::collections::HashMap;
use std::time::Duration;
use chrono;
use super::debug;
use std::io::{BufWriter, Write};
use std::net::TcpStream;
pub fn chrono_to_std(duration: chrono::Duration) -> Option<Duration> {
Some(Duration::from_millis(duration.num_milliseconds() as u64))
}
pub fn std_to_chrono(duration: Duration) -> Option<chrono::Duration> {
if let Ok(period) = chrono::Duration::from_std(duration) {
Some(period)
} else {
None
}
}
pub trait MapUpdates<T> {
fn add(&mut self, field: &str, value: T, allow_override: bool) -> Option<T>;
}
impl<T> MapUpdates<T> for HashMap<String, T> {
fn add(&mut self, field: &str, value: T, allow_override: bool) -> Option<T> {
if field.is_empty() { return None; }
let f = field.to_lowercase();
if allow_override {
self.insert(f, value)
} else {
self.entry(f).or_insert(value);
None
}
}
}
pub fn write_to_buff(buffer: &mut BufWriter<&TcpStream>, content: &[u8]) {
if let Err(err) = buffer.write(content) {
debug::print(
&format!("An error has taken place when writing the response header to the stream: {}", err),
1);
}
}
pub fn flush_buffer(buffer: &mut BufWriter<&TcpStream>) -> Option<u8> {
if let Err(err) = buffer.flush() {
debug::print(
&format!("An error has taken place when flushing the response to the stream: {}", err)[..],
1);
return Some(1)
}
Some(0)
}