connectorx/transports/
dummy_arrowstream.rs

1//! Transport from Dummy Source to Arrow Destination.
2
3use crate::destinations::arrowstream::{ArrowDestination, ArrowDestinationError, ArrowTypeSystem};
4use crate::sources::dummy::{DummySource, DummyTypeSystem};
5use crate::typesystem::TypeConversion;
6use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
7use thiserror::Error;
8
9/// Convert Dummy data types to Arrow data types.
10pub struct DummyArrowTransport;
11
12#[derive(Error, Debug)]
13pub enum DummyArrowTransportError {
14    #[error(transparent)]
15    Destination(#[from] ArrowDestinationError),
16
17    #[error(transparent)]
18    ConnectorX(#[from] crate::errors::ConnectorXError),
19}
20
21impl_transport!(
22    name = DummyArrowTransport,
23    error = DummyArrowTransportError,
24    systems = DummyTypeSystem => ArrowTypeSystem,
25    route = DummySource => ArrowDestination,
26    mappings = {
27        { F64[f64]                => Float64[f64]               | conversion auto}
28        { I64[i64]                => Int64[i64]                 | conversion auto}
29        { Bool[bool]              => Boolean[bool]              | conversion auto}
30        { String[String]          => LargeUtf8[String]          | conversion auto}
31        { DateTime[DateTime<Utc>] => Date64[NaiveDateTime]      | conversion option}
32    }
33);
34
35impl TypeConversion<DateTime<Utc>, NaiveDateTime> for DummyArrowTransport {
36    fn convert(val: DateTime<Utc>) -> NaiveDateTime {
37        val.naive_utc()
38    }
39}
40
41impl TypeConversion<NaiveDateTime, DateTime<Utc>> for DummyArrowTransport {
42    fn convert(val: NaiveDateTime) -> DateTime<Utc> {
43        DateTime::from_naive_utc_and_offset(val, Utc)
44    }
45}
46
47impl TypeConversion<NaiveDate, DateTime<Utc>> for DummyArrowTransport {
48    fn convert(val: NaiveDate) -> DateTime<Utc> {
49        DateTime::from_naive_utc_and_offset(
50            val.and_hms_opt(0, 0, 0)
51                .unwrap_or_else(|| panic!("and_hms_opt return None")),
52            Utc,
53        )
54    }
55}