oadsrust 0.3.0

Content-addressed dataset packs and loaders for OA
// @generated by tool/gen/ds/generate.py — edit tool/gen/ds/schema/packs.json instead.
use crate::core::pack::{FetchProgress, Pack, PackFile};
use oa::{Engine, Result};
use std::path::PathBuf;

pub(crate) const SVHN_PACK: Pack = Pack {
	name: "svhn",
	version: "1.0.0",
	license: "non-commercial (Google Street View)",
	files: &[
		PackFile {
			name: "train_32x32.mat",
			size: 182_040_794,
			shake256: "85a5006cc57bdd6bb3de6653fe3c2301bc5824e526643d267d2f63fc152c8743",
			urls: &["http://ufldl.stanford.edu/housenumbers/train_32x32.mat"],
		},
		PackFile {
			name: "test_32x32.mat",
			size: 64_275_384,
			shake256: "96b67044770279d716be97ca364a50d371ef3581ed53035bfb4522184088e075",
			urls: &["http://ufldl.stanford.edu/housenumbers/test_32x32.mat"],
		},
		PackFile {
			name: "extra_32x32.mat",
			size: 1_329_278_602,
			shake256: "bb347c619406b9624ec0722bd8195612548cf2b117b45ee041dc25847f43bbc2",
			urls: &["http://ufldl.stanford.edu/housenumbers/extra_32x32.mat"],
		},
	],
};

/// Official SVHN split selection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SvhnSplit {
	/// `train_32x32.mat` (73,257 images).
	#[default]
	Train,
	/// `extra_32x32.mat` (531,131 easier images).
	Extra,
	/// `test_32x32.mat` (26,032 images).
	Test,
}

/// Load options for [`Svhn`][crate::image::svhn::Svhn].
#[derive(Clone, Debug)]
pub struct SvhnConfig {
	/// Which official split to materialize.
	pub split: SvhnSplit,
	/// Scale image pixels into `[0, 1]`; `false` keeps raw `0..=255`.
	pub normalize: bool,
	/// Cache root override. `None` resolves `OA_DATA_DIR`, then the platform
	/// cache directory; see [`data_root`](crate::pack::data_root).
	pub cache_dir: Option<PathBuf>,
}

impl Default for SvhnConfig {
	fn default() -> Self {
		Self {
			split: SvhnSplit::Train,
			normalize: true,
			cache_dir: None,
		}
	}
}

impl SvhnConfig {
	/// Construct the record with its default state.
	#[inline]
	#[must_use]
	pub fn new() -> Self {
		Self::default()
	}
}

impl super::Svhn {
	/// Decode the selected split and upload it on this thread's
	/// default engine.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_on`](Self::load_on).
	#[inline]
	pub fn load(config: &SvhnConfig) -> Result<Self> {
		Self::load_on(&Engine::current()?, config)
	}

	/// Decode the selected split and upload it on `engine`.
	///
	/// This is the [`load`](Self::load) contract evaluated on
	/// `engine` instead of this thread's default engine.
	///
	/// # Errors
	///
	/// Returns the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_on(engine: &Engine, config: &SvhnConfig) -> Result<Self> {
		Self::load_with_progress_on(engine, config, &mut |_| true)
	}

	/// [`load`](Self::load) with a fetch progress callback.
	///
	/// # Errors
	///
	/// Returns an error when the default engine cannot be
	/// initialized, plus the errors of
	/// [`load_with_progress_on`](Self::load_with_progress_on).
	#[inline]
	pub fn load_with_progress(
		config: &SvhnConfig,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}