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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use serde::{Deserialize, Serialize};
use std::{
	borrow::Cow, cmp::Ordering, collections::HashMap, fmt::Debug, hash::{BuildHasher, Hash, Hasher}, mem
};

#[cfg(feature = "parquet")]
use amadeus_parquet::ParquetData;
#[cfg(feature = "postgres")]
use amadeus_postgres::PostgresData;
#[cfg(feature = "amadeus-serde")]
use amadeus_serde::SerdeData;

#[cfg(not(feature = "parquet"))]
use std::any::Any as ParquetData;
#[cfg(not(feature = "postgres"))]
use std::any::Any as PostgresData;
#[cfg(not(feature = "amadeus-serde"))]
use std::any::Any as SerdeData;

pub use amadeus_derive::Data;
pub use amadeus_types::{
	AmadeusOrd, Bson, Date, DateTime, DateTimeWithoutTimezone, DateWithoutTimezone, Decimal, Downcast, DowncastFrom, Enum, Group, IpAddr, Json, List, Time, TimeWithoutTimezone, Timezone, Url, Value, Webpage
};

pub trait Data:
	Clone
	+ amadeus_types::Data
	+ AmadeusOrd
	+ ParquetData
	+ PostgresData
	+ SerdeData
	+ DowncastFrom<Value>
	+ Into<Value>
	+ Debug
	+ Send
	+ 'static
{
	fn size(&self) -> usize {
		mem::size_of::<Self>() + self.heap()
	}
	fn heap(&self) -> usize;
	fn cast<D: Data>(self) -> Result<D, CastError> {
		self.into().downcast().map_err(|_| CastError)
	}
	fn eq<D: Data>(self, other: D) -> bool {
		self.into() == other.into()
	}
	fn partial_cmp<D: Data>(self, other: D) -> Option<Ordering> {
		self.into().partial_cmp(other.into())
	}
	fn hash<H: Hasher>(self, state: &mut H) {
		<Value as Hash>::hash(&self.into(), state)
	}
}

pub struct CastError;

// impl<T> PartialEq<T> for dyn Data {
// 	fn eq(&self, other: &T) -> bool {
// 		other.eq(self)
// 	}
// }

impl<T> Data for Option<T>
where
	T: Data,
{
	fn heap(&self) -> usize {
		self.as_ref().map_or(0, Data::heap)
	}
}
impl<T> Data for Box<T>
where
	T: Data,
{
	fn heap(&self) -> usize {
		mem::size_of::<T>() + (**self).heap()
	}
}
impl<T> Data for List<T>
where
	T: Data,
{
	fn heap(&self) -> usize {
		todo!()
		// self.capacity() * mem::size_of::<T>() + self.iter()
	}
}
impl<K, V, S> Data for HashMap<K, V, S>
where
	K: Hash + Eq + Data,
	V: Data,
	S: BuildHasher + Clone + Default + Send + 'static,
{
	fn heap(&self) -> usize {
		self.capacity() * mem::size_of::<(K, V)>()
			+ self.iter().map(|(k, v)| k.heap() + v.heap()).sum::<usize>()
	}
}

macro_rules! impl_data {
	($($t:ty)*) => ($(
		impl Data for $t {
			fn heap(&self) -> usize {
				0
			}
		}
	)*);
}
impl_data!(bool u8 i8 u16 i16 u32 i32 u64 i64 f32 f64 Decimal Group Date DateWithoutTimezone Time TimeWithoutTimezone DateTime DateTimeWithoutTimezone Timezone IpAddr);

macro_rules! impl_data {
	($($t:ty)*) => ($(
		impl Data for $t {
			fn heap(&self) -> usize {
				self.capacity()
			}
		}
	)*);
}
impl_data!(String Bson Json Enum);

impl Data for Url {
	fn heap(&self) -> usize {
		self.as_str().len()
	}
}
impl Data for Webpage<'static> {
	fn heap(&self) -> usize {
		self.url.heap()
			+ if let Cow::Owned(slice) = &self.contents {
				slice.capacity()
			} else {
				0
			}
	}
}

