Skip to content

Commit a8c7f0b

Browse files
committed
Move make_bitfield_serde to struct_accessors.
Fixes <#159>.
1 parent efa1b19 commit a8c7f0b

File tree

2 files changed

+88
-84
lines changed

2 files changed

+88
-84
lines changed

src/ondisk.rs

+4-84
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#![allow(clippy::new_without_default)]
1111

1212
pub use crate::naples::{ParameterTimePoint, ParameterTokenConfig};
13-
use crate::struct_accessors::{Getter, Setter, make_accessors};
13+
use crate::struct_accessors::{
14+
Getter, Setter, make_accessors, make_bitfield_serde,
15+
};
1416
use crate::token_accessors::{Tokens, TokensMut, make_token_accessors};
1517
use crate::types::Error;
1618
use crate::types::PriorityLevel;
@@ -1243,88 +1245,6 @@ impl Default for GROUP_HEADER {
12431245
}
12441246
}
12451247

1246-
/// A variant of the make_accessors macro for modular_bitfields.
1247-
macro_rules! make_bitfield_serde {(
1248-
$(#[$struct_meta:meta])*
1249-
$struct_vis:vis
1250-
struct $StructName:ident {
1251-
$(
1252-
$(#[$field_meta:meta])*
1253-
$field_vis:vis
1254-
$field_name:ident
1255-
$(|| $(#[$serde_field_orig_meta:meta])* $serde_ty:ty : $field_orig_ty:ty)?
1256-
$(: $field_ty:ty)?
1257-
$(| $getter_vis:vis get $field_user_ty:ty $(: $setter_vis:vis set $field_setter_user_ty:ty)?)?
1258-
),* $(,)?
1259-
}
1260-
) => {
1261-
$(#[$struct_meta])*
1262-
$struct_vis
1263-
struct $StructName {
1264-
$(
1265-
$(#[$field_meta])*
1266-
$field_vis
1267-
$($field_name : $field_ty,)?
1268-
$($field_name : $field_orig_ty,)?
1269-
)*
1270-
}
1271-
1272-
impl $StructName {
1273-
pub fn builder() -> Self {
1274-
Self::new() // NOT default
1275-
}
1276-
pub fn build(&self) -> Self {
1277-
self.clone()
1278-
}
1279-
}
1280-
1281-
#[cfg(feature = "serde")]
1282-
impl $StructName {
1283-
$(
1284-
paste!{
1285-
$(
1286-
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
1287-
-> Result<$field_ty> {
1288-
Ok(self.$field_name())
1289-
}
1290-
)?
1291-
$(
1292-
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
1293-
-> Result<$serde_ty> {
1294-
Ok(self.$field_name().into())
1295-
}
1296-
)?
1297-
$(
1298-
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $field_ty) -> &mut Self {
1299-
self.[<set_ $field_name>](value.into());
1300-
self
1301-
}
1302-
)?
1303-
$(
1304-
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $serde_ty) -> &mut Self {
1305-
self.[<set_ $field_name>](value.into());
1306-
self
1307-
}
1308-
)?
1309-
}
1310-
)*
1311-
}
1312-
1313-
#[cfg(feature = "serde")]
1314-
paste::paste! {
1315-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1316-
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
1317-
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1318-
#[cfg_attr(feature = "serde", serde(rename = "" $StructName))]
1319-
pub(crate) struct [<Serde $StructName>] {
1320-
$(
1321-
$(pub $field_name : <$field_ty as Specifier>::InOut,)?
1322-
$($(#[$serde_field_orig_meta])* pub $field_name : $serde_ty,)?
1323-
)*
1324-
}
1325-
}
1326-
}}
1327-
13281248
make_bitfield_serde! {
13291249
#[bitfield(bits = 8)]
13301250
#[repr(u8)]
@@ -1512,7 +1432,7 @@ pub mod gnb {
15121432
use super::{
15131433
BitfieldSpecifier, EntryCompatible, EntryId, FromBytes, FromPrimitive,
15141434
Getter, GnbEntryId, Immutable, IntoBytes, KnownLayout, Result, Setter,
1515-
ToPrimitive, Unaligned, paste,
1435+
ToPrimitive, Unaligned, make_bitfield_serde, paste,
15161436
};
15171437
#[cfg(feature = "serde")]
15181438
use super::{Deserialize, SerdeHex8, Serialize};

src/struct_accessors.rs

+84
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,87 @@ macro_rules! make_accessors {(
361361
)}
362362

363363
pub(crate) use make_accessors;
364+
365+
/// A variant of the make_accessors macro for modular_bitfields.
366+
macro_rules! make_bitfield_serde {(
367+
$(#[$struct_meta:meta])*
368+
$struct_vis:vis
369+
struct $StructName:ident {
370+
$(
371+
$(#[$field_meta:meta])*
372+
$field_vis:vis
373+
$field_name:ident
374+
$(|| $(#[$serde_field_orig_meta:meta])* $serde_ty:ty : $field_orig_ty:ty)?
375+
$(: $field_ty:ty)?
376+
$(| $getter_vis:vis get $field_user_ty:ty $(: $setter_vis:vis set $field_setter_user_ty:ty)?)?
377+
),* $(,)?
378+
}
379+
) => {
380+
$(#[$struct_meta])*
381+
$struct_vis
382+
struct $StructName {
383+
$(
384+
$(#[$field_meta])*
385+
$field_vis
386+
$($field_name : $field_ty,)?
387+
$($field_name : $field_orig_ty,)?
388+
)*
389+
}
390+
391+
impl $StructName {
392+
pub fn builder() -> Self {
393+
Self::new() // NOT default
394+
}
395+
pub fn build(&self) -> Self {
396+
self.clone()
397+
}
398+
}
399+
400+
#[cfg(feature = "serde")]
401+
impl $StructName {
402+
$(
403+
paste!{
404+
$(
405+
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
406+
-> Result<$field_ty> {
407+
Ok(self.$field_name())
408+
}
409+
)?
410+
$(
411+
pub(crate) fn [<serde_ $field_name>] (self : &'_ Self)
412+
-> Result<$serde_ty> {
413+
Ok(self.$field_name().into())
414+
}
415+
)?
416+
$(
417+
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $field_ty) -> &mut Self {
418+
self.[<set_ $field_name>](value.into());
419+
self
420+
}
421+
)?
422+
$(
423+
pub(crate) fn [<serde_with_ $field_name>](self : &mut Self, value: $serde_ty) -> &mut Self {
424+
self.[<set_ $field_name>](value.into());
425+
self
426+
}
427+
)?
428+
}
429+
)*
430+
}
431+
432+
#[cfg(feature = "serde")]
433+
paste::paste! {
434+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
435+
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
436+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
437+
#[cfg_attr(feature = "serde", serde(rename = "" $StructName))]
438+
pub(crate) struct [<Serde $StructName>] {
439+
$(
440+
$(pub $field_name : <$field_ty as Specifier>::InOut,)?
441+
$($(#[$serde_field_orig_meta])* pub $field_name : $serde_ty,)?
442+
)*
443+
}
444+
}
445+
}}
446+
447+
pub(crate) use make_bitfield_serde;

0 commit comments

Comments
 (0)