pub mod binary_version;
pub mod schema_version;
pub use binary_version::BinaryVersion;
pub use schema_version::SchemaVersion;
use crate::{
data_tree::DataTreeReflection,
hardlink::{HardlinkListReflection, SharedLinkSummary},
size::{self, Blocks, Bytes},
};
use derive_more::{Deref, DerefMut, From, TryInto};
use smart_default::SmartDefault;
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
#[derive(Debug, SmartDefault, Clone)]
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
pub struct JsonShared<Size: size::Size> {
#[cfg_attr(
feature = "json",
serde(skip_serializing_if = "JsonShared::skip_details")
)]
pub details: Option<HardlinkListReflection<Size>>,
#[cfg_attr(
feature = "json",
serde(skip_serializing_if = "JsonShared::skip_summary")
)]
pub summary: Option<SharedLinkSummary<Size>>,
}
#[cfg(feature = "json")]
impl<Size: size::Size> JsonShared<Size> {
fn skip_details(details: &Option<HardlinkListReflection<Size>>) -> bool {
details
.as_ref()
.is_none_or(|reflection| reflection.is_empty())
}
fn skip_summary(summary: &Option<SharedLinkSummary<Size>>) -> bool {
summary
.as_ref()
.is_none_or(|summary| summary == &SharedLinkSummary::default())
}
fn skip(&self) -> bool {
JsonShared::skip_details(&self.details) && JsonShared::skip_summary(&self.summary)
}
}
#[derive(Debug, Clone, Deref, DerefMut)]
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
pub struct JsonTree<Size: size::Size> {
#[deref]
#[deref_mut]
pub tree: DataTreeReflection<String, Size>,
#[cfg_attr(
feature = "json",
serde(default, skip_serializing_if = "JsonShared::skip")
)]
pub shared: JsonShared<Size>,
}
#[derive(Debug, Clone, From, TryInto)]
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "json", serde(tag = "unit"))]
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
pub enum JsonDataBody {
Bytes(JsonTree<Bytes>),
Blocks(JsonTree<Blocks>),
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "json", serde(rename_all = "kebab-case"))]
pub struct JsonData {
pub schema_version: SchemaVersion,
#[cfg_attr(feature = "json", serde(rename = "pdu"))]
pub binary_version: Option<BinaryVersion>,
#[cfg_attr(feature = "json", serde(flatten))]
pub body: JsonDataBody,
}