1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::errors::FirestoreError;
use crate::timestamp_utils::to_timestamp;
use chrono::prelude::*;
use gcloud_sdk::google::firestore::v1::Precondition;
/// A precondition on a document, used for conditional write operations in Firestore.
///
/// Preconditions allow you to specify conditions that must be met for a write
/// operation (create, update, delete) to succeed. If the precondition is not met,
/// the operation will fail, typically with a `DataConflictError` or similar.
/// ```
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum FirestoreWritePrecondition {
/// The target document must exist (if `true`) or must not exist (if `false`).
///
/// - `Exists(true)`: The operation will only succeed if the document already exists.
/// Useful for conditional updates or deletes.
/// - `Exists(false)`: The operation will only succeed if the document does not already exist.
/// Useful for conditional creates (to prevent overwriting).
Exists(bool),
/// The target document must exist and its `update_time` must match the provided timestamp.
///
/// This is used for optimistic concurrency control. The operation will only succeed
/// if the document has not been modified since the specified `update_time`.
/// The `DateTime<Utc>` must be microsecond-aligned, as Firestore timestamps have
/// microsecond precision.
UpdateTime(DateTime<Utc>),
}
impl TryInto<gcloud_sdk::google::firestore::v1::Precondition> for FirestoreWritePrecondition {
type Error = FirestoreError;
fn try_into(self) -> Result<Precondition, Self::Error> {
match self {
FirestoreWritePrecondition::Exists(value) => {
Ok(gcloud_sdk::google::firestore::v1::Precondition {
condition_type: Some(
gcloud_sdk::google::firestore::v1::precondition::ConditionType::Exists(
value,
),
),
})
}
FirestoreWritePrecondition::UpdateTime(value) => {
Ok(gcloud_sdk::google::firestore::v1::Precondition {
condition_type: Some(
gcloud_sdk::google::firestore::v1::precondition::ConditionType::UpdateTime(
to_timestamp(value),
),
),
})
}
}
}
}