update(Register): add type field and its completion to converter

This commit is contained in:
Wataru Otsubo 2025-02-01 19:30:39 +09:00
parent 8e0761e39e
commit eec855c3a1
2 changed files with 39 additions and 1 deletions

View file

@ -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,

View file

@ -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<u32>,
/// 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<String>
pub desc: Option<String>,
}
#[derive(Debug, PartialEq)]