codama_nodes/pda_seed_nodes/
variable_pda_seed_node.rs1use crate::{CamelCaseString, Docs, HasName, TypeNode};
2use codama_nodes_derive::node;
3
4#[node]
5pub struct VariablePdaSeedNode {
6 pub name: CamelCaseString,
8 #[serde(default, skip_serializing_if = "crate::is_default")]
9 pub docs: Docs,
10
11 pub r#type: TypeNode,
13}
14
15impl From<VariablePdaSeedNode> for crate::Node {
16 fn from(val: VariablePdaSeedNode) -> Self {
17 crate::Node::PdaSeed(val.into())
18 }
19}
20
21impl VariablePdaSeedNode {
22 pub fn new<T, U>(name: T, r#type: U) -> Self
23 where
24 T: Into<CamelCaseString>,
25 U: Into<TypeNode>,
26 {
27 Self {
28 name: name.into(),
29 docs: Docs::default(),
30 r#type: r#type.into(),
31 }
32 }
33}
34
35impl HasName for VariablePdaSeedNode {
36 fn name(&self) -> &CamelCaseString {
37 &self.name
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use crate::{NumberTypeNode, U32};
45
46 #[test]
47 fn new() {
48 let node = VariablePdaSeedNode::new("my_seed", NumberTypeNode::le(U32));
49 assert_eq!(node.name, CamelCaseString::new("mySeed"));
50 assert_eq!(node.r#type, TypeNode::Number(NumberTypeNode::le(U32)));
51 }
52
53 #[test]
54 fn direct_instantiation() {
55 let node = VariablePdaSeedNode {
56 name: "mySeed".into(),
57 docs: vec!["Hello".to_string()].into(),
58 r#type: NumberTypeNode::le(U32).into(),
59 };
60 assert_eq!(node.name, CamelCaseString::new("mySeed"));
61 assert_eq!(*node.docs, vec!["Hello".to_string()]);
62 assert_eq!(node.r#type, TypeNode::Number(NumberTypeNode::le(U32)));
63 }
64
65 #[test]
66 fn to_json() {
67 let node = VariablePdaSeedNode::new("mySeed", NumberTypeNode::le(U32));
68 let json = serde_json::to_string(&node).unwrap();
69 assert_eq!(
70 json,
71 r#"{"kind":"variablePdaSeedNode","name":"mySeed","type":{"kind":"numberTypeNode","format":"u32","endian":"le"}}"#
72 );
73 }
74
75 #[test]
76 fn from_json() {
77 let json = r#"{"kind":"variablePdaSeedNode","name":"mySeed","type":{"kind":"numberTypeNode","format":"u32","endian":"le"}}"#;
78 let node: VariablePdaSeedNode = serde_json::from_str(json).unwrap();
79 assert_eq!(
80 node,
81 VariablePdaSeedNode::new("mySeed", NumberTypeNode::le(U32))
82 );
83 }
84}