fbxcel_dom/
any.rs

1//! Types and functions for all supported versions.
2//!
3//! To see how to use `AnyDocument`, see [crate-level
4//! documentation](../index.html).
5
6use std::io::{Read, Seek};
7
8use fbxcel::{low::FbxVersion, tree::any::AnyTree};
9
10pub use self::error::{Error, Result};
11
12mod error;
13
14/// FBX tree type with any supported version.
15#[non_exhaustive]
16pub enum AnyDocument {
17    /// FBX 7.4 or later.
18    V7400(FbxVersion, Box<crate::v7400::Document>),
19}
20
21impl AnyDocument {
22    /// Loads a document from the given reader.
23    ///
24    /// This works for seekable readers (which implement `std::io::Seek`), but
25    /// `from_seekable_reader` should be used for them, because it is more
26    /// efficent.
27    pub fn from_reader(reader: impl Read) -> Result<Self> {
28        match AnyTree::from_reader(reader)? {
29            AnyTree::V7400(fbx_version, tree, _footer) => {
30                let doc = crate::v7400::Loader::new().load_from_tree(tree)?;
31                Ok(AnyDocument::V7400(fbx_version, Box::new(doc)))
32            }
33            tree => Err(Error::UnsupportedVersion(tree.fbx_version())),
34        }
35    }
36
37    /// Loads a document from the given seekable reader.
38    pub fn from_seekable_reader(reader: impl Read + Seek) -> Result<Self> {
39        match AnyTree::from_seekable_reader(reader)? {
40            AnyTree::V7400(fbx_version, tree, _footer) => {
41                let doc = crate::v7400::Loader::new().load_from_tree(tree)?;
42                Ok(AnyDocument::V7400(fbx_version, Box::new(doc)))
43            }
44            tree => Err(Error::UnsupportedVersion(tree.fbx_version())),
45        }
46    }
47
48    /// Returns the FBX version of the loaded document.
49    pub fn fbx_version(&self) -> FbxVersion {
50        match self {
51            Self::V7400(ver, _) => *ver,
52        }
53    }
54}