holochain_integrity_types/
zome.rs

1//! A `Zome` is a module of app-defined code which can be run by Holochain.
2//! A group of Zomes are composed to form a `DnaDef`.
3//!
4//! Real-world Holochain Zomes are written in Wasm.
5//! This module also provides for an "inline" zome definition, which is written
6//! using Rust closures, and is useful for quickly defining zomes on-the-fly
7//! for tests.
8
9use std::borrow::Cow;
10
11use holochain_serialized_bytes::prelude::*;
12
13/// ZomeName as a String.
14#[derive(Clone, Debug, Serialize, Hash, Deserialize, Ord, Eq, PartialEq, PartialOrd)]
15#[repr(transparent)]
16pub struct ZomeName(pub Cow<'static, str>);
17
18impl ZomeName {
19    /// Create an unknown zome name.
20    pub fn unknown() -> Self {
21        "UnknownZomeName".into()
22    }
23
24    /// Create a zome name from a string.
25    pub fn new<S: ToString>(s: S) -> Self {
26        ZomeName(s.to_string().into())
27    }
28}
29
30impl std::fmt::Display for ZomeName {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{}", self.0)
33    }
34}
35
36impl From<&str> for ZomeName {
37    fn from(s: &str) -> Self {
38        Self(s.to_string().into())
39    }
40}
41
42impl From<String> for ZomeName {
43    fn from(s: String) -> Self {
44        Self(s.into())
45    }
46}
47
48/// A single function name.
49#[repr(transparent)]
50#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Eq, Hash)]
51pub struct FunctionName(pub String);
52
53impl FunctionName {
54    /// Create a new function name.
55    pub fn new<S: ToString>(s: S) -> Self {
56        FunctionName(s.to_string())
57    }
58}
59
60impl std::fmt::Display for FunctionName {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{}", self.0)
63    }
64}
65
66impl From<FunctionName> for String {
67    fn from(function_name: FunctionName) -> Self {
68        function_name.0
69    }
70}
71
72impl From<String> for FunctionName {
73    fn from(s: String) -> Self {
74        Self(s)
75    }
76}
77
78impl From<&str> for FunctionName {
79    fn from(s: &str) -> Self {
80        Self::from(s.to_string())
81    }
82}
83
84impl AsRef<str> for FunctionName {
85    fn as_ref(&self) -> &str {
86        self.0.as_ref()
87    }
88}