// License: see LICENSE file at root directory of `master` branch

//! # Root

use std::{
    io::{self, Read},
    mem,
    net::TcpStream,
    time::Duration,
};

/// # Unique ID
///
/// SHA3-512 is intended to be used as UIDs.
pub type Uid = [u8; 64];

/// # Default IP address, used for both server and client
pub const DEFAULT_IP: [u8; 4] = [127, 0, 0, 1];

/// # Default read/write timeout: 3 seconds
pub const DEFAULT_RW_TIMEOUT: Duration = Duration::from_secs(3);

/// # Compares UIDs
pub fn cmp_uids(a: &Uid, b: &Uid) -> bool {
    for i in 0..a.len() {
        if a[i] != b[i] {
            return false;
        }
    }
    true
}

/// # Reads UID
pub fn read_uid<T>(stream: &mut TcpStream) -> io::Result<T> where T: From<Uid> {
    let mut buf = [0_u8; mem::size_of::<Uid>()];
    stream.read_exact(&mut buf)?;
    Ok(buf.into())
}

#[test]
fn test_uid() {
    assert_eq!(mem::size_of::<Uid>(), 64);
}
