use hdk::prelude::*;
use holo_hash::ActionHashB64;
use std::fmt;
use crate::wire_record::WireRecord;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, SerializedBytes)]
#[serde(from = "UIEnum")]
#[serde(into = "UIEnum")]
pub enum ActionType {
Create,
Update,
Delete,
}
#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone, PartialEq)]
struct UIEnum(String);
impl From<UIEnum> for ActionType {
fn from(ui_enum: UIEnum) -> Self {
match ui_enum.0.as_str() {
"create" => Self::Create,
"update" => Self::Update,
_ => Self::Delete,
}
}
}
impl From<ActionType> for UIEnum {
fn from(action_type: ActionType) -> Self {
Self(action_type.to_string().to_lowercase())
}
}
impl fmt::Display for ActionType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub fn create_receive_signal_cap_grant() -> ExternResult<()> {
let mut functions = BTreeSet::new();
functions.insert((zome_info()?.name, "recv_remote_signal".into()));
create_cap_grant(CapGrantEntry {
tag: "".into(),
access: ().into(),
functions: GrantedFunctions::Listed(functions),
})?;
Ok(())
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum SignalData<T> {
Create(WireRecord<T>),
Update(WireRecord<T>),
Delete(ActionHashB64),
}
#[doc = " then `data` should be `SignalData::Create`."]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ActionSignal<T> {
pub entry_type: String,
pub action: ActionType,
pub data: SignalData<T>,
}