use libipld::{Cid, DagCbor};
use lru_mem::HeapSize;
use serde::{Deserialize, Serialize};
use std::{
future::Future,
io::Result,
time::{Duration, SystemTime},
};
#[derive(Debug, DagCbor, Clone, Serialize, Deserialize)]
pub struct Multipart {
pub length: u64,
pub content: ContentType,
}
impl HeapSize for Multipart {
fn heap_size(&self) -> usize {
match &self.content {
ContentType::Mixed(cids) => cids.len() * 64 + 8,
ContentType::Related(cids) => cids.len() * 64 + 8,
ContentType::Alternative(_, _) => 2 * 64 + 8,
ContentType::Blocks(cids) => cids.len() * 64 + 8,
ContentType::Mime(name, data) => data.len() + name.len() + 8,
}
}
}
#[derive(Debug, DagCbor, Clone, Serialize, Deserialize)]
pub enum ContentType {
Mixed(Vec<Cid>),
Related(Vec<Cid>),
Alternative(Cid, Cid),
Blocks(Vec<Cid>),
Mime(String, Vec<u8>),
}
pub struct IpldEntry {
pub cid: Cid,
pub length: u64,
pub created: SystemTime,
pub expired: SystemTime,
}
pub struct IpldWrite {
pub cid: Cid,
pub unlinked: Vec<Cid>,
}
pub trait IpldFS {
type ReadEntry<'cx>: Future<Output = Result<IpldEntry>> + Send + Unpin + 'cx
where
Self: 'cx;
type Write<'cx>: Future<Output = Result<Vec<Cid>>> + Send + Unpin + 'cx
where
Self: 'cx;
type Read<'cx>: Future<Output = Result<Multipart>> + Send + Unpin + 'cx
where
Self: 'cx;
fn read_entry<'cx, 'a>(&'a mut self, cid: Cid) -> Self::ReadEntry<'cx>
where
'a: 'cx;
fn write<'cx, 'a>(
&'a mut self,
cid: Cid,
multipart: Multipart,
lease: Duration,
) -> Self::Write<'cx>
where
'a: 'cx;
fn read<'cx, 'a>(&'a mut self, cid: &'cx Cid) -> Self::Read<'cx>
where
'a: 'cx;
}