use hypercore_protocol::hypercore::VerifyingKey;
use super::constants::PEERMERGE_VERSION;
use crate::{DocumentId, FeedDiscoveryKey, FeedPublicKey};
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
#[non_exhaustive]
pub enum FeedType {
Hypercore = 0,
P2Panda = 1,
}
impl TryFrom<u8> for FeedType {
type Error = ();
fn try_from(input: u8) -> Result<Self, <Self as TryFrom<u8>>::Error> {
match input {
0u8 => Ok(Self::Hypercore),
1u8 => Ok(Self::P2Panda),
_ => Err(()),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum AccessType {
Proxy = 0,
ReadOnly = 1,
ReadWrite = 2,
}
impl TryFrom<u8> for AccessType {
type Error = ();
fn try_from(input: u8) -> Result<Self, <Self as TryFrom<u8>>::Error> {
match input {
0u8 => Ok(Self::Proxy),
1u8 => Ok(Self::ReadOnly),
2u8 => Ok(Self::ReadWrite),
_ => Err(()),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StaticDocumentInfo {
pub version: u8,
pub feed_type: FeedType,
pub child: bool,
pub document_public_key: FeedPublicKey,
pub document_discovery_key: FeedDiscoveryKey,
pub document_id: DocumentId,
pub document_signature_verifying_key: VerifyingKey,
}
impl StaticDocumentInfo {
pub(crate) fn from_keys(
document_public_key: FeedPublicKey,
document_discovery_key: FeedDiscoveryKey,
document_id: DocumentId,
document_signature_verifying_key: VerifyingKey,
child: bool,
) -> Self {
Self::new(
PEERMERGE_VERSION,
FeedType::Hypercore,
child,
document_public_key,
document_discovery_key,
document_id,
document_signature_verifying_key,
)
}
pub(crate) fn new(
version: u8,
feed_type: FeedType,
child: bool,
document_public_key: [u8; 32],
document_discovery_key: [u8; 32],
document_id: DocumentId,
document_signature_verifying_key: VerifyingKey,
) -> Self {
Self {
version,
feed_type,
child,
document_public_key,
document_discovery_key,
document_id,
document_signature_verifying_key,
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DynamicDocumentInfo {
pub document_type: String,
pub document_header: Option<NameDescription>,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct UrlDocumentInfo {
pub access_type: AccessType,
pub encrypted: Option<bool>,
pub static_info: StaticDocumentInfo,
pub dynamic_info: Option<DynamicDocumentInfo>,
}
impl UrlDocumentInfo {
pub fn id(&self) -> DocumentId {
self.static_info.document_id
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DocumentInfo {
pub access_type: AccessType,
pub encrypted: Option<bool>,
pub child_documents: Vec<DocumentId>,
pub static_info: StaticDocumentInfo,
pub dynamic_info: Option<DynamicDocumentInfo>,
}
impl DocumentInfo {
pub fn id(&self) -> DocumentId {
self.static_info.document_id
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DocumentSharingInfo {
pub proxy_document_url: String,
pub read_write_document_url: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NameDescription {
pub name: String,
pub description: Option<String>,
}
impl NameDescription {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
description: None,
}
}
pub fn new_with_description(name: &str, description: &str) -> Self {
Self {
name: name.to_string(),
description: Some(description.to_string()),
}
}
}