use std::borrow::Borrow;
use std::borrow::Cow;
use holochain_serialized_bytes::prelude::*;
const DEFAULT_REQUIRED_VALIDATIONS: u8 = 5;
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum EntryDefId {
App(AppEntryName),
CapClaim,
CapGrant,
}
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct AppEntryName(pub Cow<'static, str>);
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for AppEntryName {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self(Cow::Owned(String::arbitrary(u)?)))
}
}
pub trait EntryDefRegistration {
const ENTRY_DEFS: &'static [EntryDef];
}
#[derive(
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct RequiredValidations(pub u8);
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct EntryDef {
pub id: EntryDefId,
pub visibility: EntryVisibility,
pub required_validations: RequiredValidations,
pub cache_at_agent_activity: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct EntryDefs(pub Vec<EntryDef>);
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
SerializedBytes,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum EntryVisibility {
Public,
Private,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, SerializedBytes)]
pub enum EntryDefsCallbackResult {
Defs(EntryDefs),
}
impl AppEntryName {
pub fn new(s: impl Into<Cow<'static, str>>) -> Self {
Self(s.into())
}
pub const fn from_str(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
}
impl Default for EntryVisibility {
fn default() -> Self {
Self::Public
}
}
impl From<u8> for RequiredValidations {
fn from(u: u8) -> Self {
Self(u)
}
}
impl From<RequiredValidations> for u8 {
fn from(required_validations: RequiredValidations) -> Self {
required_validations.0
}
}
impl Default for RequiredValidations {
fn default() -> Self {
Self(DEFAULT_REQUIRED_VALIDATIONS)
}
}
impl EntryVisibility {
pub fn is_public(&self) -> bool {
*self == EntryVisibility::Public
}
}
impl EntryDef {
pub fn new(
id: EntryDefId,
visibility: EntryVisibility,
required_validations: RequiredValidations,
cache_at_agent_activity: bool,
) -> Self {
Self {
id,
visibility,
required_validations,
cache_at_agent_activity,
}
}
#[cfg(any(test, feature = "test_utils"))]
pub fn default_from_id<I: Into<EntryDefId>>(id: I) -> Self {
EntryDef {
id: id.into(),
..Default::default()
}
}
}
impl std::ops::Index<usize> for EntryDefs {
type Output = EntryDef;
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
impl IntoIterator for EntryDefs {
type Item = EntryDef;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<Vec<EntryDef>> for EntryDefs {
fn from(v: Vec<EntryDef>) -> Self {
Self(v)
}
}
impl From<Vec<EntryDef>> for EntryDefsCallbackResult {
fn from(v: Vec<EntryDef>) -> Self {
Self::Defs(v.into())
}
}
impl From<String> for EntryDefId {
fn from(s: String) -> Self {
Self::App(s.into())
}
}
impl From<&str> for EntryDefId {
fn from(s: &str) -> Self {
Self::App(s.to_string().into())
}
}
impl From<&'static str> for AppEntryName {
fn from(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
}
impl From<String> for AppEntryName {
fn from(s: String) -> Self {
Self(Cow::Owned(s))
}
}
impl From<AppEntryName> for EntryDefId {
fn from(name: AppEntryName) -> Self {
EntryDefId::App(name)
}
}
impl Borrow<str> for AppEntryName {
fn borrow(&self) -> &str {
self.0.borrow()
}
}
impl std::fmt::Display for AppEntryName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[cfg(any(test, feature = "test_utils"))]
impl Default for EntryDef {
fn default() -> Self {
Self {
id: EntryDefId::App(AppEntryName(Default::default())),
visibility: Default::default(),
required_validations: Default::default(),
cache_at_agent_activity: false,
}
}
}