hyperstack_sdk/
frame.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "lowercase")]
5pub enum Mode {
6    State,
7    Kv,
8    Append,
9    List,
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Operation {
14    Upsert,
15    Patch,
16    Delete,
17    Create,
18}
19
20impl std::str::FromStr for Operation {
21    type Err = std::convert::Infallible;
22
23    fn from_str(s: &str) -> Result<Self, Self::Err> {
24        Ok(match s {
25            "upsert" => Operation::Upsert,
26            "patch" => Operation::Patch,
27            "delete" => Operation::Delete,
28            "create" => Operation::Create,
29            _ => Operation::Upsert,
30        })
31    }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Frame {
36    pub mode: Mode,
37    #[serde(rename = "entity")]
38    pub entity: String,
39    pub op: String,
40    pub key: String,
41    pub data: serde_json::Value,
42}
43
44impl Frame {
45    pub fn entity_name(&self) -> &str {
46        &self.entity
47    }
48
49    pub fn operation(&self) -> Operation {
50        self.op.parse().unwrap()
51    }
52}
53
54pub fn parse_frame(bytes: &[u8]) -> Result<Frame, serde_json::Error> {
55    let text = String::from_utf8_lossy(bytes);
56    serde_json::from_str(&text)
57}