reifydb-sdk 0.8.1

SDK for building ReifyDB operators, procedures, transforms and more
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use reifydb_value::value::duration::Duration;

use super::Config;

impl Config {
	pub fn duration(&self, key: &str) -> Option<Duration> {
		self.opt_coerce(key)
	}

	pub fn require_duration(&self, key: &str) -> Duration {
		self.opt_coerce(key).unwrap_or_else(|| self.missing(key, "a duration"))
	}

	pub fn duration_or(&self, key: &str, default: Duration) -> Duration {
		self.opt_coerce(key).unwrap_or(default)
	}
}

#[cfg(test)]
mod tests {
	use reifydb_value::value::{Value, duration::Duration, time::Time};

	use super::super::testutil::config;

	#[test]
	fn casts_duration_values() {
		let d = Duration::from_seconds(60).unwrap();
		let cfg = config(vec![("d", Value::Duration(d))]);
		assert_eq!(cfg.duration("d"), Some(d));
	}

	#[test]
	fn rejects_other_temporal_and_numeric() {
		let cfg = config(vec![("t", Value::Time(Time::midnight())), ("n", Value::Uint8(60))]);
		assert_eq!(cfg.duration("t"), None, "a time does not coerce to a duration");
		assert_eq!(cfg.duration("n"), None, "a raw integer does not coerce to a duration");
	}

	#[test]
	fn parses_duration_literal_string() {
		let cfg = config(vec![("d", Value::utf8("1m")), ("sub", Value::utf8("1s"))]);
		assert_eq!(
			cfg.duration("d"),
			Some(Duration::from_minutes(1).unwrap()),
			"a duration literal string coerces to a Duration"
		);
		assert_eq!(
			cfg.require_duration("sub"),
			Duration::from_seconds(1).unwrap(),
			"a sub-minute duration literal must stay sub-minute, not round up"
		);
	}

	#[test]
	fn or_and_require_behavior() {
		let d = Duration::from_seconds(60).unwrap();
		let default = Duration::zero();
		let cfg = config(vec![("present", Value::Duration(d))]);
		assert_eq!(cfg.duration_or("present", default), d);
		assert_eq!(cfg.duration_or("absent", default), default);
		assert_eq!(cfg.require_duration("present"), d);
	}

	#[test]
	#[should_panic(expected = "is missing or not a duration")]
	fn require_panics_when_missing() {
		let cfg = config(vec![]);
		cfg.require_duration("k");
	}
}