aequora-server 0.1.1

Server-authoritative, database-agnostic local-first synchronization for Rust
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! High-level server SDK independent from Axum and physical database types.

use aequora_adapter_sdk::{
    AdapterError, Authenticator, AuthorityService, Credential, DomainContext, DomainHandler,
    DomainOutcome,
};
use aequora_operation::{EncodedOperation, OperationKind};
use std::{
    collections::{BTreeMap, btree_map::Entry},
    fmt,
    sync::Arc,
};
use thiserror::Error;

/// Domain handler registration failure detected before traffic is served.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum DomainRegistrationError {
    /// More than one handler claimed an operation kind.
    #[error("operation kind {kind} is registered more than once")]
    DuplicateKind {
        /// Duplicated stable kind.
        kind: OperationKind,
    },
    /// Handler did not declare a certified consistency profile.
    #[error("operation kind {kind} has no consistency profile")]
    MissingProfile {
        /// Operation kind missing its profile.
        kind: OperationKind,
    },
    /// Handler declared schema version zero.
    #[error("operation kind {kind} has reserved schema version zero")]
    MissingSchema {
        /// Operation kind missing a usable schema version.
        kind: OperationKind,
    },
}

/// Immutable, validated domain-handler registry.
#[derive(Clone)]
pub struct DomainRegistry {
    handlers: Arc<BTreeMap<OperationKind, Arc<dyn DomainHandler>>>,
}

impl fmt::Debug for DomainRegistry {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DomainRegistry")
            .field("operation_kinds", &self.handlers.keys().collect::<Vec<_>>())
            .finish()
    }
}

impl DomainRegistry {
    /// Starts an empty registry builder.
    #[must_use]
    pub fn builder() -> DomainRegistryBuilder {
        DomainRegistryBuilder::default()
    }

    fn handler(&self, kind: OperationKind) -> Option<&Arc<dyn DomainHandler>> {
        self.handlers.get(&kind)
    }
}

/// Validating domain registry builder.
#[derive(Default)]
pub struct DomainRegistryBuilder {
    handlers: BTreeMap<OperationKind, Arc<dyn DomainHandler>>,
    error: Option<DomainRegistrationError>,
}

impl DomainRegistryBuilder {
    /// Registers one explicitly profiled domain handler.
    #[must_use]
    pub fn register(mut self, handler: impl DomainHandler + 'static) -> Self {
        let kind = handler.operation_kind();
        if self.error.is_some() {
            return self;
        }
        if handler.profile().is_none() {
            self.error = Some(DomainRegistrationError::MissingProfile { kind });
        } else if handler.schema_version() == 0 {
            self.error = Some(DomainRegistrationError::MissingSchema { kind });
        } else {
            match self.handlers.entry(kind) {
                Entry::Vacant(entry) => {
                    entry.insert(Arc::new(handler));
                }
                Entry::Occupied(_) => {
                    self.error = Some(DomainRegistrationError::DuplicateKind { kind });
                }
            }
        }
        self
    }

    /// Freezes the registry after validating duplicate ownership, schema, and profile metadata.
    ///
    /// # Errors
    ///
    /// Returns the first deterministic registration error.
    pub fn build(self) -> Result<DomainRegistry, DomainRegistrationError> {
        if let Some(error) = self.error {
            Err(error)
        } else {
            Ok(DomainRegistry {
                handlers: Arc::new(self.handlers),
            })
        }
    }
}

/// Stable server-side operation outcome. Business rejection is not a system failure.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ServerOperationOutcome {
    /// Authority committed the accepted handler result.
    Accepted(Vec<u8>),
    /// Domain business rules rejected the intent.
    BusinessRejected {
        /// Stable application-defined code.
        code: Arc<str>,
    },
    /// Domain requested explicit conflict resolution.
    Conflict {
        /// Stable application-defined reason.
        reason: Arc<str>,
    },
}

