///|
/// URDF parse diagnostics.
///|
#derive(Debug, Eq)
/// Structured details of a URDF parse failure. `line`/`col` are 1-based when
/// the parser could locate the offending element; `snippet` carries the raw
/// source text around the error when available.
pub(all) struct UrdfParseErrorInfo {
/// Human-readable description of what failed to parse.
message : String
/// 1-based line number in the source, when known.
line : Int32?
/// 1-based column number in the source, when known.
col : Int32?
/// Excerpt of the source around the error, when available.
snippet : String?
}
///|
/// Error raised by the URDF parser. Carries a structured `UrdfParseErrorInfo`;
/// format it for display with `format_urdf_parse_error`.
pub suberror UrdfParseError {
UrdfParseError(UrdfParseErrorInfo)
}
///|
/// Outcome of a non-raising URDF parse: either the parsed `Robot` or
/// structured error details. Returned by `parse_urdf_from_string_result` for
/// callers that prefer values over `raise`.
pub(all) enum ParseUrdfResult {
Ok(Robot)
Err(UrdfParseErrorInfo)
}
///|
/// Formats a parse error as `@line:col: message`, plus an optional snippet.
pub fn format_urdf_parse_error(info : UrdfParseErrorInfo) -> String {
let pos_prefix = match (info.line, info.col) {
(Some(l), Some(c)) => "@" + l.to_string() + ":" + c.to_string() + ": "
_ => ""
}
let header = pos_prefix + info.message
match info.snippet {
None => header
Some(s) => {
let lines = s.replace_all(old="\n", new="\n ")
header + "\nSnippet:\n " + lines
}
}
}