use zerocopy::{Immutable, KnownLayout, TryFromBytes};
#[repr(C)]
#[derive(Copy, Clone, Debug, KnownLayout, Immutable, TryFromBytes)]
pub(crate) struct FlagParameters(pub(super) u8);
pub(crate) enum DataShape {
Y,
XY,
YY,
XYY,
XYXY,
}
#[derive(Copy, Clone, Debug)]
pub(crate) enum Precision {
SixteenBit,
ThirtyTwoBit,
}
impl Precision {
pub(crate) fn bytes_per_point(&self) -> usize {
match self {
Precision::SixteenBit => 2,
Precision::ThirtyTwoBit => 4,
}
}
}
impl FlagParameters {
pub(crate) fn y_precision(&self) -> Precision {
if (self.0 & 1) == 1 {
Precision::SixteenBit
} else {
Precision::ThirtyTwoBit
}
}
fn use_fexper_extension(&self) -> bool {
((self.0 >> 1) & 1) == 1
}
pub(super) fn multifile(&self) -> bool {
((self.0 >> 2) & 1) == 1
}
fn z_values_are_random(&self) -> bool {
((self.0 >> 3) & 1) == 1
}
fn z_values_are_uneven(&self) -> bool {
((self.0 >> 4) & 1) == 1
}
fn custom_axis_labels(&self) -> bool {
((self.0 >> 5) & 1) == 1
}
fn xyxy(&self) -> bool {
((self.0 >> 6) & 1) == 1
}
pub(super) fn xy(&self) -> bool {
((self.0 >> 7) & 1) == 1
}
pub(crate) fn data_shape(&self) -> DataShape {
if !self.multifile() {
if !self.xy() {
return DataShape::Y;
} else if self.xyxy() {
panic!("exception in datashape creation")
} else {
return DataShape::XY;
}
}
if !self.xy() {
DataShape::YY
} else {
if !self.xy() {
DataShape::XYY
} else {
DataShape::XYXY
}
}
}
}
impl ::std::fmt::Display for FlagParameters {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
f,
"y precision: {} bit",
self.y_precision().bytes_per_point() * 8
)?;
writeln!(f, "fexper: {}", self.use_fexper_extension())?;
writeln!(f, "multifile data: {}", self.multifile())?;
writeln!(f, "random z: {}", self.z_values_are_random())?;
writeln!(f, "uneven z: {}", self.z_values_are_uneven())?;
writeln!(f, "custom axis labels: {}", self.custom_axis_labels())?;
writeln!(f, "xyxy: {}", self.xyxy())?;
writeln!(f, "xy: {}", self.xy())?;
Ok(())
}
}