diff --git a/src/converter.rs b/src/converter.rs index 4d0c964..db5d454 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -54,8 +54,11 @@ pub enum DomConversionError { param: &'static str, pos: (TextPos, TextPos), }, - #[error("other dom conversion error: {0}")] - OtherError(String), + #[error("other dom conversion error: {comment}: {start} - {end}", start = pos.0, end = pos.1)] + OtherError { + comment: &'static str, + pos: (TextPos, TextPos), + }, } impl DomConversionError { @@ -115,6 +118,15 @@ impl DomConversionError { found, } } + + fn other_error(comment: &'static str, node: Node) -> Self { + let range = node.range(); + let doc = node.document(); + Self::OtherError { + comment, + pos: (doc.text_pos_at(range.start), doc.text_pos_at(range.end)), + } + } } mod util { @@ -126,7 +138,11 @@ mod util { pub(crate) fn get_name(node: Node) -> Result { match node.attribute("name") { - Some(name) => Ok(name.to_string()), + Some(name) if !name.is_empty() => Ok(name.to_string()), + Some(_name) => Err(DomConversionError::other_error( + "name cannot be empty", + node, + )), None => Err(DomConversionError::attr_not_found("name", node)), } } @@ -286,8 +302,9 @@ impl Block { .next() { Some(s) => Ok(s.to_string()), - None => Err(DomConversionError::OtherError( - "decoder format is not yet fixed".to_string(), + None => Err(DomConversionError::other_error( + "decoder format is not yet fixed", + node, )), }?, }; @@ -377,18 +394,16 @@ impl Register { // Validation if mask.is_some() && !children.is_empty() { - return Err(DomConversionError::OtherError(format!( - "both mask and field are used in the same register: {} - {}", - node.document().text_pos_at(node.range().start), - node.document().text_pos_at(node.range().end), - ))); + return Err(DomConversionError::other_error( + "both mask and field are used in the same register", + node, + )); } 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), - ))); + return Err(DomConversionError::other_error( + "both default and field are used in the same register", + node, + )); } if let (Some(mask), Some(default)) = (mask, default) { if default & !(mask) != 0 {