1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#![allow(clippy::unsafe_derive_deserialize)] use ::serde::{Deserialize, Serialize}; use futures::pin_mut; use pin_project::pin_project; use std::{ pin::Pin, task::{Context, Poll} }; use crate::{ dist_pipe::DistributedPipe, dist_sink::DistributedSink, dist_stream::{DistributedStream, StreamTask, StreamTaskAsync} }; #[cfg(feature = "aws")] #[doc(inline)] pub use amadeus_aws::Cloudfront; #[cfg(feature = "aws")] pub mod aws { pub use crate::data::CloudfrontRow; #[doc(inline)] pub use amadeus_aws::{AwsCredentials, AwsError, AwsRegion, S3Directory, S3File}; } #[cfg(feature = "commoncrawl")] #[doc(inline)] pub use amadeus_commoncrawl::{self as commoncrawl, CommonCrawl}; #[cfg(feature = "parquet")] #[doc(inline)] pub use amadeus_parquet::{self as parquet, Parquet, ParquetDirectory}; #[cfg(feature = "postgres")] #[doc(inline)] pub use amadeus_postgres::{self as postgres, Postgres}; #[cfg(feature = "amadeus-serde")] #[doc(inline)] pub use amadeus_serde::{self as serde, Csv, Json}; pub trait Source { type Item: crate::data::Data; type Error: std::error::Error; type DistStream: DistributedStream<Item = Result<Self::Item, Self::Error>>; fn dist_stream(self) -> Self::DistStream; } pub trait Destination<I> where I: DistributedPipe<Self::Item>, { type Item: crate::data::Data; type Error: std::error::Error; type DistSink: DistributedSink<I, Self::Item, Result<(), Self::Error>>; } #[cfg(feature = "amadeus-serde")] impl<File, Row> Source for Json<File, Row> where File: amadeus_core::file::File, Row: super::data::Data, { type Item = <Self as amadeus_core::Source>::Item; type Error = <Self as amadeus_core::Source>::Error; type DistStream = <Self as amadeus_core::Source>::DistStream; fn dist_stream(self) -> Self::DistStream { <Self as amadeus_core::Source>::dist_stream(self) } } #[cfg(feature = "amadeus-serde")] impl<File, Row> Source for Csv<File, Row> where File: amadeus_core::file::File, Row: super::data::Data, { type Item = <Self as amadeus_core::Source>::Item; type Error = <Self as amadeus_core::Source>::Error; type DistStream = <Self as amadeus_core::Source>::DistStream; fn dist_stream(self) -> Self::DistStream { <Self as amadeus_core::Source>::dist_stream(self) } } #[cfg(feature = "parquet")] impl<File, Row> Source for Parquet<File, Row> where File: amadeus_core::file::File, Row: super::data::Data, { type Item = <Self as amadeus_core::Source>::Item; type Error = <Self as amadeus_core::Source>::Error; type DistStream = <Self as amadeus_core::Source>::DistStream; fn dist_stream(self) -> Self::DistStream { <Self as amadeus_core::Source>::dist_stream(self) } } #[cfg(feature = "postgres")] impl<Row> Source for Postgres<Row> where Row: super::data::Data, { type Item = <Self as amadeus_core::Source>::Item; type Error = <Self as amadeus_core::Source>::Error; type DistStream = <Self as amadeus_core::Source>::DistStream; fn dist_stream(self) -> Self::DistStream { <Self as amadeus_core::Source>::dist_stream(self) } } #[cfg(feature = "aws")] impl Source for Cloudfront { type Item = crate::data::CloudfrontRow; type Error = <Self as amadeus_core::Source>::Error; type DistStream = IntoIter<<Self as amadeus_core::Source>::DistStream, Self::Item>; fn dist_stream(self) -> Self::DistStream { IntoIter( <Self as amadeus_core::Source>::dist_stream(self), PhantomData, ) } } #[cfg(feature = "commoncrawl")] impl Source for CommonCrawl { type Item = amadeus_types::Webpage<'static>; type Error = <Self as amadeus_core::Source>::Error; type DistStream = IntoIter<<Self as amadeus_core::Source>::DistStream, Self::Item>; fn dist_stream(self) -> Self::DistStream { IntoIter( <Self as amadeus_core::Source>::dist_stream(self), PhantomData, ) } } use std::marker::PhantomData; pub struct IntoIter<I, U>(I, PhantomData<fn() -> U>); impl<I, T, E, U> DistributedStream for IntoIter<I, U> where I: DistributedStream<Item = Result<T, E>>, T: Into<U>, U: 'static, { type Item = Result<U, E>; type Task = IntoTask<I::Task, U>; fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() } fn next_task(&mut self) -> Option<Self::Task> { self.0.next_task().map(|task| IntoTask { task, marker: PhantomData, }) } } #[pin_project] #[derive(Serialize, Deserialize)] #[serde(transparent)] pub struct IntoTask<I, U> { #[pin] task: I, #[serde(skip)] marker: PhantomData<fn() -> U>, } impl<I, T, E, U> StreamTask for IntoTask<I, U> where I: StreamTask<Item = Result<T, E>>, T: Into<U>, { type Item = Result<U, E>; type Async = IntoTask<I::Async, U>; fn into_async(self) -> Self::Async { IntoTask { task: self.task.into_async(), marker: self.marker, } } } impl<I, T, E, U> StreamTaskAsync for IntoTask<I, U> where I: StreamTaskAsync<Item = Result<T, E>>, T: Into<U>, { type Item = Result<U, E>; fn poll_run( self: Pin<&mut Self>, cx: &mut Context, sink: Pin<&mut impl amadeus_core::sink::Sink<Self::Item>>, ) -> Poll<()> { let sink = amadeus_core::sink::SinkMap::new(sink, |item: Result<_, _>| item.map(Into::into)); pin_mut!(sink); self.project().task.poll_run(cx, sink) } } impl<I, T, E, U> Iterator for IntoIter<I, U> where I: Iterator<Item = Result<T, E>>, T: Into<U>, { type Item = Result<U, E>; fn next(&mut self) -> Option<Self::Item> { self.0.next().map(|x| x.map(Into::into)) } }