use holochain_serialized_bytes::prelude::*;
pub mod error;
#[cfg(feature = "full-dna-def")]
pub mod inline_zome;
use error::ZomeResult;
#[cfg(feature = "full-dna-def")]
use self::inline_zome::InlineZome;
#[cfg(feature = "full-dna-def")]
use error::ZomeError;
#[cfg(feature = "full-dna-def")]
use std::sync::Arc;
#[derive(Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "full-dna-def", derive(shrinkwraprs::Shrinkwrap))]
pub struct Zome {
name: ZomeName,
#[cfg_attr(feature = "full-dna-def", shrinkwrap(main_field))]
def: ZomeDef,
}
impl Zome {
pub fn new(name: ZomeName, def: ZomeDef) -> Self {
Self { name, def }
}
pub fn zome_name(&self) -> &ZomeName {
&self.name
}
pub fn zome_def(&self) -> &ZomeDef {
&self.def
}
pub fn into_inner(self) -> (ZomeName, ZomeDef) {
(self.name, self.def)
}
}
impl From<(ZomeName, ZomeDef)> for Zome {
fn from(pair: (ZomeName, ZomeDef)) -> Self {
Self::new(pair.0, pair.1)
}
}
impl From<Zome> for (ZomeName, ZomeDef) {
fn from(zome: Zome) -> Self {
zome.into_inner()
}
}
impl From<Zome> for ZomeName {
fn from(zome: Zome) -> Self {
zome.name
}
}
impl From<Zome> for ZomeDef {
fn from(zome: Zome) -> Self {
zome.def
}
}
#[derive(Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[serde(untagged, into = "ZomeDefSerialized")]
pub enum ZomeDef {
Wasm(WasmZome),
#[serde(skip_deserializing)]
#[cfg(feature = "full-dna-def")]
Inline(Arc<InlineZome>),
}
#[derive(Serialize)]
#[serde(untagged)]
enum ZomeDefSerialized {
Wasm(WasmZome),
#[cfg(feature = "full-dna-def")]
InlineUid(String),
}
impl From<ZomeDef> for ZomeDefSerialized {
fn from(d: ZomeDef) -> Self {
match d {
ZomeDef::Wasm(zome) => Self::Wasm(zome),
#[cfg(feature = "full-dna-def")]
ZomeDef::Inline(zome) => Self::InlineUid(zome.uuid.clone()),
}
}
}
#[cfg(feature = "full-dna-def")]
impl From<InlineZome> for ZomeDef {
fn from(iz: InlineZome) -> Self {
Self::Inline(Arc::new(iz))
}
}
impl ZomeDef {
pub fn wasm_hash(&self, _zome_name: &ZomeName) -> ZomeResult<holo_hash::WasmHash> {
match self {
ZomeDef::Wasm(WasmZome { wasm_hash }) => Ok(wasm_hash.clone()),
#[cfg(feature = "full-dna-def")]
ZomeDef::Inline(_) => Err(ZomeError::NonWasmZome(_zome_name.clone())),
}
}
}
#[derive(
Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, SerializedBytes,
)]
pub struct WasmZome {
pub wasm_hash: holo_hash::WasmHash,
}
impl WasmZome {
pub fn new(wasm_hash: holo_hash::WasmHash) -> Self {
Self { wasm_hash }
}
}
impl ZomeDef {
pub fn from_hash(wasm_hash: holo_hash::WasmHash) -> Self {
Self::Wasm(WasmZome { wasm_hash })
}
}
#[derive(Clone, Debug, Serialize, Hash, Deserialize, Ord, Eq, PartialEq, PartialOrd)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(transparent)]
pub struct ZomeName(pub String);
impl ZomeName {
pub fn unknown() -> Self {
"UnknownZomeName".into()
}
}
impl std::fmt::Display for ZomeName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for ZomeName {
fn from(s: &str) -> Self {
Self::from(s.to_string())
}
}
impl From<String> for ZomeName {
fn from(s: String) -> Self {
Self(s)
}
}
#[repr(transparent)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct FunctionName(pub String);
impl std::fmt::Display for FunctionName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<FunctionName> for String {
fn from(function_name: FunctionName) -> Self {
function_name.0
}
}
impl From<String> for FunctionName {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for FunctionName {
fn from(s: &str) -> Self {
Self::from(s.to_string())
}
}
impl AsRef<str> for FunctionName {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}