use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
use serde::{Deserialize, Serialize};
pub struct MicroSeconds;
impl rkyv::with::ArchiveWith<chrono::DateTime<chrono::Utc>> for MicroSeconds {
type Archived = rkyv::Archived<i64>;
type Resolver = ();
fn resolve_with(
field: &chrono::DateTime<chrono::Utc>,
_: (),
out: rkyv::Place<Self::Archived>,
) {
let ts = field.timestamp_micros();
ts.resolve((), out);
}
}
impl<S: rkyv::rancor::Fallible + ?Sized> rkyv::with::SerializeWith<chrono::DateTime<chrono::Utc>, S>
for MicroSeconds
{
fn serialize_with(
field: &chrono::DateTime<chrono::Utc>,
serializer: &mut S,
) -> Result<Self::Resolver, S::Error> {
RkyvSerialize::serialize(&field.timestamp_micros(), serializer)
}
}
impl<D: rkyv::rancor::Fallible + ?Sized>
rkyv::with::DeserializeWith<rkyv::Archived<i64>, chrono::DateTime<chrono::Utc>, D>
for MicroSeconds
{
fn deserialize_with(
archived: &rkyv::Archived<i64>,
_deserializer: &mut D,
) -> Result<chrono::DateTime<chrono::Utc>, D::Error> {
use chrono::TimeZone;
let ts: i64 = (*archived).into();
Ok(chrono::Utc.timestamp_micros(ts).single().unwrap())
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct Field {
pub value: FieldValue,
pub option: FieldOption,
}
impl Field {
pub fn new(value: FieldValue, option: FieldOption) -> Self {
Self { value, option }
}
pub fn with_default_option(value: FieldValue) -> Self {
let option = FieldOption::from_field_value(&value);
Self { value, option }
}
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Serialize,
Deserialize,
Archive,
RkyvSerialize,
RkyvDeserialize,
)]
pub enum NumericType {
Integer,
Float,
}
pub type FieldValue = crate::data::DataValue;
fn default_true() -> bool {
true
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct TextOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
#[serde(default = "default_true")]
pub term_vectors: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[rkyv(with = rkyv::with::Skip)]
pub analyzer: Option<String>,
}
impl TextOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
pub fn term_vectors(mut self, term_vectors: bool) -> Self {
self.term_vectors = term_vectors;
self
}
pub fn analyzer(mut self, name: impl Into<String>) -> Self {
self.analyzer = Some(name.into());
self
}
}
impl Default for TextOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
term_vectors: false,
analyzer: None,
}
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct BytesOption {
#[serde(default = "default_true")]
pub stored: bool,
}
impl BytesOption {
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
impl Default for BytesOption {
fn default() -> Self {
Self { stored: true }
}
}
impl BytesOption {
pub fn new() -> Self {
Self::default()
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct IntegerOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
}
impl IntegerOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
impl Default for IntegerOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
}
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct FloatOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
}
impl FloatOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
impl Default for FloatOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
}
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct BooleanOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
}
impl BooleanOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
impl Default for BooleanOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
}
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct DateTimeOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
}
impl DateTimeOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
impl Default for DateTimeOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
}
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub struct GeoOption {
#[serde(default = "default_true")]
pub indexed: bool,
#[serde(default = "default_true")]
pub stored: bool,
}
impl GeoOption {
pub fn indexed(mut self, indexed: bool) -> Self {
self.indexed = indexed;
self
}
pub fn stored(mut self, stored: bool) -> Self {
self.stored = stored;
self
}
}
#[derive(
Debug, Clone, PartialEq, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize,
)]
pub enum FieldOption {
Text(TextOption),
Integer(IntegerOption),
Float(FloatOption),
Boolean(BooleanOption),
Bytes(BytesOption),
DateTime(DateTimeOption),
Geo(GeoOption),
}
impl Default for FieldOption {
fn default() -> Self {
FieldOption::Text(TextOption::default())
}
}
impl FieldOption {
pub fn from_field_value(value: &FieldValue) -> Self {
match value {
FieldValue::Text(_) => FieldOption::Text(TextOption::default()),
FieldValue::Int64(_) => FieldOption::Integer(IntegerOption::default()),
FieldValue::Float64(_) => FieldOption::Float(FloatOption::default()),
FieldValue::Bool(_) => FieldOption::Boolean(BooleanOption::default()),
FieldValue::Vector(_) | FieldValue::Bytes(_, _) => {
FieldOption::Bytes(BytesOption::default())
}
FieldValue::DateTime(_) => FieldOption::DateTime(DateTimeOption::default()),
FieldValue::Geo(_, _) => FieldOption::Geo(GeoOption::default()),
FieldValue::Null => FieldOption::Text(TextOption::default()),
}
}
}
impl Default for GeoOption {
fn default() -> Self {
Self {
indexed: true,
stored: true,
}
}
}