1use std::io;
2
3use zerocopy::{Immutable, KnownLayout, TryFromBytes, ValidityError, try_transmute};
4
5use super::HeaderFlags;
6
7#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Immutable, KnownLayout, TryFromBytes)]
8#[repr(u8)]
9pub enum AutoBool {
10 #[default]
11 Auto = 0,
12 No = 1,
13 Yes = 2,
14}
15
16impl AutoBool {
17 pub fn try_read_from_io<R>(mut src: R) -> io::Result<Self>
18 where
19 Self: Sized,
20 R: io::Read,
21 {
22 let mut buf = [0; size_of::<Self>()];
23 src.read_exact(&mut buf)?;
24 Self::try_read_from_bytes(&buf)
25 .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err.to_string()))
26 }
27
28 #[must_use]
29 pub const fn from_header_flags(flags: &HeaderFlags, flag: HeaderFlags) -> Self {
30 if flags.contains(flag) {
31 Self::Yes
32 } else {
33 Self::No
34 }
35 }
36
37 #[must_use]
38 pub const fn as_str(&self) -> &'static str {
39 match self {
40 Self::Auto => "Auto",
41 Self::No => "No",
42 Self::Yes => "Yes",
43 }
44 }
45}
46
47impl From<bool> for AutoBool {
48 fn from(value: bool) -> Self {
49 if value { Self::Yes } else { Self::No }
50 }
51}
52
53impl TryFrom<u8> for AutoBool {
54 type Error = ValidityError<u8, Self>;
55
56 fn try_from(value: u8) -> Result<Self, Self::Error> {
57 try_transmute!(value)
58 }
59}