Skip to content

Commit

Permalink
Fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi3 committed Sep 22, 2023
1 parent 93aabdc commit c8dfc48
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,10 @@ pub trait QueryBuilder:
)
.unwrap(),
#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
Value::TimeDuration(Some(v)) => self.write_string_quoted(&time_duration_format::ISO8601Duration::from(v).to_string(), &mut s),
Value::TimeDuration(Some(v)) => self.write_string_quoted(
&time_duration_format::ISO8601Duration::from(v).to_string(),
&mut s,
),
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(Some(v)) => write!(s, "{v}").unwrap(),
#[cfg(feature = "with-bigdecimal")]
Expand Down
71 changes: 47 additions & 24 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ pub enum ArrayType {
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTimeWithTimeZone,

#[cfg(all(feature = "postgres-interval", feature = "with-chrono"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-chrono"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-chrono")))
)]
ChronoDuration,

#[cfg(feature = "with-time")]
Expand All @@ -101,9 +104,12 @@ pub enum ArrayType {
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeWithTimeZone,

#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-time"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-time")))
)]
TimeDuration,

#[cfg(feature = "with-uuid")]
Expand Down Expand Up @@ -212,9 +218,12 @@ pub enum Value {
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
ChronoDateTimeWithTimeZone(Option<Box<DateTime<FixedOffset>>>),

#[cfg(all(feature = "postgres-interval", feature = "with-chrono"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-chrono"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-chrono")))
)]
ChronoDuration(Option<Box<Duration>>),

#[cfg(feature = "with-time")]
Expand All @@ -232,9 +241,12 @@ pub enum Value {
#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
TimeDateTimeWithTimeZone(Option<Box<OffsetDateTime>>),

#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-time"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-time")))
)]
TimeDuration(Option<Box<time::Duration>>),

#[cfg(feature = "with-uuid")]
Expand Down Expand Up @@ -641,11 +653,14 @@ mod with_chrono {
}

#[cfg(all(feature = "postgres-interval", feature = "with-chrono"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-chrono"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-chrono")))
)]
mod with_chrono_duration {
use super::*;
use chrono::Duration;

type_to_box_value!(Duration, ChronoDuration, Interval(None, None));
}

Expand Down Expand Up @@ -709,35 +724,43 @@ mod with_time {
}

#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-time"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-time")))
)]
mod with_time_duration {
use super::*;
use super::*;

type_to_box_value!(time::Duration, TimeDuration, Interval(None, None));
}

#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "postgres-interval", feature = "with-time"))))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "postgres-interval", feature = "with-time")))
)]
pub mod time_duration_format {
use core::fmt;

// from chrono/0.4.31/src/chrono/duration.rs.html#22-39
/// The number of nanoseconds in a microsecond.
const NANOS_PER_MICRO: i32 = 1000;
/// The number of nanoseconds in a millisecond.
const NANOS_PER_MILLI: i32 = 1_000_000;
/// The number of (non-leap) seconds in days.
const SECS_PER_DAY: i64 = 86_400;

#[derive(Debug)]
pub struct ISO8601Duration { inner: time::Duration }

pub struct ISO8601Duration {
inner: time::Duration,
}

impl ISO8601Duration {
pub fn from(d: &time::Duration) -> Self {
Self { inner: *d }
}
}

impl fmt::Display for ISO8601Duration {
// adapted from chrono/0.4.31/src/chrono/duration.rs.html#407-434
/// Format a duration using the [ISO 8601] format
Expand Down Expand Up @@ -898,7 +921,7 @@ pub mod with_array {

#[cfg(feature = "with-chrono")]
impl<Tz> NotU8 for DateTime<Tz> where Tz: chrono::TimeZone {}

#[cfg(all(feature = "postgres-interval", feature = "with-chrono"))]
impl NotU8 for Duration {}

Expand All @@ -913,7 +936,7 @@ pub mod with_array {

#[cfg(feature = "with-time")]
impl NotU8 for OffsetDateTime {}

#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
impl NotU8 for time::Duration {}

Expand Down Expand Up @@ -1198,9 +1221,9 @@ impl Value {
#[cfg(all(feature = "postgres-interval", feature = "with-chrono"))]
impl Value {
pub fn is_chrono_duration(&self) -> bool {
return matches!(self, Self::ChronoDuration(_));
matches!(self, Self::ChronoDuration(_));
}

pub fn as_ref_chrono_duration(&self) -> Option<&Duration> {
match self {
Self::ChronoDuration(v) => box_to_opt_ref!(v),
Expand All @@ -1212,9 +1235,9 @@ impl Value {
#[cfg(all(feature = "postgres-interval", feature = "with-time"))]
impl Value {
pub fn is_time_duration(&self) -> bool {
return matches!(self, Self::TimeDuration(_));
matches!(self, Self::TimeDuration(_));
}

pub fn as_ref_time_duration(&self) -> Option<&time::Duration> {
match self {
Self::TimeDuration(v) => box_to_opt_ref!(v),
Expand Down

0 comments on commit c8dfc48

Please sign in to comment.