codama_nodes/value_nodes/
string_value_node.rs1use codama_nodes_derive::node;
2
3#[node]
4pub struct StringValueNode {
5 pub string: String,
7}
8
9impl From<StringValueNode> for crate::Node {
10 fn from(val: StringValueNode) -> Self {
11 crate::Node::Value(val.into())
12 }
13}
14
15impl StringValueNode {
16 pub fn new<T>(string: T) -> Self
17 where
18 T: Into<String>,
19 {
20 Self {
21 string: string.into(),
22 }
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn new() {
32 assert_eq!(
33 StringValueNode::new("Hello World".to_string()).string,
34 "Hello World".to_string()
35 );
36 assert_eq!(
37 StringValueNode::new("Hello World").string,
38 "Hello World".to_string()
39 );
40 assert_eq!(StringValueNode::new('a').string, "a".to_string());
41 }
42
43 #[test]
44 fn to_json() {
45 let node = StringValueNode::new("Hello World!");
46 let json = serde_json::to_string(&node).unwrap();
47 assert_eq!(
48 json,
49 r#"{"kind":"stringValueNode","string":"Hello World!"}"#
50 );
51 }
52
53 #[test]
54 fn from_json() {
55 let json = r#"{"kind":"stringValueNode","string":"Hello World!"}"#;
56 let node: StringValueNode = serde_json::from_str(json).unwrap();
57 assert_eq!(node, StringValueNode::new("Hello World!"));
58 }
59}