Skip to main content

codama_nodes/value_nodes/
bytes_value_node.rs

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