cloud_sdk/operation/
metadata.rs1#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub enum OperationImpact {
6 ReadOnly,
8 Mutation,
10 Destructive,
12}
13
14#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
16pub enum RequestSemantics {
17 Safe,
19 Idempotent,
21 NonIdempotent,
23}
24
25#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
27pub enum RetryEligibility {
28 Never,
30 ExplicitPolicy,
32}
33
34#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
36pub enum CostIntent {
37 NoKnownCost,
39 MayIncurCost,
41}
42
43#[derive(Clone, Copy, Debug, Eq, PartialEq)]
45pub enum OperationMetadataError {
46 ReadOnlyMustBeSafe,
48 StateChangeCannotBeSafe,
50 NonIdempotentRetry,
52}
53
54impl_static_error!(OperationMetadataError,
55 Self::ReadOnlyMustBeSafe => "read-only operation must use safe semantics",
56 Self::StateChangeCannotBeSafe => "state-changing operation cannot use safe semantics",
57 Self::NonIdempotentRetry => "non-idempotent operation cannot be retry eligible",
58);
59
60#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
68pub struct OperationMetadata {
69 impact: OperationImpact,
70 semantics: RequestSemantics,
71 retry: RetryEligibility,
72 cost: CostIntent,
73}
74
75impl OperationMetadata {
76 pub const fn new(
78 impact: OperationImpact,
79 semantics: RequestSemantics,
80 retry: RetryEligibility,
81 cost: CostIntent,
82 ) -> Result<Self, OperationMetadataError> {
83 match (impact, semantics, retry) {
84 (OperationImpact::ReadOnly, semantics, _)
85 if !matches!(semantics, RequestSemantics::Safe) =>
86 {
87 return Err(OperationMetadataError::ReadOnlyMustBeSafe);
88 }
89 (
90 OperationImpact::Mutation | OperationImpact::Destructive,
91 RequestSemantics::Safe,
92 _,
93 ) => {
94 return Err(OperationMetadataError::StateChangeCannotBeSafe);
95 }
96 (_, RequestSemantics::NonIdempotent, RetryEligibility::ExplicitPolicy) => {
97 return Err(OperationMetadataError::NonIdempotentRetry);
98 }
99 _ => {}
100 }
101 Ok(Self {
102 impact,
103 semantics,
104 retry,
105 cost,
106 })
107 }
108
109 #[must_use]
111 pub const fn impact(self) -> OperationImpact {
112 self.impact
113 }
114
115 #[must_use]
117 pub const fn semantics(self) -> RequestSemantics {
118 self.semantics
119 }
120
121 #[must_use]
123 pub const fn retry_eligibility(self) -> RetryEligibility {
124 self.retry
125 }
126
127 #[must_use]
129 pub const fn cost_intent(self) -> CostIntent {
130 self.cost
131 }
132}