google_cloud_spanner/
value.rs

1use std::ops::Deref;
2use std::time::Duration;
3
4use google_cloud_googleapis::spanner::v1::transaction_options::read_only::TimestampBound as InternalTimestampBound;
5use google_cloud_googleapis::spanner::v1::transaction_options::ReadOnly;
6
7#[derive(Clone, PartialEq, Eq)]
8pub struct Timestamp {
9    /// Represents seconds of UTC time since Unix epoch
10    /// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
11    /// 9999-12-31T23:59:59Z inclusive.
12    pub seconds: i64,
13    /// Non-negative fractions of a second at nanosecond resolution. Negative
14    /// second values with fractions must still have non-negative nanos values
15    /// that count forward in time. Must be from 0 to 999,999,999
16    /// inclusive.
17    pub nanos: i32,
18}
19
20impl From<Timestamp> for prost_types::Timestamp {
21    fn from(t: Timestamp) -> Self {
22        prost_types::Timestamp {
23            seconds: t.seconds,
24            nanos: t.nanos,
25        }
26    }
27}
28
29impl From<prost_types::Timestamp> for Timestamp {
30    fn from(t: prost_types::Timestamp) -> Self {
31        Timestamp {
32            seconds: t.seconds,
33            nanos: t.nanos,
34        }
35    }
36}
37
38#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
39pub struct CommitTimestamp {
40    pub(crate) timestamp: time::OffsetDateTime,
41}
42
43impl CommitTimestamp {
44    pub fn new() -> Self {
45        Self::default()
46    }
47}
48
49impl Default for CommitTimestamp {
50    fn default() -> Self {
51        CommitTimestamp {
52            timestamp: time::OffsetDateTime::UNIX_EPOCH,
53        }
54    }
55}
56
57impl Deref for CommitTimestamp {
58    type Target = time::OffsetDateTime;
59
60    fn deref(&self) -> &Self::Target {
61        &self.timestamp
62    }
63}
64
65impl From<CommitTimestamp> for time::OffsetDateTime {
66    fn from(s: CommitTimestamp) -> Self {
67        s.timestamp
68    }
69}
70
71#[derive(Clone)]
72pub struct TimestampBound {
73    inner: InternalTimestampBound,
74}
75
76impl TimestampBound {
77    pub fn strong_read() -> Self {
78        TimestampBound {
79            inner: InternalTimestampBound::Strong(true),
80        }
81    }
82    pub fn exact_staleness(d: Duration) -> Self {
83        TimestampBound {
84            inner: InternalTimestampBound::ExactStaleness(d.try_into().unwrap()),
85        }
86    }
87    pub fn max_staleness(d: Duration) -> Self {
88        TimestampBound {
89            inner: InternalTimestampBound::MaxStaleness(d.try_into().unwrap()),
90        }
91    }
92    pub fn min_read_timestamp(t: Timestamp) -> Self {
93        TimestampBound {
94            inner: InternalTimestampBound::MinReadTimestamp(t.into()),
95        }
96    }
97    pub fn read_timestamp(t: Timestamp) -> Self {
98        TimestampBound {
99            inner: InternalTimestampBound::ReadTimestamp(t.into()),
100        }
101    }
102}
103
104impl From<TimestampBound> for ReadOnly {
105    fn from(tb: TimestampBound) -> Self {
106        ReadOnly {
107            return_read_timestamp: true,
108            timestamp_bound: Some(tb.inner),
109        }
110    }
111}