Skip to main content

appletheia_application/command/
idempotency_output.rs

1use std::{fmt, fmt::Display, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4
5use super::IdempotencyOutputError;
6
7#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
8#[serde(transparent)]
9pub struct IdempotencyOutput(serde_json::Value);
10
11impl IdempotencyOutput {
12    pub fn new(value: serde_json::Value) -> Self {
13        Self(value)
14    }
15
16    pub fn value(&self) -> &serde_json::Value {
17        &self.0
18    }
19
20    pub fn into_value(self) -> serde_json::Value {
21        self.0
22    }
23}
24
25impl From<serde_json::Value> for IdempotencyOutput {
26    fn from(value: serde_json::Value) -> Self {
27        Self::new(value)
28    }
29}
30
31impl From<IdempotencyOutput> for serde_json::Value {
32    fn from(value: IdempotencyOutput) -> Self {
33        value.into_value()
34    }
35}
36
37impl Display for IdempotencyOutput {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        write!(f, "{}", self.0)
40    }
41}
42
43impl FromStr for IdempotencyOutput {
44    type Err = IdempotencyOutputError;
45
46    fn from_str(s: &str) -> Result<Self, Self::Err> {
47        Ok(Self::new(serde_json::from_str(s)?))
48    }
49}
50
51impl TryFrom<&str> for IdempotencyOutput {
52    type Error = IdempotencyOutputError;
53
54    fn try_from(value: &str) -> Result<Self, Self::Error> {
55        Self::from_str(value)
56    }
57}
58
59impl TryFrom<String> for IdempotencyOutput {
60    type Error = IdempotencyOutputError;
61
62    fn try_from(value: String) -> Result<Self, Self::Error> {
63        let json = serde_json::from_str::<serde_json::Value>(&value)?;
64        Ok(Self::new(json))
65    }
66}