1use crate::destinations::{Consume, Destination, DestinationPartition};
8use crate::errors::{ConnectorXError, Result as CXResult};
9use crate::sources::{PartitionParser, Produce, Source, SourcePartition};
10
11#[doc(hidden)]
12pub trait TypeSystem: Copy + Clone + Send + Sync {
16 fn check<T: TypeAssoc<Self>>(self) -> CXResult<()> {
18 T::check(self)
19 }
20}
21
22#[doc(hidden)]
23pub trait TypeAssoc<TS: TypeSystem> {
25 fn check(ts: TS) -> CXResult<()>;
26}
27
28#[doc(hidden)]
29pub trait Realize<F>
31where
32 F: ParameterizedFunc,
33{
34 fn realize(self) -> CXResult<F::Function>;
36}
37
38#[doc(hidden)]
39pub trait ParameterizedFunc {
44 type Function;
45 fn realize<T>() -> Self::Function
46 where
47 Self: ParameterizedOn<T>,
48 {
49 Self::parameterize()
50 }
51}
52
53#[doc(hidden)]
54pub trait ParameterizedOn<T>: ParameterizedFunc {
57 fn parameterize() -> Self::Function;
58}
59
60pub trait TypeConversion<T, U> {
62 fn convert(val: T) -> U;
64}
65
66pub trait Transport {
70 type TSS: TypeSystem;
72 type TSD: TypeSystem;
74 type S: Source;
76 type D: Destination;
78 type Error: From<ConnectorXError>
80 + From<<Self::S as Source>::Error>
81 + From<<Self::D as Destination>::Error>
82 + Send
83 + std::fmt::Debug;
84
85 fn convert_typesystem(ts: Self::TSS) -> CXResult<Self::TSD>;
88
89 fn convert_type<T1, T2>(val: T1) -> T2
92 where
93 Self: TypeConversion<T1, T2>,
94 {
95 <Self as TypeConversion<T1, T2>>::convert(val)
96 }
97
98 fn process<'s, 'd, 'r>(
102 ts1: Self::TSS,
103 ts2: Self::TSD,
104 src: &'r mut <<Self::S as Source>::Partition as SourcePartition>::Parser<'s>,
105 dst: &'r mut <Self::D as Destination>::Partition<'d>,
106 ) -> Result<(), Self::Error>
107 where
108 Self: 'd;
109
110 #[allow(clippy::type_complexity)]
113 fn processor<'s, 'd>(
114 ts1: Self::TSS,
115 ts2: Self::TSD,
116 ) -> CXResult<
117 fn(
118 src: &mut <<Self::S as Source>::Partition as SourcePartition>::Parser<'s>,
119 dst: &mut <Self::D as Destination>::Partition<'d>,
120 ) -> Result<(), Self::Error>,
121 >
122 where
123 Self: 'd;
124}
125
126#[doc(hidden)]
127pub fn process<'s, 'd, 'r, T1, T2, TP, S, D, ES, ED, ET>(
128 src: &'r mut <<S as Source>::Partition as SourcePartition>::Parser<'s>,
129 dst: &'r mut <D as Destination>::Partition<'d>,
130) -> Result<(), ET>
131where
132 T1: TypeAssoc<<S as Source>::TypeSystem>,
133 S: Source<Error = ES>,
134 <S as Source>::Partition: SourcePartition<Error = ES>,
135
136 <<S as Source>::Partition as SourcePartition>::Parser<'s>: Produce<'r, T1, Error = ES>,
137 ES: From<ConnectorXError> + Send,
138
139 T2: TypeAssoc<<D as Destination>::TypeSystem>,
140 D: Destination<Error = ED>,
141 <D as Destination>::Partition<'d>: Consume<T2, Error = ED>,
142 ED: From<ConnectorXError> + Send,
143
144 TP: TypeConversion<T1, T2>,
145 ET: From<ES> + From<ED>,
146{
147 let val: T1 = PartitionParser::parse(src)?;
148 let val: T2 = <TP as TypeConversion<T1, _>>::convert(val);
149 DestinationPartition::write(dst, val)?;
150 Ok(())
151}
152
153#[cfg(all(test, any(feature = "src_mysql", feature = "src_postgres")))]
156pub(crate) mod testing {
157 use super::*;
158 use std::marker::PhantomData;
159
160 pub(crate) struct Check<TS>(PhantomData<TS>);
162
163 impl<TS> ParameterizedFunc for Check<TS> {
164 type Function = fn(TS) -> CXResult<()>;
165 }
166
167 impl<TS: TypeSystem, T: TypeAssoc<TS>> ParameterizedOn<T> for Check<TS> {
168 fn parameterize() -> Self::Function {
169 T::check
170 }
171 }
172
173 pub(crate) struct Name;
175
176 impl ParameterizedFunc for Name {
177 type Function = fn() -> &'static str;
178 }
179
180 impl<T> ParameterizedOn<T> for Name {
181 fn parameterize() -> Self::Function {
182 std::any::type_name::<T>
183 }
184 }
185
186 pub(crate) fn assert_maps<TS>(ts: fn(bool) -> TS, rust_type: &str)
190 where
191 TS: TypeSystem + Realize<Check<TS>> + Realize<Name> + std::fmt::Debug,
192 {
193 let (non_null, nullable) = (ts(false), ts(true));
194
195 let name = Realize::<Name>::realize(non_null).unwrap()();
196 assert!(
197 name.ends_with(rust_type) && !name.starts_with("core::option::Option"),
198 "{non_null:?} realizes to {name}, expected {rust_type}"
199 );
200 let name = Realize::<Name>::realize(nullable).unwrap()();
201 assert!(
202 name.starts_with("core::option::Option<") && name.ends_with(&format!("{rust_type}>")),
203 "{nullable:?} realizes to {name}, expected Option<{rust_type}>"
204 );
205
206 for (ts, other) in [(non_null, nullable), (nullable, non_null)] {
207 let check = Realize::<Check<TS>>::realize(ts).unwrap();
208 assert!(check(ts).is_ok(), "{ts:?} does not check against itself");
209 let err = check(other).unwrap_err();
210 assert!(
211 matches!(err, crate::errors::ConnectorXError::TypeCheckFailed(..)),
212 "{err}"
213 );
214 }
215 }
216}