codama_nodes/value_nodes/
bytes_value_node.rs1use crate::BytesEncoding;
2use codama_nodes_derive::node;
3
4#[node]
5pub struct BytesValueNode {
6 pub data: String,
8 pub encoding: BytesEncoding,
9}
10
11impl From<BytesValueNode> for crate::Node {
12 fn from(val: BytesValueNode) -> Self {
13 crate::Node::Value(val.into())
14 }
15}
16
17impl BytesValueNode {
18 pub fn new<T>(encoding: BytesEncoding, data: T) -> Self
19 where
20 T: Into<String>,
21 {
22 Self {
23 encoding,
24 data: data.into(),
25 }
26 }
27
28 pub fn base16<T>(data: T) -> Self
29 where
30 T: Into<String>,
31 {
32 Self::new(BytesEncoding::Base16, data)
33 }
34
35 pub fn base58<T>(data: T) -> Self
36 where
37 T: Into<String>,
38 {
39 Self::new(BytesEncoding::Base58, data)
40 }
41
42 pub fn base64<T>(data: T) -> Self
43 where
44 T: Into<String>,
45 {
46 Self::new(BytesEncoding::Base64, data)
47 }
48
49 pub fn utf8<T>(data: T) -> Self
50 where
51 T: Into<String>,
52 {
53 Self::new(BytesEncoding::Utf8, data)
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::Utf8;
61
62 #[test]
63 fn new() {
64 let node = BytesValueNode::new(Utf8, "Hello World");
65 assert_eq!(node.encoding, BytesEncoding::Utf8);
66 assert_eq!(node.data, "Hello World");
67 }
68
69 #[test]
70 fn base16() {
71 let node = BytesValueNode::base16("deadb0d1e5");
72 assert_eq!(node.encoding, BytesEncoding::Base16);
73 assert_eq!(node.data, "deadb0d1e5");
74 }
75
76 #[test]
77 fn base58() {
78 let node = BytesValueNode::base58("AoxAdTcWDxzTkzJf");
79 assert_eq!(node.encoding, BytesEncoding::Base58);
80 assert_eq!(node.data, "AoxAdTcWDxzTkzJf");
81 }
82
83 #[test]
84 fn base64() {
85 let node = BytesValueNode::base64("HelloWorld++");
86 assert_eq!(node.encoding, BytesEncoding::Base64);
87 assert_eq!(node.data, "HelloWorld++");
88 }
89
90 #[test]
91 fn utf8() {
92 let node = BytesValueNode::utf8("Hello World");
93 assert_eq!(node.encoding, BytesEncoding::Utf8);
94 assert_eq!(node.data, "Hello World");
95 }
96
97 #[test]
98 fn to_json() {
99 let node = BytesValueNode::base16("deadb0d1e5");
100 let json = serde_json::to_string(&node).unwrap();
101 assert_eq!(
102 json,
103 r#"{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}"#
104 );
105 }
106
107 #[test]
108 fn from_json() {
109 let json = r#"{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}"#;
110 let node: BytesValueNode = serde_json::from_str(json).unwrap();
111 assert_eq!(node, BytesValueNode::base16("deadb0d1e5"));
112 }
113}