oadsrust 0.2.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::pack::{FetchProgress, Pack, PackFile};
use oa::{Engine, Result};
use std::path::PathBuf;

pub(crate) const SHAKESPEARE_PACK: Pack = Pack {
	name: "tiny_shakespeare",
	version: "1.0.0",
	license: "public-domain",
	files: &[PackFile {
		name: "input.txt",
		size: 1_115_394,
		shake256: "f71bef2200fc229ce76fac55556cac487c21be9ed12e4bfc828882d8219fab91",
		urls: &[
			"https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt",
		],
	}],
};

/// Token window length of each sample by default.
pub const DEFAULT_SEQ_LEN: usize = 128;

/// Load options for [`TinyShakespeare`][crate::text::tiny_shakespeare::TinyShakespeare].
#[derive(Clone, Debug)]
pub struct TinyShakespeareConfig {
	/// Token window length of each sample; must be at least 1. Defaults to
	/// `DEFAULT_SEQ_LEN`.
	pub seq_len: usize,
	/// 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 TinyShakespeareConfig {
	fn default() -> Self {
		Self {
			seq_len: DEFAULT_SEQ_LEN,
			cache_dir: None,
		}
	}
}

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

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