use crate::cell::CellBuilder;
use crate::hash::{ParamHash, ParamHasher};
use crate::library::Library;
use std::hash::Hash;
pub struct BuildCtx<'a> {
pub lib: &'a Library,
}
impl<'a> BuildCtx<'a> {
pub fn new(lib: &'a Library) -> Self {
Self { lib }
}
}
pub trait Component: Hash + 'static {
fn build(&self, ctx: &BuildCtx<'_>) -> CellBuilder;
fn param_hash(&self) -> ParamHash {
let mut h = ParamHasher::new();
std::hash::Hasher::write(&mut h, std::any::type_name::<Self>().as_bytes());
std::hash::Hasher::write_u8(&mut h, 0);
Hash::hash(self, &mut h);
h.finalize()
}
fn cell_name(&self) -> String {
format!(
"{}_{}",
std::any::type_name::<Self>()
.rsplit("::")
.next()
.unwrap_or("cell"),
self.param_hash().short_hex()
)
}
}