Skip to main content

semantic_scene/
dataset.rs

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