/// Stable high-level server error.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum StableServerError {
    /// Required builder field is absent.
    #[error("required server field `{field}` is missing")]
    MissingConfiguration {
        /// Missing semantic field.
        field: &'static str,
    },
    /// Operation kind is not registered.
    #[error("operation kind {kind} is not registered")]
    UnknownOperation {
        /// Unknown stable operation kind.
        kind: OperationKind,
    },
    /// Operation schema is incompatible with its registered handler.
    #[error("operation kind {kind} schema {received} does not match registered schema {expected}")]
    SchemaMismatch {
        /// Stable operation kind.
        kind: OperationKind,
        /// Received schema version.
        received: u16,
        /// Registered schema version.
        expected: u16,
    },
    /// Authentication, handler, or authority adapter failed.
    #[error(transparent)]
    Adapter(#[from] AdapterError),
    /// A newer handler outcome is not understood by this server release.
    #[error("domain handler returned an unsupported outcome variant")]
    UnsupportedOutcome,
}

/// Runtime-validated server builder.
#[derive(Default)]
pub struct AequoraServerBuilder {
    authority: Option<Arc<dyn AuthorityService>>,
    registry: Option<DomainRegistry>,
    authenticator: Option<Arc<dyn Authenticator>>,
}

impl AequoraServerBuilder {
    /// Installs the authoritative commit service.
    #[must_use]
    pub fn authority_store(mut self, authority: impl AuthorityService + 'static) -> Self {
        self.authority = Some(Arc::new(authority));
        self
    }

    /// Installs a pre-shared authoritative commit service.
    #[must_use]
    pub fn shared_authority_store(mut self, authority: Arc<dyn AuthorityService>) -> Self {
        self.authority = Some(authority);
        self
    }

    /// Installs the validated application domain registry.
    #[must_use]
    pub fn registry(mut self, registry: DomainRegistry) -> Self {
        self.registry = Some(registry);
        self
    }

    /// Alias matching domain-oriented examples.
    #[must_use]
    pub fn domain(self, registry: DomainRegistry) -> Self {
        self.registry(registry)
    }

    /// Installs the authentication provider.
    #[must_use]
    pub fn authenticator(mut self, authenticator: impl Authenticator + 'static) -> Self {
        self.authenticator = Some(Arc::new(authenticator));
        self
    }

    /// Installs a pre-shared authentication provider.
    #[must_use]
    pub fn shared_authenticator(mut self, authenticator: Arc<dyn Authenticator>) -> Self {
        self.authenticator = Some(authenticator);
        self
    }

    /// Validates required fields before any request traffic is accepted.
    ///
    /// # Errors
    ///
    /// Returns a stable validation error naming the first missing field.
    pub fn build(self) -> Result<AequoraServer, StableServerError> {
        Ok(AequoraServer {
            inner: Arc::new(ServerInner {
                authority: self
                    .authority
                    .ok_or(StableServerError::MissingConfiguration {
                        field: "authority_store",
                    })?,
                registry: self
                    .registry
                    .ok_or(StableServerError::MissingConfiguration { field: "registry" })?,
                authenticator: self.authenticator.ok_or(
                    StableServerError::MissingConfiguration {
                        field: "authenticator",
                    },
                )?,
            }),
        })
    }
}

struct ServerInner {
    authority: Arc<dyn AuthorityService>,
    registry: DomainRegistry,
    authenticator: Arc<dyn Authenticator>,
}

/// Cheaply clonable high-level server core. Transport integration belongs in `aequora-axum` or
/// another adapter crate.
#[derive(Clone)]
pub struct AequoraServer {
    inner: Arc<ServerInner>,
}

impl fmt::Debug for AequoraServer {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("AequoraServer")
            .field("registry", &self.inner.registry)
            .finish_non_exhaustive()
    }
}

impl AequoraServer {
    /// Starts a runtime-validated high-level server builder.
    #[must_use]
    pub fn builder() -> AequoraServerBuilder {
        AequoraServerBuilder::default()
    }

