connectorx/destinations/arrowstream/
funcs.rs

1use super::arrow_assoc::ArrowAssoc;
2use super::Builder;
3use crate::errors::Result;
4use crate::typesystem::{ParameterizedFunc, ParameterizedOn};
5use anyhow::anyhow;
6use arrow::array::{ArrayBuilder, ArrayRef};
7use arrow::datatypes::Field;
8
9pub struct FNewBuilder;
10
11impl ParameterizedFunc for FNewBuilder {
12    type Function = fn(nrows: usize) -> Builder;
13}
14
15impl<T> ParameterizedOn<T> for FNewBuilder
16where
17    T: ArrowAssoc,
18{
19    fn parameterize() -> Self::Function {
20        fn imp<T>(nrows: usize) -> Builder
21        where
22            T: ArrowAssoc,
23        {
24            Box::new(T::builder(nrows)) as Builder
25        }
26        imp::<T>
27    }
28}
29
30pub struct FFinishBuilder;
31
32impl ParameterizedFunc for FFinishBuilder {
33    type Function = fn(Builder) -> Result<ArrayRef>;
34}
35
36impl<T> ParameterizedOn<T> for FFinishBuilder
37where
38    T: ArrowAssoc,
39{
40    fn parameterize() -> Self::Function {
41        fn imp<T>(mut builder: Builder) -> Result<ArrayRef>
42        where
43            T: ArrowAssoc,
44        {
45            let t = builder
46                .downcast_mut::<T::Builder>()
47                .ok_or_else(|| anyhow!("cannot cast arrow builder for finish"))?;
48            let a = ArrayBuilder::finish(t);
49            Ok(a)
50        }
51        imp::<T>
52    }
53}
54
55pub struct FNewField;
56
57impl ParameterizedFunc for FNewField {
58    type Function = fn(header: &str) -> Field;
59}
60
61impl<T> ParameterizedOn<T> for FNewField
62where
63    T: ArrowAssoc,
64{
65    fn parameterize() -> Self::Function {
66        fn imp<T>(header: &str) -> Field
67        where
68            T: ArrowAssoc,
69        {
70            T::field(header)
71        }
72        imp::<T>
73    }
74}