1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Type-safe host service registration.
use std::{
any::{Any, TypeId},
collections::HashMap,
sync::Arc,
};
use thiserror::Error;
/// Service registration failure.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum ServiceRegistryError {
#[error("a service of this type is already registered")]
Duplicate,
}
/// Immutable type-indexed services supplied by an application or adapter.
#[derive(Clone, Default)]
pub struct ServiceRegistry {
entries: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}
impl ServiceRegistry {
/// Creates an empty registry.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Registers one service without allowing silent replacement.
pub fn register<T: Any + Send + Sync>(
&mut self,
service: T,
) -> Result<(), ServiceRegistryError> {
if self.entries.contains_key(&TypeId::of::<T>()) {
return Err(ServiceRegistryError::Duplicate);
}
self.entries.insert(TypeId::of::<T>(), Arc::new(service));
Ok(())
}
/// Registers a service, replacing any existing one of the same type.
///
/// Crate-private: only the engine may overwrite, and only for services it
/// derives per run and must not accept from a host.
pub(crate) fn replace<T: Any + Send + Sync>(&mut self, service: T) {
self.entries.insert(TypeId::of::<T>(), Arc::new(service));
}
/// Number of registered services.
pub(crate) fn len(&self) -> usize {
self.entries.len()
}
/// Looks up a service by its concrete interface type.
#[must_use]
pub fn get<T: Any + Send + Sync>(&self) -> Option<&T> {
self.entries.get(&TypeId::of::<T>())?.downcast_ref()
}
}
/// Evidence an adapter may present as fact.
///
/// Exact, and carrying a locator a human can follow back to the source. Both
/// halves matter: an exact measurement nobody can audit is not reviewable, and
/// a well-located estimate is not exact. Owned here so every service applies
/// the same admission test.
pub(crate) fn reviewable_exact_evidence(evidence: &axioval_ir::Evidence) -> bool {
evidence.exact && !evidence.locator.trim().is_empty()
}