Skip to main content

semantic_scene/loader/
mod.rs

1//! Loader traits and dataset-specific loader implementations.
2use std::{
3    io::{BufRead, BufReader, Cursor},
4    path::Path,
5};
6
7use crate::SemanticScene;
8
9pub mod mp3d;
10
11pub use mp3d::{Mp3dLoader, Mp3dOptions};
12
13/// Trait implemented by semantic scene descriptor loaders.
14///
15/// `from_reader` is the required primitive so loaders can work with files,
16/// in-memory buffers, compressed streams, and test fixtures. `from_str` and
17/// `from_path` are convenience methods implemented in terms of it.
18pub trait SemanticSceneLoader {
19    /// Loader-specific options.
20    type Options: Default;
21    /// Loader-specific error type.
22    type Error;
23
24    /// Loads a semantic scene from any buffered reader.
25    ///
26    /// # Errors
27    ///
28    /// Returns loader-specific errors for I/O, parse, and validation failures.
29    fn from_reader<R: BufRead>(
30        reader: R,
31        options: Self::Options,
32    ) -> Result<SemanticScene, Self::Error>;
33
34    /// Loads a semantic scene from an in-memory string.
35    ///
36    /// # Errors
37    ///
38    /// Returns loader-specific parse and validation failures.
39    fn from_str(input: &str, options: Self::Options) -> Result<SemanticScene, Self::Error> {
40        Self::from_reader(Cursor::new(input), options)
41    }
42
43    /// Loads a semantic scene from a path.
44    ///
45    /// # Errors
46    ///
47    /// Returns I/O errors from opening the file or loader-specific parse and validation failures.
48    fn from_path(
49        path: impl AsRef<Path>,
50        options: Self::Options,
51    ) -> Result<SemanticScene, Self::Error>
52    where
53        Self::Error: From<std::io::Error>,
54    {
55        let file = std::fs::File::open(path)?;
56        Self::from_reader(BufReader::new(file), options)
57    }
58}