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 11 import std.stdio; 12 13 import dyaml.node; 14 import dyaml.style; 15 16 17 /** Get the scalar style a node had in the file it was loaded from. 18 * 19 * This is only useful for nodes loaded from files. 20 * 21 * This is a "hack" because a YAML application is supposed to be unaware of styles 22 * used in YAML styles, i.e. treating different styles differently is unstandard. 23 * However, determining style may be useful in some cases, e.g. YAML utilities. 24 * 25 * May only be called on scalar nodes (nodes where node.isScalar() == true). 26 * 27 * Example: 28 * -------------------- 29 * // Node node // loaded from a file 30 * if(node.isScalar) 31 * { 32 * import std.stdio; 33 * writeln(node.scalarStyleHack()); 34 * } 35 * -------------------- 36 */ 37 ScalarStyle scalarStyleHack(ref const(Node) node) @safe nothrow 38 { 39 assert(node.isScalar, "Trying to get scalar style of a non-scalar node"); 40 return node.scalarStyle; 41 } 42 unittest 43 { 44 writeln("D:YAML scalarStyleHack getter unittest"); 45 auto node = Node(5); 46 assert(node.scalarStyleHack() == ScalarStyle.Invalid); 47 } 48 49 /** Get the collection style a YAML node had in the file it was loaded from. 50 * 51 * May only be called on collection nodes (nodes where node.isScalar() != true). 52 * 53 * See_Also: scalarStyleHack 54 */ 55 CollectionStyle collectionStyleHack(ref const(Node) node) @safe nothrow 56 { 57 assert(!node.isScalar, "Trying to get collection style of a scalar node"); 58 return node.collectionStyle; 59 } 60 unittest 61 { 62 writeln("D:YAML collectionStyleHack getter unittest"); 63 auto node = Node([1, 2, 3, 4, 5]); 64 assert(node.collectionStyleHack() == CollectionStyle.Invalid); 65 } 66 67 68 /** Set the scalar style node should have when written to a file. 69 * 70 * Setting the style might be useful when generating YAML or reformatting existing files. 71 * 72 * May only be called on scalar nodes (nodes where node.isScalar() == true). 73 */ 74 void scalarStyleHack(ref Node node, const ScalarStyle rhs) @safe nothrow 75 { 76 assert(node.isScalar, "Trying to set scalar style of a non-scalar node"); 77 node.scalarStyle = rhs; 78 } 79 /// 80 unittest 81 { 82 writeln("D:YAML scalarStyleHack setter unittest"); 83 auto node = Node(5); 84 node.scalarStyleHack = ScalarStyle.DoubleQuoted; 85 assert(node.scalarStyleHack() == ScalarStyle.DoubleQuoted); 86 } 87 88 /** Set the collection style node should have when written to a file. 89 * 90 * Setting the style might be useful when generating YAML or reformatting existing files. 91 * 92 * May only be called on collection nodes (nodes where node.isScalar() != true). 93 */ 94 void collectionStyleHack(ref Node node, const CollectionStyle rhs) @safe nothrow 95 { 96 assert(!node.isScalar, "Trying to set collection style of a scalar node"); 97 node.collectionStyle = rhs; 98 } 99 /// 100 unittest 101 { 102 writeln("D:YAML collectionStyleHack setter unittest"); 103 auto node = Node([1, 2, 3, 4, 5]); 104 node.collectionStyleHack = CollectionStyle.Block; 105 assert(node.collectionStyleHack() == CollectionStyle.Block); 106 }