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 SQUAD_V2_PACK: Pack = Pack {
	name: "squad_v2",
	version: "1.0.0",
	license: "CC-BY-SA-4.0",
	files: &[
		PackFile {
			name: "train-v2.0.json",
			size: 42_123_633,
			shake256: "09a7f77787cb89923d0311fc33e592a128ca59edba7dbdf474ef1ec9f18d6720",
			urls: &["https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v2.0.json"],
		},
		PackFile {
			name: "dev-v2.0.json",
			size: 4_370_528,
			shake256: "0ac893b888f2ccb20bdc8de0c3a52793d1c697c9f7a32e7e533a82661a7d9f18",
			urls: &["https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v2.0.json"],
		},
	],
};

/// Official SQuAD v2 split selection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SquadV2Split {
	/// `train-v2.0.json` (130,319 questions).
	#[default]
	Train,
	/// `dev-v2.0.json` (11,873 questions).
	Validation,
}

/// Load options for [`SquadV2`][crate::text::squad_v2::SquadV2].
#[derive(Clone, Debug)]
pub struct SquadV2Config {
	/// Official split to load.
	pub split: SquadV2Split,
	/// 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 SquadV2Config {
	fn default() -> Self {
		Self {
			split: SquadV2Split::Train,
			cache_dir: None,
		}
	}
}

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

impl super::SquadV2 {
	/// 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: &SquadV2Config) -> 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: &SquadV2Config) -> 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: &SquadV2Config,
		progress: &mut impl FnMut(FetchProgress) -> bool,
	) -> Result<Self> {
		Self::load_with_progress_on(&Engine::current()?, config, progress)
	}
}