diff --git a/src/converter.rs b/src/converter.rs index 0334bb7..4d0c964 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -1,4 +1,8 @@ //! Convert DOM to register interface defined in [`crate::types`], complementing optional parameters. +//! +//! root: [`Module::from_xml_dom`] +//! +//! error: [`DomConversionError`] use std::{num, str}; @@ -11,6 +15,7 @@ use crate::types::{ Value, }; +/// Possible errors in conversion, with positional information. #[derive(Debug, Error, PartialEq)] pub enum DomConversionError { #[error("attribute {attr} not found in element: {start} - {end}", start = pos.0, end = pos.1)] @@ -323,6 +328,18 @@ impl Register { .map_err(|e| DomConversionError::parse_prefixed_u32_error(e, "addr", node))?, None => 0, }; + let r#type = util::get_type(node) + .transpose()? + .map_or_else( + || { + node.ancestors() + .filter_map(util::get_type) + .next() + .transpose() + }, + |x| Ok(Some(x)), + )? + .ok_or_else(|| DomConversionError::parameter_completion_error("type", node))?; let mask = node .attribute("mask") .map(|addr| { @@ -366,10 +383,29 @@ impl Register { node.document().text_pos_at(node.range().end), ))); } + if default.is_some() && !children.is_empty() { + return Err(DomConversionError::OtherError(format!( + "both default and field are used in the same register: {} - {}", + node.document().text_pos_at(node.range().start), + node.document().text_pos_at(node.range().end), + ))); + } + if let (Some(mask), Some(default)) = (mask, default) { + if default & !(mask) != 0 { + log::warn!( + "default value {} doesn't fit mask {}: {} - {}", + default, + mask, + node.document().text_pos_at(node.range().start), + node.document().text_pos_at(node.range().end) + ) + } + } Ok(Register { name, addr, + r#type, mask, modf, multiple, diff --git a/src/types.rs b/src/types.rs index c3bfd3f..4bc205e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -53,6 +53,8 @@ pub struct Register { pub name: String, /// Fill this with proper calc. pub addr: u32, + /// Fill this with proper calc. + pub r#type: DataType, pub mask: Option, /// Fill this with proper calc. pub modf: RwSpecifier, @@ -111,7 +113,7 @@ pub struct Field { pub struct Value { pub name: String, pub data: u32, - pub desc: Option + pub desc: Option, } #[derive(Debug, PartialEq)]