#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::time::SystemTime;
mod reference;
pub use reference::{ParseReferenceError, Reference};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ApplicationId(&'static str);
impl ApplicationId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ProcessId(&'static str);
impl ProcessId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ExecutionId(&'static str);
impl ExecutionId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ModuleId(&'static str);
impl ModuleId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CapabilityId(&'static str);
impl CapabilityId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct QualifierId(&'static str);
impl QualifierId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ContributionId(&'static str);
impl ContributionId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ContributionTargetId(&'static str);
impl ContributionTargetId {
pub const fn new(name: &'static str) -> Self {
Self(name)
}
pub const fn as_str(self) -> &'static str {
self.0
}
}
pub trait Qualifier {
const ID: QualifierId;
}
pub trait Capability {
type Value: ?Sized;
const ID: CapabilityId;
}
pub trait Module {
const ID: ModuleId;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LifecycleContext {
application: ApplicationId,
process: ProcessId,
}
impl LifecycleContext {
pub const fn new(application: ApplicationId, process: ProcessId) -> Self {
Self {
application,
process,
}
}
pub const fn application(self) -> ApplicationId {
self.application
}
pub const fn process(self) -> ProcessId {
self.process
}
}
pub trait Initialize: Module {
type Error;
fn initialize(&mut self, context: &LifecycleContext) -> Result<(), Self::Error>;
}
pub trait Start: Module {
type Error;
fn start(&mut self, context: &LifecycleContext) -> Result<(), Self::Error>;
}
pub trait Ready: Module {
type Error;
fn ready(&mut self, context: &LifecycleContext) -> Result<(), Self::Error>;
}
pub trait Drain: Module {
type Error;
fn drain(&mut self, context: &LifecycleContext) -> Result<(), Self::Error>;
}
pub trait Stop: Module {
type Error;
fn stop(&mut self, context: &LifecycleContext) -> Result<(), Self::Error>;
}
pub trait Requires<C: Capability>: Module {
const SELECTED_PROVIDER: Option<ModuleId> = None;
}
pub trait Provides<C: Capability>: Module {
fn provided_value(&self) -> &C::Value;
}
pub trait Contribution {
const ID: ContributionId;
const REQUIRED: bool = true;
}
pub trait ContributionTarget {
type Contribution: Contribution;
type Runtime;
type Error;
const ID: ContributionTargetId;
fn build(
&self,
contributions: &[(ModuleId, Self::Contribution)],
) -> Result<Self::Runtime, Self::Error>;
}
pub trait Clock: Send + Sync {
fn now(&self) -> SystemTime;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> SystemTime {
SystemTime::now()
}
}
#[derive(Debug)]
pub struct ManualClock(std::sync::Mutex<SystemTime>);
impl ManualClock {
pub const fn new(now: SystemTime) -> Self {
Self(std::sync::Mutex::new(now))
}
pub fn set(&self, now: SystemTime) {
*self.lock() = now;
}
pub fn advance(&self, by: std::time::Duration) {
let mut now = self.lock();
*now = now
.checked_add(by)
.expect("ManualClock advanced past SystemTime range");
}
fn lock(&self) -> std::sync::MutexGuard<'_, SystemTime> {
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
impl Clock for ManualClock {
fn now(&self) -> SystemTime {
*self.lock()
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct ClockCapability;
impl Capability for ClockCapability {
type Value = dyn Clock;
const ID: CapabilityId = CapabilityId::new("rustclamp.core.clock");
}