codama_nodes/value_nodes/
bytes_value_node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::BytesEncoding;
use codama_nodes_derive::node;

#[node]
pub struct BytesValueNode {
    // Data.
    pub data: String,
    pub encoding: BytesEncoding,
}

impl From<BytesValueNode> for crate::Node {
    fn from(val: BytesValueNode) -> Self {
        crate::Node::Value(val.into())
    }
}

impl BytesValueNode {
    pub fn new<T>(encoding: BytesEncoding, data: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            encoding,
            data: data.into(),
        }
    }

    pub fn base16<T>(data: T) -> Self
    where
        T: Into<String>,
    {
        Self::new(BytesEncoding::Base16, data)
    }

    pub fn base58<T>(data: T) -> Self
    where
        T: Into<String>,
    {
        Self::new(BytesEncoding::Base58, data)
    }

    pub fn base64<T>(data: T) -> Self
    where
        T: Into<String>,
    {
        Self::new(BytesEncoding::Base64, data)
    }

    pub fn utf8<T>(data: T) -> Self
    where
        T: Into<String>,
    {
        Self::new(BytesEncoding::Utf8, data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Utf8;

    #[test]
    fn new() {
        let node = BytesValueNode::new(Utf8, "Hello World");
        assert_eq!(node.encoding, BytesEncoding::Utf8);
        assert_eq!(node.data, "Hello World");
    }

    #[test]
    fn base16() {
        let node = BytesValueNode::base16("deadb0d1e5");
        assert_eq!(node.encoding, BytesEncoding::Base16);
        assert_eq!(node.data, "deadb0d1e5");
    }

    #[test]
    fn base58() {
        let node = BytesValueNode::base58("AoxAdTcWDxzTkzJf");
        assert_eq!(node.encoding, BytesEncoding::Base58);
        assert_eq!(node.data, "AoxAdTcWDxzTkzJf");
    }

    #[test]
    fn base64() {
        let node = BytesValueNode::base64("HelloWorld++");
        assert_eq!(node.encoding, BytesEncoding::Base64);
        assert_eq!(node.data, "HelloWorld++");
    }

    #[test]
    fn utf8() {
        let node = BytesValueNode::utf8("Hello World");
        assert_eq!(node.encoding, BytesEncoding::Utf8);
        assert_eq!(node.data, "Hello World");
    }

    #[test]
    fn to_json() {
        let node = BytesValueNode::base16("deadb0d1e5");
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}"#;
        let node: BytesValueNode = serde_json::from_str(json).unwrap();
        assert_eq!(node, BytesValueNode::base16("deadb0d1e5"));
    }
}