    /// Authenticates, dispatches, and authoritatively commits one semantic operation.
    ///
    /// # Errors
    ///
    /// Returns a stable authentication, registry, handler, schema, or authority failure.
    pub async fn execute(
        &self,
        credential: &Credential,
        operation: EncodedOperation,
    ) -> Result<ServerOperationOutcome, StableServerError> {
        let identity = self.inner.authenticator.authenticate(credential).await?;
        let kind = operation.kind();
        let handler = self
            .inner
            .registry
            .handler(kind)
            .ok_or(StableServerError::UnknownOperation { kind })?;
        let expected = handler.schema_version();
        let received = operation.schema_version().get();
        if received != expected {
            return Err(StableServerError::SchemaMismatch {
                kind,
                received,
                expected,
            });
        }
        let context = DomainContext {
            identity,
            operation_id: operation.operation_id(),
        };
        match handler.handle(context, operation.payload()).await? {
            DomainOutcome::Accepted(outcome) => self
                .inner
                .authority
                .commit(identity, operation, outcome)
                .await
                .map(ServerOperationOutcome::Accepted)
                .map_err(StableServerError::from),
            DomainOutcome::BusinessRejected { code } => {
                Ok(ServerOperationOutcome::BusinessRejected { code })
            }
            DomainOutcome::Conflict { reason } => Ok(ServerOperationOutcome::Conflict { reason }),
            _ => Err(StableServerError::UnsupportedOutcome),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use aequora_adapter_sdk::{AdapterErrorKind, ClientIdentity, DomainContext};
    use aequora_operation::{Operation, OperationEncodingError, OperationSchemaVersion};
    use aequora_types::{DeviceId, TenantId};
    use async_trait::async_trait;

    struct Handler;

    #[async_trait]
    impl DomainHandler for Handler {
        fn operation_kind(&self) -> OperationKind {
            OperationKind::new(77).unwrap_or_else(|error| panic!("kind is valid: {error}"))
        }

        fn schema_version(&self) -> u16 {
            1
        }

        fn profile(&self) -> Option<&'static str> {
            Some("strong-aggregate")
        }

        async fn handle(
            &self,
            _context: DomainContext,
            payload: &[u8],
        ) -> Result<DomainOutcome, AdapterError> {
            Ok(DomainOutcome::Accepted(payload.to_vec()))
        }
    }

    struct Auth;

    #[async_trait]
    impl Authenticator for Auth {
        async fn authenticate(
            &self,
            _credential: &Credential,
        ) -> Result<ClientIdentity, AdapterError> {
            Ok(ClientIdentity {
                tenant_id: TenantId::new(),
                device_id: DeviceId::new(),
            })
        }
    }

    struct Authority;

    #[async_trait]
    impl AuthorityService for Authority {
        async fn commit(
            &self,
            _identity: ClientIdentity,
            _operation: EncodedOperation,
            outcome: Vec<u8>,
        ) -> Result<Vec<u8>, AdapterError> {
            Ok(outcome)
        }
    }

    struct TestOperation;

    impl Operation for TestOperation {
        type Outcome = ();

        const KIND: OperationKind = OperationKind::from_static(77);
        const SCHEMA_VERSION: OperationSchemaVersion = OperationSchemaVersion::from_static(1);

        fn encode(&self) -> Result<Vec<u8>, OperationEncodingError> {
            Ok(vec![7])
        }
    }

    #[test]
    fn duplicate_registration_fails_before_runtime_traffic() {
        let result = DomainRegistry::builder()
            .register(Handler)
            .register(Handler)
            .build();
        assert!(matches!(
            result,
            Err(DomainRegistrationError::DuplicateKind { .. })
        ));
    }

    #[tokio::test]
    async fn server_dispatches_without_http_or_database_types() {
        let registry = DomainRegistry::builder()
            .register(Handler)
            .build()
            .unwrap_or_else(|error| panic!("registry should build: {error}"));
        let server = AequoraServer::builder()
            .authority_store(Authority)
            .registry(registry)
            .authenticator(Auth)
            .build()
            .unwrap_or_else(|error| panic!("server should build: {error}"));
        let operation = EncodedOperation::from_operation(&TestOperation)
            .unwrap_or_else(|error| panic!("operation should encode: {error}"));
        assert_eq!(
            server.execute(&Credential::new("test"), operation).await,
            Ok(ServerOperationOutcome::Accepted(vec![7]))
        );
    }

    #[test]
    fn adapter_errors_remain_system_failures() {
        let error = AdapterError::new(AdapterErrorKind::Internal, "redacted");
        assert!(matches!(
            StableServerError::from(error),
            StableServerError::Adapter(_)
        ));
    }
}