codama_nodes/
instruction_argument_node.rs1use crate::{
2 CamelCaseString, DefaultValueStrategy, Docs, HasName, InstructionInputValueNode,
3 StructFieldTypeNode, StructTypeNode, TypeNode,
4};
5use codama_nodes_derive::node;
6
7#[node]
8pub struct InstructionArgumentNode {
9 pub name: CamelCaseString,
11 #[serde(skip_serializing_if = "crate::is_default")]
12 pub default_value_strategy: Option<DefaultValueStrategy>,
13 #[serde(default, skip_serializing_if = "crate::is_default")]
14 pub docs: Docs,
15
16 pub r#type: TypeNode,
18 #[serde(skip_serializing_if = "crate::is_default")]
19 pub default_value: Option<InstructionInputValueNode>,
20}
21
22impl InstructionArgumentNode {
23 pub fn new<T, U>(name: T, r#type: U) -> Self
24 where
25 T: Into<CamelCaseString>,
26 U: Into<TypeNode>,
27 {
28 Self {
29 name: name.into(),
30 default_value_strategy: None,
31 docs: Docs::default(),
32 r#type: r#type.into(),
33 default_value: None,
34 }
35 }
36}
37
38impl HasName for InstructionArgumentNode {
39 fn name(&self) -> &CamelCaseString {
40 &self.name
41 }
42}
43
44impl From<StructFieldTypeNode> for InstructionArgumentNode {
45 fn from(value: StructFieldTypeNode) -> Self {
46 Self {
47 name: value.name,
48 default_value_strategy: value.default_value_strategy,
49 docs: value.docs,
50 r#type: value.r#type,
51 default_value: value.default_value.map(InstructionInputValueNode::from),
52 }
53 }
54}
55
56impl From<StructTypeNode> for Vec<InstructionArgumentNode> {
57 fn from(val: StructTypeNode) -> Self {
58 val.fields
59 .into_iter()
60 .map(InstructionArgumentNode::from)
61 .collect()
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use crate::{ArgumentValueNode, NumberTypeNode, U32};
69
70 #[test]
71 fn new() {
72 let node = InstructionArgumentNode::new("my_argument", NumberTypeNode::le(U32));
73 assert_eq!(node.name, CamelCaseString::new("myArgument"));
74 assert_eq!(node.r#type, TypeNode::Number(NumberTypeNode::le(U32)));
75 }
76
77 #[test]
78 fn direct_instantiation() {
79 let node = InstructionArgumentNode {
80 name: "myArgument".into(),
81 default_value_strategy: Some(DefaultValueStrategy::Optional),
82 docs: vec!["Hello".to_string()].into(),
83 r#type: NumberTypeNode::le(U32).into(),
84 default_value: Some(ArgumentValueNode::new("myOtherArgument").into()),
85 };
86
87 assert_eq!(node.name, CamelCaseString::new("myArgument"));
88 assert_eq!(
89 node.default_value_strategy,
90 Some(DefaultValueStrategy::Optional)
91 );
92 assert_eq!(*node.docs, vec!["Hello".to_string()]);
93 assert_eq!(node.r#type, TypeNode::Number(NumberTypeNode::le(U32)));
94 assert_eq!(
95 node.default_value,
96 Some(InstructionInputValueNode::Argument(ArgumentValueNode::new(
97 "myOtherArgument"
98 )))
99 );
100 }
101
102 #[test]
103 fn to_json() {
104 let node = InstructionArgumentNode::new("myArgument", NumberTypeNode::le(U32));
105 let json = serde_json::to_string(&node).unwrap();
106 assert_eq!(
107 json,
108 r#"{"kind":"instructionArgumentNode","name":"myArgument","type":{"kind":"numberTypeNode","format":"u32","endian":"le"}}"#
109 );
110 }
111
112 #[test]
113 fn from_json() {
114 let json = r#"{"kind":"instructionArgumentNode","name":"myArgument","type":{"kind":"numberTypeNode","format":"u32","endian":"le"}}"#;
115 let node: InstructionArgumentNode = serde_json::from_str(json).unwrap();
116 assert_eq!(
117 node,
118 InstructionArgumentNode::new("myArgument", NumberTypeNode::le(U32))
119 );
120 }
121}