semantic-scene 0.1.0

Rust parser for Habitat-Sim `SemanticScene` descriptors.
Documentation
//! Loader traits and dataset-specific loader implementations.
use std::{
    io::{BufRead, BufReader, Cursor},
    path::Path,
};

use crate::SemanticScene;

pub mod mp3d;

pub use mp3d::{Mp3dLoader, Mp3dOptions};

/// Trait implemented by semantic scene descriptor loaders.
///
/// `from_reader` is the required primitive so loaders can work with files,
/// in-memory buffers, compressed streams, and test fixtures. `from_str` and
/// `from_path` are convenience methods implemented in terms of it.
pub trait SemanticSceneLoader {
    /// Loader-specific options.
    type Options: Default;
    /// Loader-specific error type.
    type Error;

    /// Loads a semantic scene from any buffered reader.
    ///
    /// # Errors
    ///
    /// Returns loader-specific errors for I/O, parse, and validation failures.
    fn from_reader<R: BufRead>(
        reader: R,
        options: Self::Options,
    ) -> Result<SemanticScene, Self::Error>;

    /// Loads a semantic scene from an in-memory string.
    ///
    /// # Errors
    ///
    /// Returns loader-specific parse and validation failures.
    fn from_str(input: &str, options: Self::Options) -> Result<SemanticScene, Self::Error> {
        Self::from_reader(Cursor::new(input), options)
    }

    /// Loads a semantic scene from a path.
    ///
    /// # Errors
    ///
    /// Returns I/O errors from opening the file or loader-specific parse and validation failures.
    fn from_path(
        path: impl AsRef<Path>,
        options: Self::Options,
    ) -> Result<SemanticScene, Self::Error>
    where
        Self::Error: From<std::io::Error>,
    {
        let file = std::fs::File::open(path)?;
        Self::from_reader(BufReader::new(file), options)
    }
}