use super::zome;
use crate::prelude::*;
#[cfg(feature = "full-dna-def")]
use crate::zome::error::ZomeError;
#[cfg(feature = "full-dna-def")]
use holo_hash::*;
pub type Zomes = Vec<(ZomeName, zome::ZomeDef)>;
pub type Uid = String;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, SerializedBytes)]
#[cfg_attr(feature = "full-dna-def", derive(derive_builder::Builder))]
#[cfg_attr(feature = "full-dna-def", builder(public))]
pub struct DnaDef {
#[cfg_attr(
feature = "full-dna-def",
builder(default = "\"Generated DnaDef\".to_string()")
)]
pub name: String,
pub uid: String,
#[cfg_attr(feature = "full-dna-def", builder(default = "().try_into().unwrap()"))]
pub properties: SerializedBytes,
pub zomes: Zomes,
}
#[cfg(feature = "test_utils")]
impl DnaDef {
pub fn unique_from_zomes(zomes: Vec<Zome>) -> DnaDef {
let zomes = zomes.into_iter().map(|z| z.into_inner()).collect();
DnaDefBuilder::default()
.zomes(zomes)
.random_uid()
.build()
.unwrap()
}
}
#[cfg(feature = "full-dna-def")]
impl DnaDef {
pub fn get_zome(&self, zome_name: &ZomeName) -> Result<zome::Zome, ZomeError> {
self.zomes
.iter()
.find(|(name, _)| name == zome_name)
.cloned()
.map(|(name, def)| Zome::new(name, def))
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
}
pub fn get_wasm_zome(&self, zome_name: &ZomeName) -> Result<&zome::WasmZome, ZomeError> {
self.zomes
.iter()
.find(|(name, _)| name == zome_name)
.map(|(_, def)| def)
.ok_or_else(|| ZomeError::ZomeNotFound(format!("Zome '{}' not found", &zome_name,)))
.and_then(|def| {
if let ZomeDef::Wasm(wasm_zome) = def {
Ok(wasm_zome)
} else {
Err(ZomeError::NonWasmZome(zome_name.clone()))
}
})
}
pub fn modify_phenotype(&self, uid: Uid, properties: SerializedBytes) -> Self {
let mut clone = self.clone();
clone.properties = properties;
clone.uid = uid;
clone
}
}
#[cfg(feature = "full-dna-def")]
pub fn random_uid() -> String {
nanoid::nanoid!()
}
#[cfg(feature = "full-dna-def")]
impl DnaDefBuilder {
pub fn random_uid(&mut self) -> &mut Self {
self.uid = Some(random_uid());
self
}
}
#[cfg(feature = "full-dna-def")]
pub type DnaDefHashed = HoloHashed<DnaDef>;
#[cfg(feature = "full-dna-def")]
impl_hashable_content!(DnaDef, Dna);