Skip to main content

nautilus_infrastructure/sql/models/
types.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_common::signal::Signal;
17use nautilus_core::UnixNanos;
18use nautilus_model::types::Currency;
19use sqlx::{FromRow, Row, postgres::PgRow};
20use ustr::Ustr;
21
22use crate::sql::models::{enums::CurrencyTypePg, read_u8, read_u16};
23
24#[derive(Debug)]
25pub struct CurrencyRow(pub Currency);
26
27#[derive(Debug)]
28pub struct SignalRow(pub Signal);
29
30impl<'r> FromRow<'r, PgRow> for CurrencyRow {
31    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
32        let id = row.try_get::<String, _>("id")?;
33        let precision = read_u8(row, "precision")?;
34        let iso4217 = read_u16(row, "iso4217")?;
35        let name = row.try_get::<String, _>("name")?;
36        let currency_type = row.try_get::<CurrencyTypePg, _>("currency_type")?;
37
38        let currency = Currency::new_checked(
39            id.as_str(),
40            precision,
41            iso4217,
42            name.as_str(),
43            currency_type.0,
44        )
45        .map_err(|e| sqlx::Error::ColumnDecode {
46            index: "currency".into(),
47            source: Box::new(e),
48        })?;
49
50        Ok(Self(currency))
51    }
52}
53
54impl<'r> FromRow<'r, PgRow> for SignalRow {
55    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
56        let name = row.try_get::<&str, _>("name").map(Ustr::from)?;
57        let value = row.try_get::<String, _>("value")?;
58        let ts_event = row.try_get::<&str, _>("ts_event").map(UnixNanos::from)?;
59        let ts_init = row.try_get::<&str, _>("ts_init").map(UnixNanos::from)?;
60        let signal = Signal::new(name, value, ts_event, ts_init);
61        Ok(Self(signal))
62    }
63}