Skip to content

Commit

Permalink
refactored and fixed exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
evanxg852000 committed Jun 24, 2022
1 parent 73b19e9 commit 4b1ce74
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 201 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async-trait = "0.1.53"
derivative = "2.2.0"
time = { version = "0.3.10", features = ["std", "macros", "serde-well-known"] }
chrono = "0.4.19"
chrono-tz = "0.6"

[target.'cfg(windows)'.dependencies]
winapi = "0.3.9"
Expand Down
2 changes: 1 addition & 1 deletion examples/date_time_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> tantivy::Result<()> {
for (_score, doc_address) in count_docs {
let retrieved_doc = searcher.doc(doc_address)?;
assert!(matches!(retrieved_doc.get_first(occurred_at),
Some(Value::DateTime(dt)) if dt.precision == DateTimePrecision::Seconds));
Some(Value::DateTime(dt)) if dt.get_precision() == DateTimePrecision::Seconds));
assert_eq!(
schema.to_json(&retrieved_doc),
r#"{"event_type":["double-click"],"occurred_at":["2022-06-22T13:00:00Z"]}"#
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub use time;

pub use crate::error::TantivyError;
pub use crate::future_result::FutureResult;
pub use crate::schema::{DateTime, DateTimePrecision};
pub use crate::schema::{DateTime, DateTimeFormat, DateTimeOptions, DateTimePrecision};

/// Tantivy result.
///
Expand Down
50 changes: 15 additions & 35 deletions src/schema/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{fmt, io};
use byteorder::{ReadBytesExt, WriteBytesExt};
use chrono::NaiveDate;
use common::BinarySerializable;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Serialize};

use super::date_time_options::DateTimeFormat;
use crate::time::format_description::well_known::{Iso8601, Rfc2822, Rfc3339};
Expand Down Expand Up @@ -102,12 +101,22 @@ impl DateTime {
}

/// Converts to the underlying precise timestamp.
/// Seconds, Milliseconds, Microseconds, Nanoseconds.
/// TODO: deprecate or remove
pub const fn into_timestamp(self) -> i64 {
let Self { timestamp, .. } = self;
timestamp
}

/// Returns to the underlying timestamp.
pub fn get_timestamp(&self) -> i64 {
self.timestamp
}

/// Returns the underlying timestamp precision.
pub fn get_precision(&self) -> DateTimePrecision {
self.precision
}

/// Convert to UTC `OffsetDateTime`
pub fn into_utc(self) -> OffsetDateTime {
let Self {
Expand Down Expand Up @@ -157,7 +166,7 @@ impl DateTime {
.ok_or_else(|| "Couldn't create NaiveDate from OffsetDateTime".to_string())
.map(|datetime| datetime.format(&str_fmt).to_string())
}
DateTimeFormat::UnixTimestamp(_) => Ok(self.timestamp.to_string()),
DateTimeFormat::Timestamp(_) => Ok(self.timestamp.to_string()),
}
}
}
Expand All @@ -172,7 +181,7 @@ impl fmt::Debug for DateTime {
}

/// DateTime Precision
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[repr(u8)]
pub enum DateTimePrecision {
/// Seconds precision
Expand Down Expand Up @@ -215,35 +224,6 @@ impl BinarySerializable for DateTimePrecision {
}
}

impl<'de> Deserialize<'de> for DateTimePrecision {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
let time_precision: String = Deserialize::deserialize(deserializer)?;
match time_precision.as_str() {
"secs" => Ok(DateTimePrecision::Seconds),
"millis" => Ok(DateTimePrecision::Milliseconds),
"micros" => Ok(DateTimePrecision::Microseconds),
"nanos" => Ok(DateTimePrecision::Nanoseconds),
unknown => Err(D::Error::custom(format!(
"Unknown precision value `{}` specified.",
unknown
))),
}
}
}

impl Serialize for DateTimePrecision {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
match self {
DateTimePrecision::Seconds => serializer.serialize_str("secs"),
DateTimePrecision::Milliseconds => serializer.serialize_str("millis"),
DateTimePrecision::Microseconds => serializer.serialize_str("micros"),
DateTimePrecision::Nanoseconds => serializer.serialize_str("nanos"),
}
}
}

/// Convert a timestamp with precision to OffsetDateTime.
fn precise_timestamp_to_datetime(
timestamp: i64,
Expand Down Expand Up @@ -294,7 +274,7 @@ mod tests {
"2020-01-02 00:30:00"
);
assert_eq!(
dt.format(DateTimeFormat::UnixTimestamp(DateTimePrecision::Seconds))
dt.format(DateTimeFormat::Timestamp(DateTimePrecision::Seconds))
.unwrap(),
"1577925000"
);
Expand Down
Loading

0 comments on commit 4b1ce74

Please sign in to comment.