Skip to main content

codama_nodes/
instruction_status_node.rs

1use crate::{InstructionLifecycle, InstructionStatusNode};
2
3#[allow(clippy::derivable_impls)]
4impl Default for InstructionStatusNode {
5    fn default() -> Self {
6        Self {
7            lifecycle: InstructionLifecycle::default(),
8            message: None,
9        }
10    }
11}
12
13impl InstructionStatusNode {
14    pub fn new(lifecycle: InstructionLifecycle) -> Self {
15        Self {
16            lifecycle,
17            message: None,
18        }
19    }
20
21    pub fn with_message<S: Into<String>>(lifecycle: InstructionLifecycle, message: S) -> Self {
22        Self {
23            lifecycle,
24            message: Some(message.into()),
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn new() {
35        let node = InstructionStatusNode::new(InstructionLifecycle::Live);
36        assert_eq!(node.lifecycle, InstructionLifecycle::Live);
37        assert_eq!(node.message, None);
38    }
39
40    #[test]
41    fn with_message() {
42        let node = InstructionStatusNode::with_message(
43            InstructionLifecycle::Deprecated,
44            "Use newInstruction",
45        );
46        assert_eq!(node.lifecycle, InstructionLifecycle::Deprecated);
47        assert_eq!(node.message.as_deref(), Some("Use newInstruction"));
48    }
49
50    #[test]
51    fn to_json() {
52        let node = InstructionStatusNode::new(InstructionLifecycle::Live);
53        let json = serde_json::to_string(&node).unwrap();
54        assert_eq!(
55            json,
56            r#"{"kind":"instructionStatusNode","lifecycle":"live"}"#
57        );
58    }
59
60    #[test]
61    fn from_json() {
62        let json = r#"{"kind":"instructionStatusNode","lifecycle":"live"}"#;
63        let node: InstructionStatusNode = serde_json::from_str(json).unwrap();
64        assert_eq!(node.lifecycle, InstructionLifecycle::Live);
65    }
66
67    #[test]
68    fn to_json_with_message() {
69        let node = InstructionStatusNode::with_message(
70            InstructionLifecycle::Deprecated,
71            "Use newInstruction instead",
72        );
73        let json = serde_json::to_string(&node).unwrap();
74        assert_eq!(
75            json,
76            r#"{"kind":"instructionStatusNode","lifecycle":"deprecated","message":"Use newInstruction instead"}"#
77        );
78    }
79
80    #[test]
81    fn from_json_with_message() {
82        let json = r#"{"kind":"instructionStatusNode","lifecycle":"deprecated","message":"Use newInstruction instead"}"#;
83        let node: InstructionStatusNode = serde_json::from_str(json).unwrap();
84        assert_eq!(node.lifecycle, InstructionLifecycle::Deprecated);
85        assert_eq!(node.message.as_deref(), Some("Use newInstruction instead"));
86    }
87}