use super::ShapeDesc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Field {
pub name: &'static str,
pub shape: ShapeDesc,
pub offset: usize,
pub flags: FieldFlags,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FieldFlags(u64);
impl FieldFlags {
pub const EMPTY: Self = Self(0);
pub const SENSITIVE: Self = Self(1 << 0);
#[inline]
pub fn contains(&self, flag: FieldFlags) -> bool {
self.0 & flag.0 != 0
}
#[inline]
pub fn set_flag(&mut self, flag: FieldFlags) -> &mut Self {
self.0 |= flag.0;
self
}
#[inline]
pub fn unset_flag(&mut self, flag: FieldFlags) -> &mut Self {
self.0 &= !flag.0;
self
}
#[inline]
pub const fn with_flag(flag: FieldFlags) -> Self {
Self(flag.0)
}
}
impl std::ops::BitOr for FieldFlags {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl std::ops::BitOrAssign for FieldFlags {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl Default for FieldFlags {
#[inline(always)]
fn default() -> Self {
Self::EMPTY
}
}
impl std::fmt::Display for FieldFlags {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.0 == 0 {
return write!(f, "none");
}
let flags = [
(Self::SENSITIVE.0, "sensitive"),
];
let mut is_first = true;
for (bit, name) in flags {
if self.0 & bit != 0 {
if !is_first {
write!(f, ", ")?;
}
is_first = false;
write!(f, "{}", name)?;
}
}
Ok(())
}
}