#[macro_export]
macro_rules! lcf_struct {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident<$lt:lifetime> indexed { $($body:tt)* }
) => {
$crate::lcf_struct!(@def $(#[$meta])* $vis $name $lt [<$lt>] [$name<$lt>] [static]
[
pub id: i32,
]
[ id: 0, ] [indexed]
{ $($body)* });
$crate::lcf_struct!(@indexed $lt [$name<$lt>]);
};
(
$(#[$meta:meta])*
$vis:vis struct $name:ident<$lt:lifetime> { $($body:tt)* }
) => {
$crate::lcf_struct!(@def $(#[$meta])* $vis $name $lt [<$lt>] [$name<$lt>] [static] [] [] []
{ $($body)* });
};
(
$(#[$meta:meta])*
$vis:vis struct $name:ident indexed { $($body:tt)* }
) => {
$crate::lcf_struct!(@def $(#[$meta])* $vis $name 'lcf [] [$name] []
[
pub id: i32,
]
[ id: 0, ] [indexed]
{ $($body)* });
$crate::lcf_struct!(@indexed 'lcf [$name]);
};
(
$(#[$meta:meta])*
$vis:vis struct $name:ident { $($body:tt)* }
) => {
$crate::lcf_struct!(@def $(#[$meta])* $vis $name 'lcf [] [$name] [] [] [] []
{ $($body)* });
};
(@indexed $lt:lifetime [$($ty:tt)*]) => {
impl<$lt> $crate::field::LcfIndexed<$lt> for $($ty)* {
#[inline]
fn id(&self) -> i32 {
self.id
}
#[inline]
fn set_id(&mut self, id: i32) {
self.id = id;
}
}
};
(@def
$(#[$meta:meta])*
$vis:vis $name:ident $lt:lifetime
[$($gen:tt)*] [$($ty:tt)*] [$($owned_lt:tt)*]
[ $($id_field:tt)* ]
[ $($id_init:tt)* ]
[ $($id_marker:tt)* ]
{
$(
$(#[$fmeta:meta])*
$id:literal $fname:ident : $fty:ty
$(= $default:expr)?
$(, $flag:ident $(= $fval:literal)? )*
),* $(,)?
}
) => {
$(#[$meta])*
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, Debug, PartialEq)]
$vis struct $name$($gen)* {
$($id_field)*
$(
$(#[$fmeta])*
pub $fname: $fty,
)*
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "serde",
serde(skip_serializing_if = "rm2k::field::UnknownChunks::is_empty"))]
pub unknown_chunks: $crate::field::UnknownChunks,
}
impl<$lt> $crate::field::LcfStruct<$lt> for $($ty)* {
const NAME: &'static str = stringify!($name);
#[allow(unused_variables)]
fn defaults(engine: $crate::engine::Engine) -> Self {
Self {
$($id_init)*
$(
$fname: $crate::lcf_struct!(@default engine, $fty $(, $default)?),
)*
unknown_chunks: $crate::field::UnknownChunks::new(),
}
}
fn apply_chunk(
&mut self,
id: u32,
data: &$lt [u8],
ctx: &mut $crate::cursor::Ctx<'_>,
) -> ::core::option::Option<u32> {
match id {
$(
$id => {
self.$fname = <$fty as $crate::field::LcfField<$lt>>::read(
data, ctx, <Self as $crate::field::LcfStruct>::NAME, $id,
);
::core::option::Option::Some($id)
}
)*
_ => {
let _ = (data, ctx);
$($(
if $crate::lcf_struct!(@is_companion id, $flag $(= $fval)?) {
return ::core::option::Option::Some($id);
}
)*)*
::core::option::Option::None
}
}
}
#[inline]
fn unknown_chunks(&self) -> &$crate::field::UnknownChunks {
&self.unknown_chunks
}
#[inline]
fn unknown_chunks_mut(&mut self) -> &mut $crate::field::UnknownChunks {
&mut self.unknown_chunks
}
#[allow(unused_variables)]
fn write_body<S: $crate::sink::Sink>(
&self,
sink: &mut S,
engine: $crate::engine::Engine,
) -> ::core::result::Result<(), S::Error> {
self.unknown_chunks.write_anchored(sink, 0)?;
$(
{
let allowed = true
$( && $crate::lcf_struct!(@allow engine, $flag $(= $fval)?) )*;
if allowed {
let fdefault =
$crate::lcf_struct!(@default engine, $fty $(, $default)?);
$(
$crate::lcf_struct!(@companion
sink, self, fdefault, engine, $fname, $flag $(= $fval)?);
)*
let force = false
$( || $crate::lcf_struct!(@force engine, $flag $(= $fval)?) )*;
if force || $crate::field::LcfField::lcf_differs(&self.$fname, &fdefault) {
let len =
$crate::field::LcfField::lcf_size(&self.$fname, engine);
$crate::wire::write_varint(sink, $id)?;
$crate::wire::write_varint(sink, len)?;
$crate::field::LcfField::write(&self.$fname, sink, engine)?;
}
}
self.unknown_chunks.write_anchored(sink, $id)?;
}
)*
::core::result::Result::Ok(())
}
}
$crate::lcf_struct!(@owned_impl
$lt [$($ty)*] [$($owned_lt)*] $name [$($id_marker)*] [$($fname),*]);
};
(@owned_impl $lt:lifetime [$($ty:tt)*] [$($owned_lt:tt)*] $n:ident [indexed] [$($f:ident),*]) => {
impl<$lt> $crate::owned::IntoOwned for $($ty)* {
type Owned = $crate::lcf_struct!(@owned_ty $n $($owned_lt)*);
fn into_owned(self) -> Self::Owned {
$n {
id: self.id,
$( $f: $crate::owned::IntoOwned::into_owned(self.$f), )*
unknown_chunks: self.unknown_chunks,
}
}
}
};
(@owned_impl $lt:lifetime [$($ty:tt)*] [$($owned_lt:tt)*] $n:ident [] [$($f:ident),*]) => {
impl<$lt> $crate::owned::IntoOwned for $($ty)* {
type Owned = $crate::lcf_struct!(@owned_ty $n $($owned_lt)*);
fn into_owned(self) -> Self::Owned {
$n {
$( $f: $crate::owned::IntoOwned::into_owned(self.$f), )*
unknown_chunks: self.unknown_chunks,
}
}
}
};
(@owned_ty $n:ident static) => { $n<'static> };
(@owned_ty $n:ident) => { $n };
(@default $e:ident, $fty:ty, $default:expr) => { $default };
(@default $e:ident, $fty:ty) => {
<$fty as $crate::field::LcfField<'_>>::field_default($e)
};
(@allow $e:ident, is2k3) => { $e.is_2k3() };
(@allow $e:ident, persist) => { true };
(@allow $e:ident, persist2k) => { true };
(@allow $e:ident, db_version) => { true };
(@allow $e:ident, size = $c:literal) => { true };
(@allow $e:ident, count = $c:literal) => { true };
(@allow $e:ident, size_persist = $c:literal) => { true };
(@allow $e:ident, count_persist = $c:literal) => { true };
(@force $e:ident, persist) => { true };
(@force $e:ident, persist2k) => { !$e.is_2k3() };
(@force $e:ident, db_version) => { $e.is_2k3() };
(@force $e:ident, is2k3) => { false };
(@force $e:ident, size = $c:literal) => { false };
(@force $e:ident, count = $c:literal) => { false };
(@force $e:ident, size_persist = $c:literal) => { false };
(@force $e:ident, count_persist = $c:literal) => { false };
(@is_companion $id:ident, size = $c:literal) => { $id == $c };
(@is_companion $id:ident, count = $c:literal) => { $id == $c };
(@is_companion $id:ident, size_persist = $c:literal) => { $id == $c };
(@is_companion $id:ident, count_persist = $c:literal) => { $id == $c };
(@is_companion $id:ident, persist) => { false };
(@is_companion $id:ident, persist2k) => { false };
(@is_companion $id:ident, db_version) => { false };
(@is_companion $id:ident, is2k3) => { false };
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, size = $c:literal) => {
if $crate::field::LcfField::element_count(&$self.$f)
!= $crate::field::LcfField::element_count(&$d)
{
$crate::lcf_struct!(@emit_companion $s, $c,
$crate::field::LcfField::lcf_size(&$self.$f, $e));
}
};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, size_persist = $c:literal) => {
$crate::lcf_struct!(@emit_companion $s, $c,
$crate::field::LcfField::lcf_size(&$self.$f, $e));
};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, count = $c:literal) => {
if $crate::field::LcfField::element_count(&$self.$f)
!= $crate::field::LcfField::element_count(&$d)
{
$crate::lcf_struct!(@emit_companion $s, $c,
$crate::field::LcfField::element_count(&$self.$f));
}
};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, count_persist = $c:literal) => {
$crate::lcf_struct!(@emit_companion $s, $c,
$crate::field::LcfField::element_count(&$self.$f));
};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, persist) => {};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, persist2k) => {};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, db_version) => {};
(@companion $s:ident, $self:ident, $d:ident, $e:ident, $f:ident, is2k3) => {};
(@emit_companion $s:ident, $c:literal, $payload:expr) => {{
let payload = $payload;
$crate::wire::write_varint($s, $c)?;
$crate::wire::write_varint($s, $crate::wire::varint_len(payload))?;
$crate::wire::write_varint($s, payload)?;
}};
}
#[macro_export]
macro_rules! lcf_flags {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident {
$(
$(#[$fmeta:meta])*
$fname:ident $($f2k3:ident)?
),* $(,)?
}
) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
$vis struct $name {
$(
$(#[$fmeta])*
pub $fname: bool,
)*
}
$crate::impl_identity_owned!($name);
impl $name {
pub const DEFAULT: Self = Self { $( $fname: false, )* };
}
impl<'lcf> $crate::field::LcfField<'lcf> for $name {
#[inline]
fn field_default(_engine: $crate::engine::Engine) -> Self {
Self::DEFAULT
}
fn read(
data: &'lcf [u8],
_ctx: &mut $crate::cursor::Ctx<'_>,
_owner: &'static str,
_id: u32,
) -> Self {
let mut out = Self::default();
let mut bit = 0usize;
$(
out.$fname = data
.get(bit / 8)
.is_some_and(|byte| (byte >> (bit % 8)) & 1 != 0);
bit += 1;
)*
let _ = bit;
out
}
#[allow(unused_variables)]
fn lcf_size(&self, engine: $crate::engine::Engine) -> u32 {
let mut bits = 0u32;
$(
if $crate::lcf_flags!(@allow engine $(, $f2k3)?) {
bits += 1;
}
)*
bits.div_ceil(8)
}
#[allow(unused_variables)]
fn write<S: $crate::sink::Sink>(
&self,
sink: &mut S,
engine: $crate::engine::Engine,
) -> ::core::result::Result<(), S::Error> {
let mut byte = 0u8;
let mut bit = 0u32;
$(
if $crate::lcf_flags!(@allow engine $(, $f2k3)?) {
byte |= u8::from(self.$fname) << bit;
bit += 1;
if bit == 8 {
sink.write(&[byte])?;
byte = 0;
bit = 0;
}
}
)*
if bit != 0 {
sink.write(&[byte])?;
}
::core::result::Result::Ok(())
}
}
};
(@allow $e:ident, is2k3) => { $e.is_2k3() };
(@allow $e:ident) => { true };
}
#[macro_export]
macro_rules! lcf_enum {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident {
$(
$(#[$vmeta:meta])*
$vname:ident = $value:literal
),* $(,)?
}
) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
$vis struct $name(pub i32);
$crate::impl_identity_owned!($name);
impl $name {
$(
$(#[$vmeta])*
pub const $vname: Self = Self($value);
)*
#[must_use]
#[inline]
pub const fn get(self) -> i32 {
self.0
}
#[must_use]
pub const fn name(self) -> Option<&'static str> {
match self.0 {
$( $value => Some(stringify!($vname)), )*
_ => None,
}
}
}
impl From<i32> for $name {
#[inline]
fn from(value: i32) -> Self {
Self(value)
}
}
impl<'lcf> $crate::field::LcfField<'lcf> for $name {
#[inline]
fn field_default(engine: $crate::engine::Engine) -> Self {
Self(<i32 as $crate::field::LcfField<'lcf>>::field_default(engine))
}
#[inline]
fn read(
data: &'lcf [u8],
ctx: &mut $crate::cursor::Ctx<'_>,
owner: &'static str,
id: u32,
) -> Self {
Self(<i32 as $crate::field::LcfField<'lcf>>::read(data, ctx, owner, id))
}
#[inline]
fn lcf_size(&self, engine: $crate::engine::Engine) -> u32 {
$crate::field::LcfField::lcf_size(&self.0, engine)
}
#[inline]
fn write<S: $crate::sink::Sink>(
&self,
sink: &mut S,
engine: $crate::engine::Engine,
) -> ::core::result::Result<(), S::Error> {
$crate::field::LcfField::write(&self.0, sink, engine)
}
}
};
}
#[macro_export]
macro_rules! lcf_strings {
($name:ident<$lt:lifetime> { $($field:ident),* $(,)? }) => {
impl<$lt> $crate::field::Strings<$lt> for $name<$lt> {
fn visit_strings(
&self,
f: &mut dyn FnMut(&'static str, &$crate::field::DbStr<$lt>),
) {
$( f(stringify!($field), &self.$field); )*
}
}
};
}