openpit 0.5.0

Embeddable pre-trade risk SDK
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.

//! Engine builder types.

use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::marker::PhantomData;

use super::engine::{Engine, EngineInner};
use super::engine_trait::EngineTraitOf;
use super::sync_mode::{AccountSync, FullSync, LocalSync, SyncMode};
use super::{AccountCurrencies, AccountGroups, BlockedAccounts, ConfigRegistry};
use crate::marketdata::MarketDataSync;
use crate::pretrade::PreTradePolicy;
use crate::storage::{LockingPolicyFactory, StorageBuilder};

// ─── MarketDataSyncOf ────────────────────────────────────────────────────────

/// Maps each engine [`SyncMode`] to the appropriate market-data sync level.
///
/// `AccountSync` engines share market data across accounts and therefore
/// require `FullSync` access; `LocalSync` and `FullSync` engines keep the
/// market-data sync level aligned with their own.
mod market_data_sync_of {
    use super::{AccountSync, FullSync, LocalSync, MarketDataSync};

    pub trait MarketDataSyncOf {
        type Sync: MarketDataSync;

        /// Builds the market-data sync instance for this engine mode.
        fn market_data_sync() -> Self::Sync;
    }

    impl MarketDataSyncOf for LocalSync {
        type Sync = LocalSync;

        fn market_data_sync() -> LocalSync {
            LocalSync
        }
    }
    impl MarketDataSyncOf for FullSync {
        type Sync = FullSync;

        fn market_data_sync() -> FullSync {
            FullSync
        }
    }
    impl MarketDataSyncOf for AccountSync {
        type Sync = FullSync;

        fn market_data_sync() -> FullSync {
            FullSync
        }
    }
}
use market_data_sync_of::MarketDataSyncOf;

// ─── IntoPolicyObject ────────────────────────────────────────────────────────

/// Converts a concrete policy into the trait-object shape selected by a
/// [`SyncMode`].
///
/// Three blanket impls exist for the policy trait:
///
/// - `Target = dyn PreTradePolicy<...>`: `Policy: PreTradePolicy + 'static`.
/// - `Target = dyn PreTradePolicy<...> + Send`: `Policy: PreTradePolicy +
///   Send + 'static`.
/// - `Target = dyn PreTradePolicy<...> + Send + Sync`:
///   `Policy: PreTradePolicy + Send + Sync + 'static`.
///
/// Custom modes using another policy-object shape must provide a matching
/// implementation.
pub trait IntoPolicyObject<Target: ?Sized>: 'static {
    /// Converts `self` into a boxed policy object.
    fn into_policy_object(self) -> Box<Target>;
}

impl<
        Order: 'static,
        ExecutionReport: 'static,
        AccountAdjustment: 'static,
        Sync: SyncMode,
        PreTradePolicy: crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync> + 'static,
    >
    IntoPolicyObject<
        dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>,
    > for PreTradePolicy
{
    fn into_policy_object(
        self,
    ) -> Box<dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>>
    {
        Box::new(self)
    }
}

impl<
        Order: 'static,
        ExecutionReport: 'static,
        AccountAdjustment: 'static,
        Sync: SyncMode,
        PreTradePolicy: crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>
            + Send
            + 'static,
    >
    IntoPolicyObject<
        dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync> + Send,
    > for PreTradePolicy
{
    fn into_policy_object(
        self,
    ) -> Box<
        dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync> + Send,
    > {
        Box::new(self)
    }
}

impl<
        Order: 'static,
        ExecutionReport: 'static,
        AccountAdjustment: 'static,
        Sync: SyncMode,
        PreTradePolicy: crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>
            + Send
            + std::marker::Sync
            + 'static,
    >
    IntoPolicyObject<
        dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>
            + Send
            + std::marker::Sync,
    > for PreTradePolicy
{
    fn into_policy_object(
        self,
    ) -> Box<
        dyn crate::pretrade::PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Sync>
            + Send
            + std::marker::Sync,
    > {
        Box::new(self)
    }
}

// ─── EngineBuildError ────────────────────────────────────────────────────────

