1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::*;
#[derive(Clone, Debug)]
pub struct Asset {
pub contributor: Vec<Contributor>,
pub created: String,
pub keywords: Vec<String>,
pub modified: String,
pub revision: Option<String>,
pub subject: Option<String>,
pub title: Option<String>,
pub unit: Option<Unit>,
pub up_axis: UpAxis,
}
impl XNode for Asset {
const NAME: &'static str = "asset";
fn parse_box(element: &Element) -> Result<Box<Self>> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = Box::new(Asset {
contributor: Contributor::parse_list(&mut it)?,
created: parse_one("created", &mut it, parse_text)?,
keywords: parse_opt("keywords", &mut it, parse_text)?.map_or_else(Vec::new, |s| {
s.split_ascii_whitespace().map(|s| s.to_owned()).collect()
}),
modified: parse_one("modified", &mut it, parse_text)?,
revision: parse_opt("revision", &mut it, parse_text)?,
subject: parse_opt("subject", &mut it, parse_text)?,
title: parse_opt("title", &mut it, parse_text)?,
unit: Unit::parse_opt(&mut it)?,
up_axis: UpAxis::parse_opt(&mut it)?.unwrap_or_default(),
});
finish(res, it)
}
fn parse(element: &Element) -> Result<Self> {
Ok(*Self::parse_box(element)?)
}
}
#[derive(Clone, Default, Debug)]
pub struct Contributor {
pub author: Option<String>,
pub authoring_tool: Option<String>,
pub comments: Option<String>,
pub copyright: Option<String>,
pub source_data: Option<Url>,
}
impl XNode for Contributor {
const NAME: &'static str = "contributor";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
let mut it = element.children().peekable();
let res = Contributor {
author: parse_opt("author", &mut it, parse_text)?,
authoring_tool: parse_opt("authoring_tool", &mut it, parse_text)?,
comments: parse_opt("comments", &mut it, parse_text)?,
copyright: parse_opt("copyright", &mut it, parse_text)?,
source_data: parse_opt("source_data", &mut it, parse_elem)?,
};
finish(res, it)
}
}
#[derive(Clone, Debug)]
pub struct Unit {
pub name: Option<String>,
pub meter: f32,
}
impl Default for Unit {
fn default() -> Self {
Unit {
name: None,
meter: 1.,
}
}
}
impl XNode for Unit {
const NAME: &'static str = "unit";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
Ok(Unit {
name: element.attr("name").map(Into::into),
meter: match element.attr("meter") {
None => 1.,
Some(s) => s.parse().map_err(|_| "parse error")?,
},
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UpAxis {
XUp,
YUp,
ZUp,
}
impl Default for UpAxis {
fn default() -> Self {
Self::YUp
}
}
impl XNode for UpAxis {
const NAME: &'static str = "up_axis";
fn parse(element: &Element) -> Result<Self> {
debug_assert_eq!(element.name(), Self::NAME);
Ok(match get_text(element) {
Some("X_UP") => UpAxis::XUp,
Some("Y_UP") => UpAxis::YUp,
Some("Z_UP") => UpAxis::ZUp,
_ => return Err("invalid <up_axis> value".into()),
})
}
}