#[cfg(not(feature = "std"))]
extern crate alloc;
use crate::{Blake3Hash, Blake3HashHex, PieceIndex};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::default::Default;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum BlockObject {
#[codec(index = 0)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
V0 {
#[cfg_attr(feature = "serde", serde(with = "hex"))]
hash: Blake3Hash,
offset: u32,
},
}
impl BlockObject {
pub fn hash(&self) -> Blake3Hash {
match self {
Self::V0 { hash, .. } => *hash,
}
}
pub fn offset(&self) -> u32 {
match self {
Self::V0 { offset, .. } => *offset,
}
}
pub fn set_offset(&mut self, new_offset: u32) {
match self {
Self::V0 { offset, .. } => {
*offset = new_offset;
}
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct BlockObjectMapping {
pub objects: Vec<BlockObject>,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum PieceObject {
#[codec(index = 0)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
V0 {
#[cfg_attr(feature = "serde", serde(with = "hex"))]
hash: Blake3Hash,
offset: u32,
},
}
impl PieceObject {
pub fn hash(&self) -> Blake3Hash {
match self {
Self::V0 { hash, .. } => *hash,
}
}
pub fn offset(&self) -> u32 {
match self {
Self::V0 { offset, .. } => *offset,
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct PieceObjectMapping {
pub objects: Vec<PieceObject>,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(from = "CompactGlobalObject", into = "CompactGlobalObject")
)]
pub struct GlobalObject {
pub hash: Blake3HashHex,
pub piece_index: PieceIndex,
pub offset: u32,
}
impl From<CompactGlobalObject> for GlobalObject {
fn from(object: CompactGlobalObject) -> Self {
Self {
hash: object.0,
piece_index: object.1,
offset: object.2,
}
}
}
impl From<GlobalObject> for CompactGlobalObject {
fn from(object: GlobalObject) -> Self {
Self(object.hash, object.piece_index, object.offset)
}
}
impl GlobalObject {
pub fn new(piece_index: PieceIndex, piece_object: &PieceObject) -> Self {
Self {
hash: piece_object.hash().into(),
piece_index,
offset: piece_object.offset(),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompactGlobalObject(Blake3HashHex, PieceIndex, u32);
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "serde", serde(rename_all_fields = "camelCase"))]
pub enum GlobalObjectMapping {
#[codec(index = 0)]
V0 {
objects: Vec<GlobalObject>,
},
}
impl Default for GlobalObjectMapping {
fn default() -> Self {
Self::V0 {
objects: Vec::new(),
}
}
}
impl GlobalObjectMapping {
#[inline]
pub fn from_objects(objects: impl IntoIterator<Item = GlobalObject>) -> Self {
Self::V0 {
objects: objects.into_iter().collect(),
}
}
pub fn objects(&self) -> &[GlobalObject] {
match self {
Self::V0 { objects, .. } => objects,
}
}
}