use std::sync::Arc;
use time::OffsetDateTime;
use super::{Resolver, Result};
pub trait Clock: Send + Sync {
fn now_local(&self) -> Result<OffsetDateTime>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct SystemClock;
impl Clock for SystemClock {
fn now_local(&self) -> Result<OffsetDateTime> {
match OffsetDateTime::now_local() {
Ok(now) => Ok(now),
Err(time::error::IndeterminateOffset) => Ok(OffsetDateTime::now_utc()),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct FixedClock(OffsetDateTime);
impl FixedClock {
#[must_use]
pub const fn new(value: OffsetDateTime) -> Self {
Self(value)
}
}
impl Clock for FixedClock {
fn now_local(&self) -> Result<OffsetDateTime> {
Ok(self.0)
}
}
pub struct ExecutionEnvironment<R: Resolver + 'static> {
pub(crate) resolver: Arc<R>,
pub(crate) clock: Option<Arc<dyn Clock>>,
pub(crate) process_xinclude: bool,
pub(crate) work_ceiling: usize,
}
impl<R: Resolver + 'static> ExecutionEnvironment<R> {
#[must_use]
pub fn new(resolver: Arc<R>) -> Self {
Self {
resolver,
clock: None,
process_xinclude: false,
work_ceiling: usize::MAX,
}
}
#[must_use]
pub const fn with_work_ceiling(mut self, work: usize) -> Self {
self.work_ceiling = work;
self
}
#[must_use]
pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
self.clock = Some(clock);
self
}
#[must_use]
pub const fn with_xinclude(mut self) -> Self {
self.process_xinclude = true;
self
}
}
#[cfg(all(test, not(feature = "embedded")))]
mod tests;