use std::any::type_name;
mod data;
pub use data::Data;
pub use dos_uid_derive::UID;
pub mod units;
pub mod select;
#[cfg(feature = "filing")]
pub mod filing;
pub type Assoc<U> = <U as UniqueIdentifier>::DataType;
pub trait Units {}
pub enum Tick {}
impl UniqueIdentifier for Tick {
type DataType = ();
}
pub trait TimerMarker {}
impl<T> Read<Tick> for T
where
T: TimerMarker + Update,
{
fn read(&mut self, _: Data<Tick>) {}
}
pub trait UniqueIdentifier: Send + Sync {
const PORT: u16 = 50_000;
type DataType: Send + Sync;
}
pub trait Quote {
fn quote() -> String;
}
impl<U: UniqueIdentifier> Quote for U {
fn quote() -> String {
fn inner(name: &str) -> String {
if let Some((prefix, suffix)) = name.split_once('<') {
let generics: Vec<_> = suffix.split(',').map(|s| inner(s)).collect();
format!("{}<{}", inner(prefix), generics.join(","))
} else {
if let Some((_, suffix)) = name.rsplit_once("::") {
suffix.into()
} else {
name.into()
}
}
}
inner(type_name::<U>())
}
}
pub trait Update: Send + Sync {
fn update(&mut self) {}
}
pub trait Read<U: UniqueIdentifier>: Update {
fn read(&mut self, data: Data<U>);
}
pub trait Write<U: UniqueIdentifier>: Update {
fn write(&mut self) -> Option<Data<U>>;
}
pub trait Size<U: UniqueIdentifier>: Update {
fn len(&self) -> usize;
}
pub trait Who<T> {
fn who(&self) -> String {
type_name::<T>().to_string()
}
fn highlight(&self) -> String {
let me = <Self as Who<T>>::who(&self);
paris::formatter::colorize_string(format!("<italic><on-bright-cyan>{}</>", me))
}
fn lite(&self) -> String {
let me = <Self as Who<T>>::who(&self);
paris::formatter::colorize_string(format!("<italic><bright-cyan>{}</>", me))
}
}
use log::{info, warn};
pub fn print_info<S: Into<String>>(msg: S, e: Option<&dyn std::error::Error>) {
if let Some(e) = e {
let mut msg: Vec<String> = vec![msg.into()];
msg.push(format!("{}", e));
let mut current = e.source();
while let Some(cause) = current {
msg.push(format!("{}", cause));
current = cause.source();
}
warn!("{}", msg.join("\n .due to: "))
} else {
info!("{}", msg.into())
}
}
pub trait Entry<U: UniqueIdentifier>: Update {
fn entry(&mut self, size: usize);
}