use crate::chunkid::ChunkId;
use crate::chunkmeta::ChunkMeta;
use crate::label::Label;
use serde::{Deserialize, Serialize};
use std::default::Default;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DataChunk {
data: Vec<u8>,
meta: ChunkMeta,
}
impl DataChunk {
pub fn new(data: Vec<u8>, meta: ChunkMeta) -> Self {
Self { data, meta }
}
pub fn data(&self) -> &[u8] {
&self.data
}
pub fn meta(&self) -> &ChunkMeta {
&self.meta
}
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct GenerationChunk {
chunk_ids: Vec<ChunkId>,
}
#[derive(Debug, thiserror::Error)]
pub enum GenerationChunkError {
#[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error),
#[error("failed to parse JSON: {0}")]
JsonParse(serde_json::Error),
#[error("failed to serialize to JSON: {0}")]
JsonGenerate(serde_json::Error),
}
impl GenerationChunk {
pub fn new(chunk_ids: Vec<ChunkId>) -> Self {
Self { chunk_ids }
}
pub fn from_data_chunk(chunk: &DataChunk) -> Result<Self, GenerationChunkError> {
let data = chunk.data();
let data = std::str::from_utf8(data)?;
serde_json::from_str(data).map_err(GenerationChunkError::JsonParse)
}
pub fn is_empty(&self) -> bool {
self.chunk_ids.is_empty()
}
pub fn len(&self) -> usize {
self.chunk_ids.len()
}
pub fn chunk_ids(&self) -> impl Iterator<Item = &ChunkId> {
self.chunk_ids.iter()
}
pub fn to_data_chunk(&self) -> Result<DataChunk, GenerationChunkError> {
let json: String =
serde_json::to_string(self).map_err(GenerationChunkError::JsonGenerate)?;
let bytes = json.as_bytes().to_vec();
let checksum = Label::sha256(&bytes);
let meta = ChunkMeta::new(&checksum);
Ok(DataChunk::new(bytes, meta))
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ClientTrust {
client_name: String,
previous_version: Option<ChunkId>,
timestamp: String,
backups: Vec<ChunkId>,
}
#[derive(Debug, thiserror::Error)]
pub enum ClientTrustError {
#[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error),
#[error("failed to parse JSON: {0}")]
JsonParse(serde_json::Error),
#[error("failed to serialize to JSON: {0}")]
JsonGenerate(serde_json::Error),
}
impl ClientTrust {
pub fn new(
name: &str,
previous_version: Option<ChunkId>,
timestamp: String,
backups: Vec<ChunkId>,
) -> Self {
Self {
client_name: name.to_string(),
previous_version,
timestamp,
backups,
}
}
pub fn client_name(&self) -> &str {
&self.client_name
}
pub fn previous_version(&self) -> Option<ChunkId> {
self.previous_version.clone()
}
pub fn timestamp(&self) -> &str {
&self.timestamp
}
pub fn backups(&self) -> &[ChunkId] {
&self.backups
}
pub fn append_backup(&mut self, id: &ChunkId) {
self.backups.push(id.clone());
}
pub fn finalize(&mut self, timestamp: String) {
self.timestamp = timestamp;
}
pub fn to_data_chunk(&self) -> Result<DataChunk, ClientTrustError> {
let json: String = serde_json::to_string(self).map_err(ClientTrustError::JsonGenerate)?;
let bytes = json.as_bytes().to_vec();
let checksum = Label::literal("client-trust");
let meta = ChunkMeta::new(&checksum);
Ok(DataChunk::new(bytes, meta))
}
pub fn from_data_chunk(chunk: &DataChunk) -> Result<Self, ClientTrustError> {
let data = chunk.data();
let data = std::str::from_utf8(data)?;
serde_json::from_str(data).map_err(ClientTrustError::JsonParse)
}
}