impl Data for Value {
	fn heap(&self) -> usize {
		match self {
			Self::Bool(value) => value.heap(),
			Self::U8(value) => value.heap(),
			Self::I8(value) => value.heap(),
			Self::U16(value) => value.heap(),
			Self::I16(value) => value.heap(),
			Self::U32(value) => value.heap(),
			Self::I32(value) => value.heap(),
			Self::U64(value) => value.heap(),
			Self::I64(value) => value.heap(),
			Self::F32(value) => value.heap(),
			Self::F64(value) => value.heap(),
			Self::Date(value) => value.heap(),
			Self::DateWithoutTimezone(value) => value.heap(),
			Self::Time(value) => value.heap(),
			Self::TimeWithoutTimezone(value) => value.heap(),
			Self::DateTime(value) => value.heap(),
			Self::DateTimeWithoutTimezone(value) => value.heap(),
			Self::Timezone(value) => value.heap(),
			Self::Decimal(value) => value.heap(),
			Self::Bson(value) => value.heap(),
			Self::String(value) => value.heap(),
			Self::Json(value) => value.heap(),
			Self::Enum(value) => value.heap(),
			Self::Url(value) => value.heap(),
			Self::Webpage(value) => value.heap(),
			Self::IpAddr(value) => value.heap(),
			Self::List(value) => value.heap(),
			Self::Map(value) => value.heap(),
			Self::Group(value) => value.heap(),
			Self::Option(value) => value
				.as_ref()
				.map_or(0, |value| value.as_value(|value| value.heap())),
		}
	}
}

// Implement Record for common array lengths.
macro_rules! array {
	($($i:tt)*) => {$(
		impl Data for [u8; $i] {
			fn heap(&self) -> usize {
				0
			}
		}
		// TODO: fix parquet then impl<T> Data for [T; $i] where T: Data {}
	)*};
}
amadeus_types::array!(array);

// Implement Record on tuples up to length 12.
macro_rules! tuple {
	($len:tt $($t:ident $i:tt)*) => {
		impl<$($t,)*> Data for ($($t,)*) where $($t: Data,)* {
			fn heap(&self) -> usize {
				$(self.$i.heap() + )* 0
			}
		}
	};
}
amadeus_types::tuple!(tuple);

#[cfg(feature = "amadeus-serde")]
#[doc(hidden)]
pub mod serde_data {
	use super::SerdeData;
	use serde::{Deserializer, Serializer};

	pub fn serialize<T, S>(self_: &T, serializer: S) -> Result<S::Ok, S::Error>
	where
		T: SerdeData + ?Sized,
		S: Serializer,
	{
		self_.serialize(serializer)
	}
	pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
	where
		T: SerdeData,
		D: Deserializer<'de>,
	{
		T::deserialize(deserializer, None)
	}
}

#[derive(
	amadeus_derive::Data, Clone, Eq, PartialEq, PartialOrd, Hash, Serialize, Deserialize, Debug,
)]
#[amadeus(crate = "crate")]
pub struct CloudfrontRow {
	pub time: DateTime,
	pub edge_location: String,
	pub response_bytes: u64,
	pub remote_ip: IpAddr,
	// #[serde(with = "http_serde")]
	// pub method: Method,
	pub host: String,
	pub url: Url,
	// #[serde(with = "http_serde")]
	// pub status: Option<StatusCode>,
	pub user_agent: Option<String>,
	pub referer: Option<String>,
	pub cookie: Option<String>,
	pub result_type: String,
	pub request_id: String,
	pub request_bytes: u64,
	// pub time_taken: Duration,
	pub forwarded_for: Option<String>,
	pub ssl_protocol_cipher: Option<(String, String)>,
	pub response_result_type: String,
	pub http_version: String,
	pub fle_status: Option<String>,
	pub fle_encrypted_fields: Option<String>,
}
#[cfg(feature = "aws")]
impl From<amadeus_aws::CloudfrontRow> for CloudfrontRow {
	fn from(from: amadeus_aws::CloudfrontRow) -> Self {
		Self {
			time: from.time,
			edge_location: from.edge_location,
			response_bytes: from.response_bytes,
			remote_ip: from.remote_ip,
			// method: from.method,
			host: from.host,
			url: from.url,
			// status: from.status,
			user_agent: from.user_agent,
			referer: from.referer,
			cookie: from.cookie,
			result_type: from.result_type,
			request_id: from.request_id,
			request_bytes: from.request_bytes,
			// time_taken: from.time_taken,
			forwarded_for: from.forwarded_for,
			ssl_protocol_cipher: from.ssl_protocol_cipher,
			response_result_type: from.response_result_type,
			http_version: from.http_version,
			fle_status: from.fle_status,
			fle_encrypted_fields: from.fle_encrypted_fields,
		}
	}
}