Skip to content
1 change: 1 addition & 0 deletions aimdb-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Breaking — typed record keys (design 063)

- Add TypedName, TypedKey and Name. Record registration, handles, direct reads/writes, transforms and joins infer payloads from declarations. Remove bare-string typed access and public schema setters.
- Add a schema type-state: `TypedKey::Schema` (`Declared` / `Undeclared`), `SchemaState`, `ContractDeclared`, and a third parameter on `RecordRegistrar<'a, T, S = Undeclared>`. `configure` hands out the key's state. `Input` / `Output` take the state as a parameter (`impl<T, S: SchemaState> Input<T, S> for MyInput`). `ContractOutboundLink` / `ContractInboundLink` (from `into_contract()`) wire only into `ContractDeclared` records. `Name<T>` gains `Debug`.

### Added — value envelopes (design 062)

Expand Down
6 changes: 4 additions & 2 deletions aimdb-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ impl AimDbBuilder {
/// ```rust,ignore
/// let mut b = AimDbBuilder::new().runtime(rt);
/// let mqtt = b.connect(MqttConnector::new("mqtt://broker.local:1883"));
/// let record_3 = aimdb_core::Name::<Temperature>::new("temp.indoor");
/// // A contract record is declared with `Key<T>`, which carries its schema.
/// let record_3 = aimdb_data_contracts::Key::<Temperature>::new("temp.indoor");
/// b.configure(record_3, |r| {
/// // A contract (`Linkable`) brings its own wire format:
/// r.output(aimdb_data_contracts::publish(mqtt, "sensors/temp/indoor"));
Expand Down Expand Up @@ -557,7 +558,7 @@ impl AimDbBuilder {
pub fn configure<K: crate::record_id::TypedKey>(
&mut self,
key: K,
f: impl FnOnce(&mut RecordRegistrar<'_, K::Contract>),
f: impl FnOnce(&mut RecordRegistrar<'_, K::Contract, K::Schema>),
) -> &mut Self {
type Payload<K> = <K as crate::TypedName>::Contract;
// Convert any RecordKey to StringKey for internal storage
Expand Down Expand Up @@ -632,6 +633,7 @@ impl AimDbBuilder {
key_link_address: key.link_address().map(ToString::to_string),
extensions: &self.extensions,
last_stage: None,
_schema: core::marker::PhantomData,
};
if let Some((name, version)) = K::SCHEMA {
reg.rec.set_schema(name, version);
Expand Down
5 changes: 4 additions & 1 deletion aimdb-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ pub use topic_pattern::{
pub use inbound_key::KeyId;

// Record identification exports
pub use record_id::{Name, RecordId, RecordKey, StringKey, TypedKey, TypedName};
pub use record_id::{
ContractDeclared, Declared, Name, RecordId, RecordKey, SchemaState, StringKey, TypedKey,
TypedName, Undeclared,
};

// Graph exports (dependency graph for record topology)
pub use graph::{DependencyGraph, EdgeType, GraphEdge, GraphNode, RecordOrigin};
Expand Down
48 changes: 46 additions & 2 deletions aimdb-core/src/record_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ mod tests {
}

#[test]
fn test_static_str_record_key() {
// &'static str implements RecordKey
fn test_name_record_key() {
// A `Name<T>` with a static name implements RecordKey
let key = Name::<u32>::new("sensors.temp");
assert_eq!(key.as_str(), "sensors.temp");
}
Expand Down Expand Up @@ -725,7 +725,45 @@ pub trait TypedKey: TypedName + RecordKey + Copy {
/// Contract name and version reported as the record's schema; `None` for
/// a record without a data contract.
const SCHEMA: Option<(&'static str, u32)>;
/// [`Declared`] when [`SCHEMA`](Self::SCHEMA) is `Some`, [`Undeclared`]
/// otherwise. `configure` hands out a registrar in this state, so
/// contract-only verbs (`linked_to`, `publish`, `observe`, …) exist only
/// on records whose key declares their contract.
type Schema: SchemaState;
}

mod sealed {
pub trait Sealed {}
}

/// Whether a record's key declares a data contract: [`Declared`] or
/// [`Undeclared`]. The third type parameter of
/// [`RecordRegistrar`](crate::typed_api::RecordRegistrar).
pub trait SchemaState: sealed::Sealed + Send + Sync + 'static {}

/// The record's key declares its contract (`Key<T>`,
/// `#[record_key(contract = T)]`): contract-only verbs are available.
#[derive(Debug)]
pub enum Declared {}

/// The record's key declares no contract (`Name<T>`,
/// `#[record_key(payload = T)]`): contract-only verbs do not compile.
#[derive(Debug)]
pub enum Undeclared {}

impl sealed::Sealed for Declared {}
impl sealed::Sealed for Undeclared {}
impl SchemaState for Declared {}
impl SchemaState for Undeclared {}

/// Implemented only by [`Declared`]: the bound contract-only verbs put on a
/// registrar's schema state.
#[diagnostic::on_unimplemented(
message = "this record's key declares no data contract",
note = "contract verbs (`linked_to`, `linked_from`, `publish`, `subscribe`, `observe`) need the record declared with `Key<T>` or `#[record_key(contract = T)]`, not `Name<T>` or `payload = T`"
)]
pub trait ContractDeclared: SchemaState {}
impl ContractDeclared for Declared {}
/// A record name with its payload type and no data contract.
///
/// `Name<'static, T>` is a key: a `const` for a record without a contract, or
Expand Down Expand Up @@ -775,6 +813,11 @@ impl<T> core::hash::Hash for Name<'_, T> {
self.name.hash(state)
}
}
impl<T> core::fmt::Debug for Name<'_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.name)
}
}
impl<T> core::borrow::Borrow<str> for Name<'_, T> {
fn borrow(&self) -> &str {
self.name
Expand All @@ -796,6 +839,7 @@ impl<T: 'static> RecordKey for Name<'static, T> {
}
impl<T: Send + Sync + Clone + core::fmt::Debug + 'static> TypedKey for Name<'static, T> {
const SCHEMA: Option<(&'static str, u32)> = None;
type Schema = Undeclared;
}
impl<T> AsRef<str> for Name<'_, T> {
fn as_ref(&self) -> &str {
Expand Down
Loading