codama_nodes/
instruction_status_node.rs1use codama_nodes_derive::node;
2use serde::{Deserialize, Serialize};
3
4#[node]
5#[derive(Default)]
6pub struct InstructionStatusNode {
7 pub lifecycle: InstructionLifecycle,
9 #[serde(default, skip_serializing_if = "crate::is_default")]
10 pub message: String,
11}
12
13impl InstructionStatusNode {
14 pub fn new(lifecycle: InstructionLifecycle) -> Self {
15 Self {
16 lifecycle,
17 message: String::default(),
18 }
19 }
20
21 pub fn with_message<S: Into<String>>(lifecycle: InstructionLifecycle, message: S) -> Self {
22 Self {
23 lifecycle,
24 message: message.into(),
25 }
26 }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31pub enum InstructionLifecycle {
32 #[default]
33 Live,
34 Deprecated,
35 Archived,
36 Draft,
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn new() {
45 let node = InstructionStatusNode::new(InstructionLifecycle::Live);
46 assert_eq!(node.lifecycle, InstructionLifecycle::Live);
47 assert_eq!(node.message, String::default());
48 }
49
50 #[test]
51 fn with_message() {
52 let node = InstructionStatusNode::with_message(
53 InstructionLifecycle::Deprecated,
54 "Use newInstruction",
55 );
56 assert_eq!(node.lifecycle, InstructionLifecycle::Deprecated);
57 assert_eq!(node.message, "Use newInstruction");
58 }
59
60 #[test]
61 fn to_json() {
62 let node = InstructionStatusNode::new(InstructionLifecycle::Live);
63 let json = serde_json::to_string(&node).unwrap();
64 assert_eq!(
65 json,
66 r#"{"kind":"instructionStatusNode","lifecycle":"live"}"#
67 );
68 }
69
70 #[test]
71 fn from_json() {
72 let json = r#"{"kind":"instructionStatusNode","lifecycle":"live"}"#;
73 let node: InstructionStatusNode = serde_json::from_str(json).unwrap();
74 assert_eq!(node.lifecycle, InstructionLifecycle::Live);
75 }
76
77 #[test]
78 fn to_json_with_message() {
79 let node = InstructionStatusNode::with_message(
80 InstructionLifecycle::Deprecated,
81 "Use newInstruction instead",
82 );
83 let json = serde_json::to_string(&node).unwrap();
84 assert_eq!(
85 json,
86 r#"{"kind":"instructionStatusNode","lifecycle":"deprecated","message":"Use newInstruction instead"}"#
87 );
88 }
89
90 #[test]
91 fn from_json_with_message() {
92 let json = r#"{"kind":"instructionStatusNode","lifecycle":"deprecated","message":"Use newInstruction instead"}"#;
93 let node: InstructionStatusNode = serde_json::from_str(json).unwrap();
94 assert_eq!(node.lifecycle, InstructionLifecycle::Deprecated);
95 assert_eq!(node.message, "Use newInstruction instead");
96 }
97}