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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Parser for FBX 7.4 or later.
use std::fmt;
use std::io::{self, Read};
use crate::{
low::{
v7400::{FbxFooter, NodeHeader},
FbxHeader, FbxVersion,
},
pull_parser::{
error::{DataError, OperationError},
reader::Reader,
v7400::{Event, FromParser, StartNode},
Error, ParserVersion, Result, SyntacticPosition, Warning,
},
};
/// Warning handler type.
type WarningHandler = Box<dyn FnMut(Warning, &SyntacticPosition) -> Result<()>>;
/// Pull parser for FBX 7.4 binary or compatible later versions.
pub struct Parser<R> {
/// Parser state.
state: State,
/// Reader.
reader: Reader<R>,
/// Warning handler.
warning_handler: Option<WarningHandler>,
}
impl<R: io::Read> Parser<R> {
/// Parser version.
pub const PARSER_VERSION: ParserVersion = ParserVersion::V7400;
/// Creates a new `Parser` from the given reader.
///
/// Returns an error if the given FBX version in unsupported.
#[inline]
pub fn from_reader(header: FbxHeader, reader: R) -> Result<Self> {
Parser::create(header.version(), Reader::new(reader, header.len()))
}
/// Creates a new `Parser` from the given seekable reader.
///
/// Returns an error if the given FBX version in unsupported.
#[inline]
pub fn from_seekable_reader(header: FbxHeader, reader: R) -> Result<Self>
where
R: io::Seek,
{
Parser::create(
header.version(),
Reader::with_seekable(reader, header.len()),
)
}
/// Creates a new `Parser`.
///
/// Returns an error if the given FBX version in unsupported.
pub(crate) fn create(fbx_version: FbxVersion, reader: Reader<R>) -> Result<Self> {
if ParserVersion::from_fbx_version(fbx_version) != Some(Self::PARSER_VERSION) {
return Err(
OperationError::UnsupportedFbxVersion(Self::PARSER_VERSION, fbx_version).into(),
);
}
Ok(Self {
state: State::new(fbx_version),
reader,
warning_handler: None,
})
}
/// Sets the warning handler.
///
/// The warning handler will receive warnings and their [syntactic
/// position]s each time the warnings happen.
///
/// If the handler returned `Ok(())`, the warning is considered non-critical
/// and parsing can be continued.
/// If the handler returned `Err(_)`, the warning is considered critical,
/// and the parsing cannot be continued.
///
/// # Examples
///
/// ```no_run
/// # use fbxcel::low::FbxHeader;
/// # let reader = std::io::empty();
/// # let header: FbxHeader = unimplemented!();
/// let mut parser = fbxcel::pull_parser::v7400::Parser::from_reader(header, reader)
/// .expect("Failed to create parser");
/// parser.set_warning_handler(|warning, pos| {
/// // Print warning.
/// eprintln!("WARNING: {} (pos={:?})", warning, pos);
/// // To ignore the warning and continue processing, return `Ok(())`.
/// // To treat the given warning as a critical error, return
/// // `Err(warning.into())`.
/// Ok(())
/// });
/// ```
///
/// [syntactic position]: `SyntacticPosition`
#[inline]
pub fn set_warning_handler<F>(&mut self, warning_handler: F)
where
F: 'static + FnMut(Warning, &SyntacticPosition) -> Result<()>,
{
self.warning_handler = Some(Box::new(warning_handler));
}
/// Returns a mutable reference to the inner reader.
#[inline]
#[must_use]
pub(crate) fn reader(&mut self) -> &mut Reader<R> {
&mut self.reader
}
/// Returns FBX version.
#[inline]
#[must_use]
pub fn fbx_version(&self) -> FbxVersion {
self.state.fbx_version
}
/// Returns the name of the current node.
///
/// # Panics
///
/// This panics if there are no open nodes.
#[inline]
#[must_use]
pub fn current_node_name(&self) -> &str {
self.state
.current_node()
.expect("Implicit top-level node has no name")
.name
.as_str()
}
/// Returns the number of attributes of the current node.
#[inline]
#[must_use]
pub(crate) fn current_attributes_count(&self) -> u64 {
self.state
.current_node()
.expect("Implicit top-level node has no name")
.attributes_count
}
/// Returns current node depth.
///
/// Implicit root node is considered to be depth 0.
#[inline]
#[must_use]
pub fn current_depth(&self) -> usize {
self.state.started_nodes.len()
}
/// Returns `true` if the parser can continue parsing, `false` otherwise.
pub(crate) fn ensure_continuable(&self) -> Result<()> {
match self.state.health() {
Health::Running => Ok(()),
Health::Finished => Err(OperationError::AlreadyFinished.into()),
Health::Aborted(err_pos) => Err(Error::with_position(
OperationError::AlreadyAborted.into(),
err_pos.clone(),
)),
}
}
/// Reads the given type from the underlying reader.
#[inline]
pub(crate) fn parse<T: FromParser>(&mut self) -> Result<T> {
T::read_from_parser(self)
}
/// Passes the given warning to the warning handler.
pub(crate) fn warn(&mut self, warning: Warning, pos: SyntacticPosition) -> Result<()> {
match self.warning_handler {
Some(ref mut handler) => match handler(warning, &pos) {
Ok(()) => Ok(()),
Err(e) => Err(e.and_position(pos)),
},
None => Ok(()),
}
}
/// Returns next event if successfully read.
///
/// You should not call `next_event()` if a parser functionality has been
/// already failed and returned error.
/// If you call `next_event()` with failed parser, error created from
/// [`OperationError::AlreadyAborted`] will be returned.
pub fn next_event(&mut self) -> Result<Event<'_, R>> {
let previous_depth = self.current_depth();
// Precondition: Health should be `Health::Running`.
self.ensure_continuable()?;
// Update health.
let event_kind = match self.next_event_impl() {
Ok(v) => v,
Err(e) => {
let err_pos = self.position();
self.set_aborted(err_pos.clone());
return Err(e.and_position(err_pos));
}
};
if event_kind == EventKind::EndFbx {
self.state.health = Health::Finished;
}
// Update the last event kind.
self.state.last_event_kind = Some(event_kind);
// Postcondition: Depth should be updated correctly.
let current_depth = self.current_depth();
match event_kind {
EventKind::StartNode => {
assert_eq!(
current_depth.wrapping_sub(previous_depth),
1,
"The depth should be incremented on `StartNode`"
);
}
EventKind::EndNode => {
assert_eq!(
previous_depth.wrapping_sub(current_depth),
1,
"The depth should be decremented on `EndNode`"
);
}
EventKind::EndFbx => {
assert_eq!(
previous_depth, 0,
"Depth should be 0 before parsing finishes"
);
assert_eq!(
current_depth, 0,
"Depth should be 0 after parsing is finished"
);
}
}
// Postcondition: The last event kind should be memorized correctly.
assert_eq!(
self.state.last_event_kind(),
Some(event_kind),
"The last event kind should be memorized correctly"
);
// Create the real result.
Ok(match event_kind {
EventKind::StartNode => Event::StartNode(StartNode::new(self)),
EventKind::EndNode => Event::EndNode,
EventKind::EndFbx => {
let footer_res = FbxFooter::read_from_parser(self).map(Box::new);
Event::EndFbx(footer_res)
}
})
}
/// Reads the next node header and changes the parser state (except for
/// parser health and the last event kind).
fn next_event_impl(&mut self) -> Result<EventKind> {
assert_eq!(self.state.health(), &Health::Running);
assert_ne!(self.state.last_event_kind(), Some(EventKind::EndFbx));
// Skip unread attribute of previous node, if exists.
self.skip_unread_attributes()?;
let event_start_offset = self.reader().position();
// Check if the current node ends here (without any marker).
// A node end marker (all-zero node header, which indicates end of the
// current node) is omitted if and only if:
//
// * the node has no children nodes, and
// * the node has one or more attributes.
//
// Note that the check can be skipped for the implicit root node,
// It has always a node end marker at the ending (because it has no
// attributes).
if let Some(current_node) = self.state.current_node() {
if current_node.node_end_offset < event_start_offset {
// The current node has already been ended.
return Err(
DataError::NodeLengthMismatch(current_node.node_end_offset, None).into(),
);
}
if current_node.node_end_offset == event_start_offset {
// `last_event_kind() == Some(EventKind::EndNode)` means that
// some node ends right before the event currently reading.
let has_children = self.state.last_event_kind() == Some(EventKind::EndNode);
let has_attributes = current_node.attributes_count != 0;
if has_children || !has_attributes {
// It's odd, the current node should have a node end marker
// at the ending, but `node_end_offset` data tells that the
// node ends without node end marker.
self.warn(Warning::MissingNodeEndMarker, self.position())?;
}
self.state.started_nodes.pop();
return Ok(EventKind::EndNode);
}
}
// Read node header.
let node_header = NodeHeader::read_from_parser(self)?;
let header_end_offset = self.reader().position();
// Check if a node or a document ends here (with explicit marker).
if node_header.is_node_end() {
// The current node explicitly ends here.
return match self.state.started_nodes.pop() {
Some(closing) => {
if closing.node_end_offset != header_end_offset {
return Err(DataError::NodeLengthMismatch(
closing.node_end_offset,
Some(header_end_offset),
)
.into());
}
if closing.attributes_count != 0 && closing.known_children_count == 0 {
// It's odd, the current node should not have a node end
// marker at the ending, but found.
self.warn(Warning::ExtraNodeEndMarker, self.position())?;
}
Ok(EventKind::EndNode)
}
None => Ok(EventKind::EndFbx),
};
}
if node_header.bytelen_name == 0 {
let mut pos = self.position();
// Need to modify position, because the currently reading node is
// not reflected to the parser.
pos.byte_pos = self.reader().position();
pos.component_byte_pos = event_start_offset;
let local_node_index = self
.state
.current_node()
.map_or(self.state.known_toplevel_nodes_count, |v| {
v.known_children_count
});
pos.node_path.push((local_node_index, String::new()));
self.warn(Warning::EmptyNodeName, pos)?;
}
// Read the node name.
let name = {
let mut vec = vec![0; node_header.bytelen_name as usize];
self.reader.read_exact(&mut vec[..])?;
String::from_utf8(vec).map_err(DataError::InvalidNodeNameEncoding)?
};
let current_offset = self.reader().position();
let starting = StartedNode {
node_start_offset: event_start_offset,
node_end_offset: node_header.end_offset,
attributes_count: node_header.num_attributes,
attributes_end_offset: current_offset + node_header.bytelen_attributes,
name,
known_children_count: 0,
};
// Update parser status.
match self.state.started_nodes.last_mut() {
Some(parent) => parent.known_children_count += 1,
None => self.state.known_toplevel_nodes_count += 1,
}
self.state.started_nodes.push(starting);
Ok(EventKind::StartNode)
}
/// Skips unread attribute of the current node, if remains.
///
/// If there are no unread attributes, this method simply do nothing.
fn skip_unread_attributes(&mut self) -> Result<()> {
let attributes_end_offset = match self.state.current_node() {
Some(v) => v.attributes_end_offset,
None => return Ok(()),
};
if attributes_end_offset > self.reader().position() {
// Skip if attributes remains (partially or entirely) unread.
self.reader().skip_to(attributes_end_offset)?;
}
Ok(())
}
/// Sets the parser to aborted state.
#[inline]
pub(crate) fn set_aborted(&mut self, pos: SyntacticPosition) {
self.state.health = Health::Aborted(pos);
}
/// Ignores events until the current node closes.
///
/// This discards parser events until the [`EndNode`] event for the current
/// node is read.
/// The last [`EndNode`] (for the current node) is also discarded.
///
/// This method seeks to the node end position without any additional
/// parsing, since the parser already knows the node end position.
/// Because of this, some errors can be overlooked, or detected at the
/// different position from the true error position.
///
/// To detect errors correctly, you should use [`next_event`][`Self::next_event`] manually.
/// See an example to how to do this.
///
/// # Panics
///
/// Panics if there are no open nodes, i.e. when [`current_depth()`][`Self::current_depth`]
/// returns 0.
///
/// # Examples
///
/// ```no_run
/// # use fbxcel::low::FbxHeader;
/// # let reader = std::io::empty();
/// # let header: FbxHeader = unimplemented!();
/// let mut parser = fbxcel::pull_parser::v7400::Parser::from_reader(header, reader)
/// .expect("Failed to create parser");
/// // Do something here.
/// // Something done.
/// let depth = parser.current_depth();
/// if depth > 0 {
/// parser.skip_current_node().expect("Failed to skip current node");
/// assert_eq!(parser.current_depth(), depth - 1);
/// }
/// ```
///
/// `parser.skip_current_node()` is almost same as the code below, except
/// for error handling.
///
/// ```no_run
/// # use fbxcel::pull_parser::{v7400::{Parser, Event}, Result};
/// fn skip_current_node<R: std::io::Read>(parser: &mut Parser<R>) -> Result<()> {
/// loop {
/// match parser.next_event()? {
/// Event::StartNode(_) => skip_current_node(parser)?,
/// Event::EndNode => return Ok(()),
/// Event::EndFbx(_) => panic!("Attempt to skip implicit top-level node"),
/// }
/// }
/// }
/// ```
///
/// [`EndNode`]: `Event::EndNode`
pub fn skip_current_node(&mut self) -> Result<()> {
let end_pos = self
.state
.started_nodes
.pop()
.expect("Attempt to skip implicit top-level node")
.node_end_offset;
self.state.last_event_kind = Some(EventKind::EndNode);
self.reader.skip_to(end_pos)?;
Ok(())
}
/// Returns the syntactic position of the current node.
///
/// Note that this allocates memory.
#[inline]
pub fn position(&self) -> SyntacticPosition {
let byte_pos = self.reader.position();
if self.state.current_node().is_none() {
// Reading implicit root node.
return SyntacticPosition {
byte_pos,
component_byte_pos: 0,
node_path: Vec::new(),
attribute_index: None,
};
}
let toplevel_index = self
.state
.known_toplevel_nodes_count
.checked_sub(1)
.expect("Should never fail: implicit root node should have some children here");
// For now, use 0 for start offset of implicit root node.
// This behaviour may change in future.
let node_start_pos = self.state.current_node().map_or(0, |v| v.node_start_offset);
// Use not `checked_sub` but `saturating_sub` here, because
// `Iterator::zip` might read extra elements which can be used as
// result.
let trailing_indices = self
.state
.started_nodes
.iter()
.map(|v| v.known_children_count.saturating_sub(1));
let node_indices = std::iter::once(toplevel_index).chain(trailing_indices);
let node_names = self.state.started_nodes.iter().map(|v| v.name.clone());
let node_path = node_indices.zip(node_names).collect();
SyntacticPosition {
byte_pos,
component_byte_pos: node_start_pos,
node_path,
attribute_index: None,
}
}
/// Returns whether the parser is already used or brand-new.
///
/// Returns `true` if the parser emitted some events in the past, returns
/// `false` if the parser have not emitted any events.
///
/// # Examples
///
/// ```no_run
/// # use fbxcel::low::FbxHeader;
/// # let reader = std::io::empty();
/// # let header: FbxHeader = unimplemented!();
/// let mut parser = fbxcel::pull_parser::v7400::Parser::from_reader(header, reader)
/// .expect("Failed to create parser");
/// assert!(!parser.is_used());
/// parser.set_warning_handler(|warning, pos| {
/// // Print warning.
/// eprintln!("WARNING: {} (pos={:?})", warning, pos);
/// // To ignore the warning and continue processing, return `Ok(())`.
/// // To treat the given warning as a critical error, return
/// // `Err(warning.into())`.
/// Ok(())
/// });
/// assert!(!parser.is_used(), "Parser emitted no events yet");
/// let _ = parser.next_event();
/// assert!(parser.is_used(), "Parser emitted an event");
/// ```
#[inline]
#[must_use]
pub fn is_used(&self) -> bool {
self.state.last_event_kind.is_some()
}
}
impl<R: fmt::Debug> fmt::Debug for Parser<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Parser")
.field("state", &self.state)
.field("reader", &self.reader)
.field(
"warning_handler",
&self.warning_handler.as_ref().map(|v| v as *const _),
)
.finish()
}
}
/// Health of a parser.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Health {
/// Ready or already started, but not yet finished, and no critical errors.
Running,
/// Successfully finished.
Finished,
/// Aborted due to critical error.
Aborted(SyntacticPosition),
}
/// Parser state.
///
/// This type contains parser state especially which are independent of parser
/// source type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct State {
/// Target FBX version.
fbx_version: FbxVersion,
/// Health of the parser.
health: Health,
/// Started nodes stack.
///
/// This stack should not have an entry for implicit root node.
started_nodes: Vec<StartedNode>,
/// Last event kind.
last_event_kind: Option<EventKind>,
/// Number of known top-level nodes.
///
/// This is here because [`StartedNode`] is not used for implicit root node.
known_toplevel_nodes_count: usize,
}
impl State {
/// Creates a new `State` for the given FBX version.
#[inline]
#[must_use]
fn new(fbx_version: FbxVersion) -> Self {
Self {
fbx_version,
health: Health::Running,
started_nodes: Vec::new(),
last_event_kind: None,
known_toplevel_nodes_count: 0,
}
}
/// Returns health of the parser.
#[inline]
#[must_use]
fn health(&self) -> &Health {
&self.health
}
/// Returns info about current node (except for implicit root node).
#[inline]
#[must_use]
fn current_node(&self) -> Option<&StartedNode> {
self.started_nodes.last()
}
/// Returns the last event kind.
#[inline]
#[must_use]
fn last_event_kind(&self) -> Option<EventKind> {
self.last_event_kind
}
}
/// Event kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum EventKind {
/// Node start.
StartNode,
/// Node end.
EndNode,
/// FBX document end.
EndFbx,
}
/// Information about started node.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct StartedNode {
/// Start offset of the node attribute.
node_start_offset: u64,
/// End offset of the node.
///
/// "End offset" means a next byte of the last byte of the last node.
node_end_offset: u64,
/// Number of node attributes.
attributes_count: u64,
/// End offset of the previous attribute.
///
/// "End offset" means a next byte of the last byte of the last attribute.
attributes_end_offset: u64,
/// Node name.
name: String,
/// Number of known children.
known_children_count: usize,
}