use super::Property;
use crate::{
helpers::{FallibleNode, FallibleRoot},
parsing::{unaligned::UnalignedParser, Parser, ParserWithMode, StringsBlock, StructsBlock},
FdtError,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CellSizes {
#[allow(missing_docs)]
pub address_cells: usize,
#[allow(missing_docs)]
pub size_cells: usize,
}
impl<'a, P: ParserWithMode<'a>> Property<'a, P> for CellSizes {
#[inline]
fn parse(node: FallibleNode<'a, P>, _: FallibleRoot<'a, P>) -> Result<Option<Self>, FdtError> {
let (mut address_cells, mut size_cells) = (None, None);
for property in node.properties()? {
let property = property?;
let mut parser = UnalignedParser::new(property.value, StringsBlock(&[]), StructsBlock(&[]));
match property.name {
"#address-cells" => address_cells = Some(parser.advance_u32()?.to_ne() as usize),
"#size-cells" => size_cells = Some(parser.advance_u32()?.to_ne() as usize),
_ => {}
}
}
Ok(address_cells.zip(size_cells).map(|(address_cells, size_cells)| CellSizes { address_cells, size_cells }))
}
}
impl Default for CellSizes {
fn default() -> Self {
CellSizes { address_cells: 2, size_cells: 1 }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AddressCells(pub usize);
impl<'a, P: ParserWithMode<'a>> Property<'a, P> for AddressCells {
#[inline]
fn parse(node: FallibleNode<'a, P>, _: FallibleRoot<'a, P>) -> Result<Option<Self>, FdtError> {
match node.properties()?.find("#address-cells")? {
Some(value) => Ok(Some(Self(value.as_value()?))),
None => Ok(None),
}
}
}
impl Default for AddressCells {
fn default() -> Self {
Self(2)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SizeCells(pub usize);
impl<'a, P: ParserWithMode<'a>> Property<'a, P> for SizeCells {
#[inline]
fn parse(node: FallibleNode<'a, P>, _: FallibleRoot<'a, P>) -> Result<Option<Self>, FdtError> {
match node.properties()?.find("#size-cells")? {
Some(value) => Ok(Some(Self(value.as_value()?))),
None => Ok(None),
}
}
}
impl Default for SizeCells {
fn default() -> Self {
Self(1)
}
}