/// Errors returned by [`ReadyEngineBuilder::build`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum EngineBuildError {
    /// Duplicate policy name across registered policy sets.
    DuplicatePolicyName { name: String },
    /// Duplicate non-default `policy_group_id` across registered policies.
    ///
    /// Multiple policies may share [`crate::DEFAULT_POLICY_GROUP_ID`], but every
    /// explicit group identifier must be unique so that downstream consumers
    /// can route outcomes by group without ambiguity.
    DuplicatePolicyGroupId {
        policy_group_id: crate::core::PolicyGroupId,
    },
}

impl Display for EngineBuildError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::DuplicatePolicyName { name } => {
                write!(formatter, "duplicate policy name: {name}")
            }
            Self::DuplicatePolicyGroupId { policy_group_id } => {
                write!(
                    formatter,
                    "duplicate non-default policy_group_id: {value}",
                    value = policy_group_id.value()
                )
            }
        }
    }
}

impl std::error::Error for EngineBuildError {}

// ─── EngineBuilder ───────────────────────────────────────────────────────────

/// Fluent builder for [`Engine`].
///
/// Policies are evaluated in registration order. Policy names must be unique
/// across start-stage, main-stage, and account-adjustment sets;
/// [`ReadyEngineBuilder::build`] returns [`EngineBuildError::DuplicatePolicyName`]
/// otherwise.
///
/// # Examples
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::time::Duration;
/// use openpit::{WithExecutionReportOperation, WithFinancialImpact, WithOrderOperation};
/// use openpit::pretrade::policies::{
///     PnlBoundsAccountAssetBarrier, PnlBoundsBrokerBarrier, PnlBoundsKillSwitchPolicy,
///     PnlBoundsKillSwitchSettings, RateLimit, RateLimitBrokerBarrier, RateLimitPolicy,
///     RateLimitSettings,
/// };
/// use openpit::Engine;
/// use openpit::param::{AccountId, Asset, Pnl};
///
/// type MyOrder = WithOrderOperation<()>;
/// type MyReport = WithFinancialImpact<WithExecutionReportOperation<()>>;
///
/// let builder = Engine::builder::<MyOrder, MyReport, ()>().no_sync();
///
/// let pnl_policy = PnlBoundsKillSwitchPolicy::new(
///     PnlBoundsKillSwitchSettings::new(
///         [PnlBoundsBrokerBarrier {
///             settlement_asset: Asset::new("USD")?,
///             lower_bound: Some(Pnl::from_str("-500")?),
///             upper_bound: None,
///         }],
///         [PnlBoundsAccountAssetBarrier {
///             barrier: PnlBoundsBrokerBarrier {
///                 settlement_asset: Asset::new("USD")?,
///                 lower_bound: Some(Pnl::from_str("-200")?),
///                 upper_bound: None,
///             },
///             account_id: AccountId::from_u64(99224416),
///             initial_pnl: Pnl::from_str("-50")?,
///         }],
///     )?,
///     builder.storage_builder(),
/// );
///
/// let rate_policy = RateLimitPolicy::new(
///     RateLimitSettings::new(
///         Some(RateLimitBrokerBarrier {
///             limit: RateLimit { max_orders: 100, window: Duration::from_secs(1) },
///         }),
///         [],
///         [],
///         [],
///     )?,
///     builder.storage_builder(),
/// );
///
/// let engine = builder
///     .pre_trade(pnl_policy)
///     .pre_trade(rate_policy)
///     .build()?;
/// let _ = engine;
/// # Ok(())
/// # }
/// ```
pub struct EngineBuilder<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static> {
    _marker: PhantomData<(Order, ExecutionReport, AccountAdjustment)>,
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static>
    EngineBuilder<Order, ExecutionReport, AccountAdjustment>
{
    /// Creates a new engine builder.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            _marker: PhantomData,
        }
    }

    /// Applies a custom synchronization mode and advances to
    /// [`SyncedEngineBuilder`].
    ///
    /// The mode must implement [`SyncMode`] and is typically zero-sized. For the
    /// built-in regimes, prefer [`full_sync`](Self::full_sync),
    /// [`no_sync`](Self::no_sync), or
    /// [`account_sync`](Self::account_sync).
    pub fn sync<Sync>(
        self,
        sync: Sync,
    ) -> SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
    where
        Sync: SyncMode,
    {
        let storage_builder = StorageBuilder::new(sync.storage_locking_policy_factory());
        // Create the engine's blocked-accounts storage eagerly so the SAME
        // instance can be handed to policy constructors before `build()` and
        // moved into `EngineInner` at `build()`. The engine and every policy
        // therefore share exactly one instance.
        let blocked_accounts =
            <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::new_shared(
                BlockedAccounts::new(&storage_builder),
            );
        // Same lifecycle as `blocked_accounts`: one shared account-group
        // registry, created eagerly and moved into `EngineInner` at `build()`.
        let account_groups =
            <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::new_shared(
                AccountGroups::new(&storage_builder),
            );
        let account_currencies =
            <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::new_shared(
                AccountCurrencies::new(&storage_builder),
            );
        SyncedEngineBuilder {
            pre_trade_policies: Vec::new(),
            storage_builder,
            blocked_accounts,
            account_groups,
            account_currencies,
            config_entries: Vec::new(),
            _marker: PhantomData,
        }
    }

    /// Applies full thread-safety synchronization and advances to
    /// [`SyncedEngineBuilder`].
    ///
    /// Storage tables created by registered policies will use
    /// [`FullLocking`]: index and value domains are each protected by an
    /// independent reader-writer lock.
    ///
    /// [`FullLocking`]: crate::storage::FullLocking
    pub fn full_sync(
        self,
    ) -> SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, FullSync> {
        self.sync(FullSync)
    }

    /// Applies single-thread (no-sync) synchronization and advances to
    /// [`SyncedEngineBuilder`].
    ///
    /// Storage tables created by registered policies will use
    /// [`NoLocking`]: no synchronization primitives are allocated. The
    /// resulting storages are `!Send + !Sync`; this option is for
    /// single-threaded embeddings where synchronization overhead must be
    /// zero.
    ///
    /// [`NoLocking`]: crate::storage::NoLocking
    pub fn no_sync(
        self,
    ) -> SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, LocalSync> {
        self.sync(LocalSync)
    }

    /// Applies account-index synchronization and advances to
    /// [`SyncedEngineBuilder`].
    ///
    /// Storage tables created by registered policies will use
    /// [`IndexLocking`]: one reader-writer lock guards key insertions and
    /// removals; per-value access is the caller's responsibility. The engine
    /// handle is `Send + !Sync`: ownership may move between OS threads
    /// sequentially, but concurrent invocation on the same handle is not
    /// supported.
    ///
    /// [`IndexLocking`]: crate::storage::IndexLocking
    pub fn account_sync(
        self,
    ) -> SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, AccountSync> {
        self.sync(AccountSync)
    }
}

