proto-types 0.1.0

Rust types generated from the google.protobuf and buf.validate protobuf packages, plus extra helpers for implementing validation with the protocheck crate.
Documentation
use std::time::SystemTime;

use crate::{Duration, Timestamp};

#[cfg(feature = "chrono")]
mod chrono {
  use chrono::Utc;

  use crate::{Timestamp, TimestampError};

  impl Timestamp {
    /// Converts this timestamp into a [`chrono::DateTime<Utc>`] struct and calls .format on it with the string argument being given.
    pub fn format(&self, string: &str) -> Result<String, TimestampError> {
      let chrono_timestamp: chrono::DateTime<Utc> = (*self).try_into()?;

      Ok(chrono_timestamp.format(string).to_string())
    }

    pub fn as_datetime_utc(&self) -> Result<chrono::DateTime<Utc>, TimestampError> {
      (*self).try_into()
    }
  }
}

impl Timestamp {
  pub fn now() -> Self {
    SystemTime::now().into()
  }

  pub fn new(seconds: i64, nanos: i32) -> Self {
    Timestamp { seconds, nanos }
  }

  /// Checks whether the Timestamp instance is within the indicated range (positive or negative) from now.
  pub fn is_within_range_from_now(&self, range: Duration) -> bool {
    (Timestamp::now() + range) >= *self && (Timestamp::now() - range) <= *self
  }

  /// Checks whether the Timestamp instance is within the indicated range in the future.
  pub fn is_within_future_range(&self, range: Duration) -> bool {
    (Timestamp::now() + range) >= *self
  }

  /// Checks whether the Timestamp instance is within the indicated range in the past.
  pub fn is_within_past_range(&self, range: Duration) -> bool {
    (Timestamp::now() - range) <= *self
  }

  pub fn is_future(&self) -> bool {
    *self > Self::now()
  }

  pub fn is_past(&self) -> bool {
    *self < Self::now()
  }
}