use plugmem_host::SettingsError;
use crate::CliError;
impl From<SettingsError> for CliError {
fn from(e: SettingsError) -> Self {
CliError::Usage(e.to_string())
}
}
pub(crate) fn read_batch_size(table: Option<&toml::Table>) -> Option<u64> {
table
.and_then(|t| t.get("maintenance"))
.and_then(toml::Value::as_table)
.and_then(|m| m.get("batch_size"))
.and_then(toml::Value::as_integer)
.filter(|n| *n >= 0)
.map(|n| n as u64)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn batch_size_reads_from_the_maintenance_table() {
let table: toml::Table = "[maintenance]\nbatch_size = 256\n".parse().unwrap();
assert_eq!(read_batch_size(Some(&table)), Some(256));
let empty: toml::Table = "[engine]\ndim = 8\n".parse().unwrap();
assert_eq!(read_batch_size(Some(&empty)), None);
assert_eq!(read_batch_size(None), None);
let neg: toml::Table = "[maintenance]\nbatch_size = -1\n".parse().unwrap();
assert_eq!(read_batch_size(Some(&neg)), None);
}
#[test]
fn settings_error_maps_to_a_usage_error() {
let e: CliError = SettingsError::Config("boom".into()).into();
assert!(matches!(e, CliError::Usage(m) if m == "boom"));
}
}