// ─── SyncedEngineBuilder ─────────────────────────────────────────────────────

/// Engine builder with a synchronization mode applied.
///
/// Obtained from [`EngineBuilder::sync`], [`EngineBuilder::full_sync`],
/// [`EngineBuilder::no_sync`], or [`EngineBuilder::account_sync`].
///
/// This builder deliberately has **no `build` method**: at least one policy
/// must be registered before the engine can be constructed. Adding any policy
/// advances to [`ReadyEngineBuilder`], which exposes [`build`](ReadyEngineBuilder::build).
///
/// The `Sync` type parameter carries the chosen [`SyncMode`]
/// forward through the builder chain so that trading policies can create
/// correctly-synchronized [`Storage`] tables without knowing the concrete
/// factory type.
///
/// [`Storage`]: crate::storage::Storage
pub struct SyncedEngineBuilder<
    Order: 'static,
    ExecutionReport: 'static,
    AccountAdjustment: 'static,
    Sync: SyncMode,
> {
    pre_trade_policies: Vec<
        Box<<Sync as SyncMode>::PreTradePolicyObject<Order, ExecutionReport, AccountAdjustment>>,
    >,
    storage_builder: StorageBuilder<<Sync as SyncMode>::StorageLockingPolicyFactory>,
    blocked_accounts:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            BlockedAccounts<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    account_groups:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            AccountGroups<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    account_currencies:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            AccountCurrencies<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    config_entries: Vec<(
        String,
        crate::core::ConfigEntry<<Sync as SyncMode>::StorageLockingPolicyFactory>,
    )>,
    _marker: PhantomData<(Order, ExecutionReport, AccountAdjustment)>,
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode,
{
    /// Returns the storage builder owned by this engine builder. Pass it (or
    /// a borrowed reference to it) to policy constructors that need internal
    /// storage tables. The factory type is shared with the engine builder's
    /// synchronization mode.
    pub fn storage_builder(
        &self,
    ) -> &StorageBuilder<<Sync as SyncMode>::StorageLockingPolicyFactory> {
        &self.storage_builder
    }
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode + MarketDataSyncOf,
{
    /// Creates a market-data service builder whose handle wrapper matches
    /// the engine's threading mode.
    ///
    /// The returned builder's `Sync` parameter controls both the handle
    /// wrapper distributed by the produced service ([`Rc`](std::rc::Rc) for
    /// [`LocalSync`], [`Arc`](std::sync::Arc) for [`FullSync`] and
    /// [`AccountSync`]) and the internal lock primitive. Under [`LocalSync`]
    /// the service is strictly single-threaded with no-op internal locks; a
    /// concurrent producer thread is **not** supported. [`FullSync`] and
    /// [`AccountSync`] engines map to a [`FullSync`] market-data service with
    /// real internal locks that does support a concurrent feed.
    ///
    /// `default_ttl` sets the service-wide quote lifetime; pick
    /// [`QuoteTtl::Infinite`](crate::QuoteTtl::Infinite) for never-expiring
    /// quotes or [`QuoteTtl::Within(d)`](crate::QuoteTtl::Within) to drop
    /// stale snapshots after `d`.
    pub fn market_data(
        &self,
        default_ttl: crate::QuoteTtl,
    ) -> crate::marketdata::MarketDataBuilder<<Sync as MarketDataSyncOf>::Sync> {
        crate::marketdata::MarketDataBuilder::with_sync(Sync::market_data_sync(), default_ttl)
    }
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    SyncedEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode,
{
    /// Registers a policy and advances to [`ReadyEngineBuilder`].
    ///
    /// The required bound on `Policy` is determined by the `SyncMode`'s
    /// policy-object shape:
    ///
    /// - [`LocalSync`] (from `no_sync`): `'static` only; `!Send`
    ///   policy state is accepted.
    /// - [`AccountSync`] (from `account_sync`): `Send + 'static`.
    /// - [`FullSync`] (from `full_sync`): `Send + Sync + 'static`.
    ///
    /// For a supported built-in policy, the builder captures a clone of its
    /// settings cell under the policy's
    /// [`name`](crate::pretrade::PreTradePolicy::name). The built engine can
    /// retune it through [`Engine::configure`](crate::Engine::configure), and
    /// the policy observes the update on its next hot-path read. Custom
    /// policies are registered normally but are not runtime-configurable in
    /// this release.
    pub fn pre_trade<Policy>(
        mut self,
        policy: Policy,
    ) -> ReadyEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
    where
        Policy: IntoPolicyObject<
            <Sync as SyncMode>::PreTradePolicyObject<Order, ExecutionReport, AccountAdjustment>,
        >,
    {
        let obj = policy.into_policy_object();
        if let Some(entry) = obj.built_in_config_entry() {
            self.config_entries.push((obj.name().to_owned(), entry));
        }
        self.pre_trade_policies.push(obj);
        ReadyEngineBuilder {
            pre_trade_policies: self.pre_trade_policies,
            storage_builder: self.storage_builder,
            blocked_accounts: self.blocked_accounts,
            account_groups: self.account_groups,
            account_currencies: self.account_currencies,
            config_entries: self.config_entries,
            _marker: PhantomData,
        }
    }
}

// ─── ReadyEngineBuilder ──────────────────────────────────────────────────────

/// Engine builder with a synchronization mode and at least one trading
/// policy registered. Can produce an [`Engine`] via [`build`](Self::build).
///
/// Obtained from the `add_policy` methods on [`SyncedEngineBuilder`] or
/// from the chained `add_policy` methods on this type itself.
///
/// The `Sync` type parameter carries the chosen [`SyncMode`]
/// to any code that needs to create additional [`Storage`] tables with the
/// same synchronization regime.
///
/// [`Storage`]: crate::storage::Storage
pub struct ReadyEngineBuilder<
    Order: 'static,
    ExecutionReport: 'static,
    AccountAdjustment: 'static,
    Sync: SyncMode,
> {
    pre_trade_policies: Vec<
        Box<<Sync as SyncMode>::PreTradePolicyObject<Order, ExecutionReport, AccountAdjustment>>,
    >,
    storage_builder: StorageBuilder<<Sync as SyncMode>::StorageLockingPolicyFactory>,
    blocked_accounts:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            BlockedAccounts<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    account_groups:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            AccountGroups<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    account_currencies:
        <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::Shared<
            AccountCurrencies<<Sync as SyncMode>::StorageLockingPolicyFactory>,
        >,
    config_entries: Vec<(
        String,
        crate::core::ConfigEntry<<Sync as SyncMode>::StorageLockingPolicyFactory>,
    )>,
    _marker: PhantomData<(Order, ExecutionReport, AccountAdjustment)>,
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    ReadyEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode,
{
    /// Returns the storage builder owned by this engine builder. Pass it (or
    /// a borrowed reference to it) to policy constructors that need internal
    /// storage tables. The factory type is shared with the engine builder's
    /// synchronization mode.
    pub fn storage_builder(
        &self,
    ) -> &StorageBuilder<<Sync as SyncMode>::StorageLockingPolicyFactory> {
        &self.storage_builder
    }
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    ReadyEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode + MarketDataSyncOf,
{
    /// See [`SyncedEngineBuilder::market_data`].
    pub fn market_data(
        &self,
        default_ttl: crate::QuoteTtl,
    ) -> crate::marketdata::MarketDataBuilder<<Sync as MarketDataSyncOf>::Sync> {
        crate::marketdata::MarketDataBuilder::with_sync(Sync::market_data_sync(), default_ttl)
    }
}

impl<Order: 'static, ExecutionReport: 'static, AccountAdjustment: 'static, Sync>
    ReadyEngineBuilder<Order, ExecutionReport, AccountAdjustment, Sync>
where
    Sync: SyncMode,
{
    /// Registers an additional policy.
    ///
    /// Supported built-in policies are registered with their runtime settings
    /// cell. Custom policies are registered normally but are not
    /// runtime-configurable in this release.
    pub fn pre_trade<Policy>(mut self, policy: Policy) -> Self
    where
        Policy: IntoPolicyObject<
            <Sync as SyncMode>::PreTradePolicyObject<Order, ExecutionReport, AccountAdjustment>,
        >,
    {
        let obj = policy.into_policy_object();
        if let Some(entry) = obj.built_in_config_entry() {
            self.config_entries.push((obj.name().to_owned(), entry));
        }
        self.pre_trade_policies.push(obj);
        self
    }

    /// Builds the engine.
    pub fn build(
        self,
    ) -> Result<
        Engine<EngineTraitOf<Order, ExecutionReport, AccountAdjustment, Sync>>,
        EngineBuildError,
    > {
        ensure_unique_policy_names(self.pre_trade_policies.iter().map(|p| p.name()))?;
        ensure_unique_group_ids(self.pre_trade_policies.iter().map(|p| p.policy_group_id()))?;
        // Duplicate policy names are rejected above, so the registry's keys are
        // already unique; collecting into the map cannot drop a distinct cell.
        let config_registry =
            <<Sync as SyncMode>::StorageLockingPolicyFactory as LockingPolicyFactory>::new_shared(
                ConfigRegistry::from_entries(self.config_entries.into_iter().collect()),
            );
        Ok(Engine::from_inner(<Sync as SyncMode>::new_strong(
            EngineInner {
                pre_trade_policies: self.pre_trade_policies,
                blocked_accounts: self.blocked_accounts,
                account_groups: self.account_groups,
                account_currencies: self.account_currencies,
                config_registry,
            },
        )))
    }
}

fn ensure_unique_policy_names<'a>(
    names: impl Iterator<Item = &'a str>,
) -> Result<(), EngineBuildError> {
    let mut unique = HashSet::new();
    for name in names {
        if !unique.insert(name.to_owned()) {
            return Err(EngineBuildError::DuplicatePolicyName {
                name: name.to_owned(),
            });
        }
    }

    Ok(())
}

fn ensure_unique_group_ids(
    group_ids: impl Iterator<Item = crate::core::PolicyGroupId>,
) -> Result<(), EngineBuildError> {
    let mut unique = HashSet::new();
    for policy_group_id in group_ids {
        if policy_group_id == crate::core::DEFAULT_POLICY_GROUP_ID {
            continue;
        }
        if !unique.insert(policy_group_id) {
            return Err(EngineBuildError::DuplicatePolicyGroupId { policy_group_id });
        }
    }

    Ok(())
}