// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

impl<'a> MinByteRange<'a> for CountAll16<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.remainder_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl<'a> FontRead<'a> for CountAll16<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

#[derive(Clone)]
pub struct CountAll16<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> CountAll16<'a> {
    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
    basic_table_impls!(impl_the_methods);

    pub fn some_field(&self) -> u16 {
        let range = self.some_field_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    pub fn remainder(&self) -> &'a [BigEndian<u16>] {
        let range = self.remainder_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    pub fn some_field_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }

    pub fn remainder_byte_range(&self) -> Range<usize> {
        let start = self.some_field_byte_range().end;
        start..start + self.data.len().saturating_sub(start) / u16::RAW_BYTE_LEN * u16::RAW_BYTE_LEN
    }
}

const _: () = assert!(FontData::default_data_long_enough(CountAll16::MIN_SIZE));

impl Default for CountAll16<'_> {
    fn default() -> Self {
        Self {
            data: FontData::default_table_data(),
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for CountAll16<'a> {
    fn type_name(&self) -> &str {
        "CountAll16"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("some_field", self.some_field())),
            1usize => Some(Field::new("remainder", self.remainder())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for CountAll16<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

impl<'a> MinByteRange<'a> for CountAll32<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.remainder_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl<'a> FontRead<'a> for CountAll32<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

#[derive(Clone)]
pub struct CountAll32<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> CountAll32<'a> {
    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
    basic_table_impls!(impl_the_methods);

    pub fn some_field(&self) -> u16 {
        let range = self.some_field_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    pub fn remainder(&self) -> &'a [BigEndian<u32>] {
        let range = self.remainder_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    pub fn some_field_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }

    pub fn remainder_byte_range(&self) -> Range<usize> {
        let start = self.some_field_byte_range().end;
        start..start + self.data.len().saturating_sub(start) / u32::RAW_BYTE_LEN * u32::RAW_BYTE_LEN
    }
}

const _: () = assert!(FontData::default_data_long_enough(CountAll32::MIN_SIZE));

impl Default for CountAll32<'_> {
    fn default() -> Self {
        Self {
            data: FontData::default_table_data(),
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for CountAll32<'a> {
    fn type_name(&self) -> &str {
        "CountAll32"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("some_field", self.some_field())),
            1usize => Some(Field::new("remainder", self.remainder())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for CountAll32<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}
