fts_sqlite/
types.rs

1//! Type definitions for the SQLite implementation.
2//!
3//! This module contains both public types used throughout the crate and internal
4//! types used for database row mapping. The public types include strongly-typed
5//! IDs and datetime representations that ensure type safety across the system.
6
7use fts_core::models::{DemandCurve, DemandCurveDto, DemandGroup, Map, ProductGroup, ValueRecord};
8
9mod datetime;
10pub use datetime::DateTime;
11
12mod ids;
13pub use ids::{BidderId, DemandId, PortfolioId, ProductId};
14
15pub(crate) struct BatchData {
16    pub demands: Option<sqlx::types::Json<Map<DemandId, DemandCurveDto>>>,
17    pub portfolios: Option<
18        sqlx::types::Json<Map<PortfolioId, (DemandGroup<DemandId>, ProductGroup<ProductId>)>>,
19    >,
20}
21
22pub(crate) struct DemandRow<AppData> {
23    pub bidder_id: BidderId,
24    pub app_data: sqlx::types::Json<AppData>,
25    pub curve_data: Option<sqlx::types::Json<DemandCurveDto>>,
26    pub portfolio_group: Option<sqlx::types::Json<Map<PortfolioId>>>,
27}
28
29pub(crate) struct DemandHistoryRow {
30    pub valid_from: DateTime,
31    pub valid_until: Option<DateTime>,
32    pub curve_data: sqlx::types::Json<DemandCurveDto>,
33}
34
35impl Into<ValueRecord<DateTime, DemandCurve>> for DemandHistoryRow {
36    fn into(self) -> ValueRecord<DateTime, DemandCurve> {
37        ValueRecord {
38            valid_from: self.valid_from,
39            valid_until: self.valid_until,
40            value: unsafe { DemandCurve::new_unchecked(self.curve_data.0) },
41            // SAFETY: this is only being called when deserializing a SQL query, and we ensure curves
42            //         are valid going into the database.
43        }
44    }
45}
46
47pub(crate) struct PortfolioDemandHistoryRow {
48    pub valid_from: DateTime,
49    pub valid_until: Option<DateTime>,
50    pub demand_group: sqlx::types::Json<DemandGroup<DemandId>>,
51}
52
53impl Into<ValueRecord<DateTime, DemandGroup<DemandId>>> for PortfolioDemandHistoryRow {
54    fn into(self) -> ValueRecord<DateTime, DemandGroup<DemandId>> {
55        ValueRecord {
56            valid_from: self.valid_from,
57            valid_until: self.valid_until,
58            value: self.demand_group.0,
59        }
60    }
61}
62
63pub(crate) struct PortfolioProductHistoryRow {
64    pub valid_from: DateTime,
65    pub valid_until: Option<DateTime>,
66    pub product_group: sqlx::types::Json<ProductGroup<ProductId>>,
67}
68
69impl Into<ValueRecord<DateTime, ProductGroup<ProductId>>> for PortfolioProductHistoryRow {
70    fn into(self) -> ValueRecord<DateTime, ProductGroup<ProductId>> {
71        ValueRecord {
72            valid_from: self.valid_from,
73            valid_until: self.valid_until,
74            value: self.product_group.0,
75        }
76    }
77}
78
79pub(crate) struct PortfolioRow<AppData> {
80    pub bidder_id: BidderId,
81    pub app_data: sqlx::types::Json<AppData>,
82    pub demand_group: Option<sqlx::types::Json<DemandGroup<DemandId>>>,
83    pub product_group: Option<sqlx::types::Json<ProductGroup<ProductId>>>,
84}
85
86pub(crate) struct OutcomeRow<Outcome> {
87    pub as_of: DateTime,
88    pub outcome: sqlx::types::Json<Outcome>,
89}