1 //          Copyright Ferdinand Majerech 2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 
7 /// Functionality that may sometimes be needed but allows unsafe or unstandard behavior, and should only be used in specific cases.
8 module dyaml.hacks;
9 
10 import dyaml.node;
11 import dyaml.style;
12 
13 
14 /** Get the scalar style a node had in the file it was loaded from.
15  *
16  * This is only useful for nodes loaded from files.
17  *
18  * This is a "hack" because a YAML application is supposed to be unaware of styles
19  * used in YAML styles,  i.e. treating different styles differently is unstandard.
20  * However, determining style may be useful in some cases, e.g. YAML utilities.
21  *
22  * May only be called on scalar nodes (nodes where node.isScalar() == true).
23  */
24 ScalarStyle scalarStyleHack(ref const(Node) node) @safe nothrow
25 {
26     assert(node.isScalar, "Trying to get scalar style of a non-scalar node");
27     return node.scalarStyle;
28 }
29 ///
30 @safe unittest
31 {
32     import dyaml;
33     Node node = Loader.fromString(`"42"`).load(); // loaded from a file
34     if(node.isScalar)
35     {
36         assert(node.scalarStyleHack() == ScalarStyle.DoubleQuoted);
37     }
38 }
39 @safe unittest
40 {
41     auto node = Node(5);
42     assert(node.scalarStyleHack() == ScalarStyle.Invalid);
43 }
44 
45 /** Get the collection style a YAML node had in the file it was loaded from.
46  *
47  * May only be called on collection nodes (nodes where node.isScalar() != true).
48  *
49  * See_Also: scalarStyleHack
50  */
51 CollectionStyle collectionStyleHack(ref const(Node) node) @safe nothrow
52 {
53     assert(!node.isScalar, "Trying to get collection style of a scalar node");
54     return node.collectionStyle;
55 }
56 @safe unittest
57 {
58     auto node = Node([1, 2, 3, 4, 5]);
59     assert(node.collectionStyleHack() == CollectionStyle.Invalid);
60 }
61 
62 
63 /** Set the scalar style node should have when written to a file.
64  *
65  * Setting the style might be useful when generating YAML or reformatting existing files.
66  *
67  * May only be called on scalar nodes (nodes where node.isScalar() == true).
68  */
69 void scalarStyleHack(ref Node node, const ScalarStyle rhs) @safe nothrow
70 {
71     assert(node.isScalar, "Trying to set scalar style of a non-scalar node");
72     node.scalarStyle = rhs;
73 }
74 ///
75 @safe unittest
76 {
77     auto node = Node(5);
78     node.scalarStyleHack = ScalarStyle.DoubleQuoted;
79     assert(node.scalarStyleHack() == ScalarStyle.DoubleQuoted);
80 }
81 
82 /** Set the collection style node should have when written to a file.
83  *
84  * Setting the style might be useful when generating YAML or reformatting existing files.
85  *
86  * May only be called on collection nodes (nodes where node.isScalar() != true).
87  */
88 void collectionStyleHack(ref Node node, const CollectionStyle rhs) @safe nothrow
89 {
90     assert(!node.isScalar, "Trying to set collection style of a scalar node");
91     node.collectionStyle = rhs;
92 }
93 ///
94 @safe unittest
95 {
96     auto node = Node([1, 2, 3, 4, 5]);
97     node.collectionStyleHack = CollectionStyle.Block;
98     assert(node.collectionStyleHack() == CollectionStyle.Block);
99 }