use crate::{
chain_header::ChainHeader,
entry::{
entry_type::{AppEntryType, EntryType},
Entry,
},
error::HolochainError,
link::link_data::LinkData,
};
use holochain_json_api::{error::JsonError, json::JsonString};
use holochain_persistence_api::cas::content::Address;
use chain_header::test_chain_header;
use std::convert::TryFrom;
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, DefaultJson)]
pub struct ValidationPackage {
pub chain_header: ChainHeader,
pub source_chain_entries: Option<Vec<Entry>>,
pub source_chain_headers: Option<Vec<ChainHeader>>,
pub custom: Option<String>,
}
impl ValidationPackage {
pub fn only_header(header: ChainHeader) -> ValidationPackage {
ValidationPackage {
chain_header: header,
source_chain_entries: None,
source_chain_headers: None,
custom: None,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, DefaultJson)]
pub enum ValidationPackageDefinition {
Entry,
ChainEntries,
ChainHeaders,
ChainFull,
Custom(String),
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum EntryValidationData<T> {
Create {
entry: T,
validation_data: ValidationData,
},
Modify {
new_entry: T,
old_entry: T,
old_entry_header: ChainHeader,
validation_data: ValidationData,
},
Delete {
old_entry: T,
old_entry_header: ChainHeader,
validation_data: ValidationData,
},
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum LinkValidationData {
LinkAdd {
link: LinkData,
validation_data: ValidationData,
},
LinkRemove {
link: LinkData,
validation_data: ValidationData,
},
}
impl TryFrom<EntryValidationData<Entry>> for EntryType {
type Error = HolochainError;
fn try_from(entry_validation: EntryValidationData<Entry>) -> Result<Self, Self::Error> {
match entry_validation {
EntryValidationData::Create { entry, .. } => {
Ok(EntryType::App(AppEntryType::try_from(entry.entry_type())?))
}
EntryValidationData::Delete { old_entry, .. } => Ok(EntryType::App(
AppEntryType::try_from(old_entry.entry_type())?,
)),
EntryValidationData::Modify { new_entry, .. } => Ok(EntryType::App(
AppEntryType::try_from(new_entry.entry_type())?,
)),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ValidationData {
pub package: ValidationPackage,
pub lifecycle: EntryLifecycle,
}
impl Default for ValidationData {
fn default() -> Self {
Self {
package: ValidationPackage {
chain_header: test_chain_header(),
source_chain_entries: None,
source_chain_headers: None,
custom: None,
},
lifecycle: EntryLifecycle::default(),
}
}
}
impl ValidationData {
pub fn sources(&self) -> Vec<Address> {
self.package
.chain_header
.provenances()
.iter()
.map(|provenance| provenance.source())
.collect()
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum EntryLifecycle {
Chain,
Dht,
Meta,
}
impl Default for EntryLifecycle {
fn default() -> Self {
EntryLifecycle::Chain
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum EntryAction {
Create,
Modify,
Delete,
}
impl Default for EntryAction {
fn default() -> Self {
EntryAction::Create
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum LinkAction {
Create,
Delete,
}