mjcf_rs/error.rs
1use std::path::PathBuf;
2
3/// Errors that can be returned while parsing an MJCF document.
4#[derive(thiserror::Error, Debug)]
5pub enum ParseError {
6 /// The XML failed to parse.
7 #[error("XML parse error: {0}")]
8 Xml(#[from] roxmltree::Error),
9
10 /// I/O error while reading a file (or one of its `<include>` targets).
11 #[error("I/O error reading {path:?}: {source}")]
12 Io {
13 /// Path that failed.
14 path: PathBuf,
15 /// Underlying I/O error.
16 #[source]
17 source: std::io::Error,
18 },
19
20 /// An attribute couldn't be parsed.
21 #[error("invalid attribute `{attr}` on <{tag}>: {message}")]
22 BadAttribute {
23 /// Tag name.
24 tag: String,
25 /// Attribute name.
26 attr: String,
27 /// Reason it failed.
28 message: String,
29 },
30
31 /// An attribute had an unexpected value.
32 #[error("unexpected `{attr}=\"{value}\"` on <{tag}>: {message}")]
33 BadAttributeValue {
34 /// Tag name.
35 tag: String,
36 /// Attribute name.
37 attr: String,
38 /// Bad value.
39 value: String,
40 /// Reason.
41 message: String,
42 },
43
44 /// `<include>` referenced a file that wasn't found, or formed a cycle.
45 #[error("invalid <include file=\"{file:?}\">: {message}")]
46 BadInclude {
47 /// Filename.
48 file: PathBuf,
49 /// Reason.
50 message: String,
51 },
52
53 /// `<element class="…">` referenced a class that doesn't exist.
54 #[error("element <{tag}> referenced unknown <default class=\"{class}\">")]
55 UnknownClass {
56 /// Tag name.
57 tag: String,
58 /// Class name.
59 class: String,
60 },
61
62 /// A feature is parsed but not supported by this version of the loader.
63 #[error("unsupported MJCF feature `{feature}`{}", hint.as_ref().map(|h| format!(": {h}")).unwrap_or_default())]
64 Unsupported {
65 /// What was unsupported.
66 feature: String,
67 /// Optional hint.
68 hint: Option<String>,
69 },
70
71 /// A body referenced a parent that wasn't found.
72 #[error("invalid model: {0}")]
73 InvalidModel(String),
74}