agent_effects_store/
kind.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum EffectKind {
12 Read,
14 IdempotentWrite,
17 ReversibleWrite,
20 IrreversibleWrite,
23}
24
25impl EffectKind {
26 pub const fn is_naturally_idempotent(self) -> bool {
29 matches!(self, Self::Read | Self::IdempotentWrite)
30 }
31
32 pub const fn as_str(self) -> &'static str {
34 match self {
35 Self::Read => "read",
36 Self::IdempotentWrite => "idempotent_write",
37 Self::ReversibleWrite => "reversible_write",
38 Self::IrreversibleWrite => "irreversible_write",
39 }
40 }
41
42 pub fn parse(s: &str) -> Option<Self> {
44 Some(match s {
45 "read" => Self::Read,
46 "idempotent_write" => Self::IdempotentWrite,
47 "reversible_write" => Self::ReversibleWrite,
48 "irreversible_write" => Self::IrreversibleWrite,
49 _ => return None,
50 })
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn storage_representation_round_trips_and_matches_serde() {
60 for kind in [
61 EffectKind::Read,
62 EffectKind::IdempotentWrite,
63 EffectKind::ReversibleWrite,
64 EffectKind::IrreversibleWrite,
65 ] {
66 assert_eq!(EffectKind::parse(kind.as_str()), Some(kind));
67 assert_eq!(
68 serde_json::to_string(&kind).unwrap(),
69 format!("\"{}\"", kind.as_str())
70 );
71 }
72 }
73}