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::CurrencyTypeModel;
23
24#[derive(Debug)]
25pub struct CurrencyModel(pub Currency);
26
27#[derive(Debug)]
28pub struct SignalModel(pub Signal);
29
30impl<'r> FromRow<'r, PgRow> for CurrencyModel {
31    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
32        let id = row.try_get::<String, _>("id")?;
33        let precision = row.try_get::<i32, _>("precision")?;
34        let iso4217 = row.try_get::<i32, _>("iso4217")?;
35        let name = row.try_get::<String, _>("name")?;
36        let currency_type_model = row.try_get::<CurrencyTypeModel, _>("currency_type")?;
37        let currency = Currency::new(
38            id.as_str(),
39            precision as u8,
40            iso4217 as u16,
41            name.as_str(),
42            currency_type_model.0,
43        );
44        Ok(Self(currency))
45    }
46}
47
48impl<'r> FromRow<'r, PgRow> for SignalModel {
49    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
50        let name = row.try_get::<&str, _>("name").map(Ustr::from)?;
51        let value = row.try_get::<String, _>("value")?;
52        let ts_event = row.try_get::<&str, _>("ts_event").map(UnixNanos::from)?;
53        let ts_init = row.try_get::<&str, _>("ts_init").map(UnixNanos::from)?;
54        let signal = Signal::new(name, value, ts_event, ts_init);
55        Ok(Self(signal))
56    }
57}