use crate::net::Address;
use faststr::FastStr;
use metainfo::FastStrMap;
use std::fmt::Debug;
const DEFAULT_MAP_CAPACITY: usize = 10;
pub struct Component<S> {
pub serve: S,
pub endpoint: Endpoint,
}
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Endpoint {
pub service_name: FastStr,
pub address: Option<Address>,
pub tags: FastStrMap,
pub key_maker: Option<fn(&Self) -> FastStr>,
}
impl Endpoint {
#[inline]
pub fn new(service_name: impl Into<FastStr>) -> Self {
Self {
service_name: service_name.into(),
address: None,
tags: FastStrMap::with_capacity(DEFAULT_MAP_CAPACITY),
key_maker: None,
}
}
pub fn key(&self) -> FastStr {
if let Some(f) = self.key_maker { f(self) } else { self.service_name.clone() }
}
#[inline]
pub fn service_name_ref(&self) -> &str {
&self.service_name
}
#[inline]
pub fn service_name(&self) -> FastStr {
self.service_name.clone()
}
#[inline]
pub fn set_service_name(&mut self, service_name: FastStr) {
self.service_name = service_name;
}
#[inline]
pub fn insert<T: Send + Sync + 'static>(&mut self, val: FastStr) {
self.tags.insert::<T>(val);
}
#[inline]
pub fn contains<T: 'static>(&self) -> bool {
self.tags.contains::<T>()
}
#[inline]
pub fn get<T: 'static>(&self) -> Option<&FastStr> {
self.tags.get::<T>()
}
#[inline]
pub fn set_address(&mut self, address: Address) {
self.address = Some(address)
}
#[inline]
pub fn address(&self) -> Option<Address> {
self.address.clone()
}
#[inline]
pub fn clear(&mut self) {
self.service_name = FastStr::from_static_str("");
self.address = None;
self.tags.clear();
self.key_maker = None;
}
}