codama_nodes/contextual_value_nodes/
account_value_node.rs

1use crate::{CamelCaseString, HasName};
2use codama_nodes_derive::node;
3
4#[node]
5pub struct AccountValueNode {
6    // Data.
7    pub name: CamelCaseString,
8}
9
10impl From<AccountValueNode> for crate::Node {
11    fn from(val: AccountValueNode) -> Self {
12        crate::Node::ContextualValue(val.into())
13    }
14}
15
16impl AccountValueNode {
17    pub fn new<T>(name: T) -> Self
18    where
19        T: Into<CamelCaseString>,
20    {
21        Self { name: name.into() }
22    }
23}
24
25impl HasName for AccountValueNode {
26    fn name(&self) -> &CamelCaseString {
27        &self.name
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn new() {
37        let node = AccountValueNode::new("my_account");
38        assert_eq!(node.name, CamelCaseString::new("myAccount"));
39    }
40
41    #[test]
42    fn to_json() {
43        let node = AccountValueNode::new("myAccount");
44        let json = serde_json::to_string(&node).unwrap();
45        assert_eq!(json, r#"{"kind":"accountValueNode","name":"myAccount"}"#);
46    }
47
48    #[test]
49    fn from_json() {
50        let json = r#"{"kind":"accountValueNode","name":"myAccount"}"#;
51        let node: AccountValueNode = serde_json::from_str(json).unwrap();
52        assert_eq!(node, AccountValueNode::new("myAccount"));
53    }
54}