use serde::{Deserialize, Serialize};
use crate::component::ComponentId;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct FragmentId(String);
impl FragmentId {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct FragmentKind(String);
impl FragmentKind {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Default,
)]
pub struct FragmentOrder(i64);
impl FragmentOrder {
#[must_use]
pub const fn new(value: i64) -> Self {
Self(value)
}
#[must_use]
pub const fn value(self) -> i64 {
self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct FragmentDeclaration {
pub id: FragmentId,
pub kind: FragmentKind,
pub content: Vec<u8>,
pub order: FragmentOrder,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct FragmentKey {
pub component_id: ComponentId,
pub fragment_id: FragmentId,
}
impl FragmentKey {
#[must_use]
pub fn canonical_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(32 + self.fragment_id.as_bytes().len());
bytes.extend_from_slice(self.component_id.as_bytes());
bytes.extend_from_slice(self.fragment_id.as_bytes());
bytes
}
}
impl Ord for FragmentKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.component_id
.as_bytes()
.cmp(other.component_id.as_bytes())
.then_with(|| {
self.fragment_id
.as_bytes()
.cmp(other.fragment_id.as_bytes())
})
}
}
impl PartialOrd for FragmentKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ComponentIncarnation(u64);
impl ComponentIncarnation {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn ordinal(self) -> u64 {
self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct FragmentRow {
pub key: FragmentKey,
pub kind: FragmentKind,
pub order: FragmentOrder,
pub content: Vec<u8>,
pub incarnation: ComponentIncarnation,
}