1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	aermicioi
29 **/
30 module aermicioi.aedi_property_reader.yaml.convertor;
31 
32 import aermicioi.aedi_property_reader.core.convertor;
33 import aermicioi.aedi_property_reader.core.accessor;
34 import aermicioi.aedi_property_reader.yaml.accessor;
35 import aermicioi.aedi_property_reader.yaml.yaml : accessor;
36 import aermicioi.aedi_property_reader.yaml.inspector;
37 import aermicioi.aedi.factory;
38 import aermicioi.aedi.storage.locator;
39 import aermicioi.aedi.storage.wrapper;
40 import aermicioi.aedi.storage.decorator;
41 import aermicioi.aedi.exception;
42 import std.traits;
43 import std.experimental.allocator;
44 import dyaml;
45 import std.conv;
46 import std.range;
47 import std.exception;
48 
49 public {
50     enum YamlAccessorFactory(From : Node) = () => new RuntimeFieldAccessor!Node(accessor());
51     enum YamlInspectorFactory(From : Node) = () => new YamlInspector;
52 }
53 
54 alias YamlConvertor = ChainedAdvisedConvertor!(
55     AdvisedConvertor!(convert, destruct),
56     AdvisedConvertor!(
57 		YamlAccessorFactory,
58 		CompositeSetterFactory,
59 		CompositeInspectorFactory,
60 		YamlInspectorFactory
61 	)
62 ).AdvisedConvertorImplementation;
63 
64 
65 void convert(To, From : Node)(in From node, ref To to, RCIAllocator allocator = theAllocator)
66 	if (!is(To == enum) && !isAggregateType!To && !isSomeChar!To) {
67 
68 	enforce!InvalidCastException(node.convertsTo!To, text("Could not convert yaml ", dumper(node), " to ", typeid(To)));
69 
70 	to = (cast() node).as!To;
71 }
72 
73 void convert(To : Z[], From : Node, Z)(in From node, ref To to, RCIAllocator allocator = theAllocator)
74 	if (isSomeChar!Z && !is(To == enum)) {
75 
76 	enforce!InvalidCastException(node.isString, text("Could not convert yaml ", dumper(node), " to ", typeid(To), " not a string"));
77 
78 	to = std.conv.to!To((cast() node).as!(string));
79 }
80 
81 void convert(To, From : Node)(in From node, ref To to, RCIAllocator allocator = theAllocator)
82 	if (isSomeChar!To && !is(To == enum)) {
83 
84 	enforce!InvalidCastException(node.isString, text("Could not convert yaml ", dumper(node), " to ", typeid(To), " not a string"));
85 
86 	to = (cast() node).as!string.to!char;
87 }
88 
89 void convert(To : Z[], From : Node, Z)(in From node, ref To to, RCIAllocator allocator = theAllocator)
90 	if (!isSomeString!To && !is(To == enum)) {
91 
92 	enforce!InvalidCastException(node.isSequence, text("Could not convert yaml ", dumper(node), " to ", typeid(To), " not a sequence"));
93 
94 	auto sequence = (cast() node).sequence;
95 	to = allocator.makeArray!Z(sequence.length);
96 
97 	foreach (index, ref element; sequence.enumerate) {
98 		element.convert(to[index], allocator);
99 	}
100 }
101 
102 void convert(To : Z[K], From : Node, Z, K)(in From node, ref To to, RCIAllocator allocator = theAllocator) if (!is(To == enum)) {
103 
104 	enforce(node.isMapping, text("Could not convert yaml ", dumper(node), " to ", typeid(To), " not a map"));
105 
106 	auto pairs = (cast() node).mapping;
107 
108 	foreach (pair; pairs) {
109 		Z value;
110 		K key;
111 
112 		pair.key.convert(key, allocator);
113 		pair.value.convert(value, allocator);
114 
115 		to[key] = value;
116 	}
117 }
118 
119 void convert(To, From : Node)(in From node, ref To to, RCIAllocator allocator = theAllocator)
120 	if (is(To == enum)) {
121 
122 	string temp;
123     node.convert!string(temp, allocator);
124     to = temp.to!To;
125 	temp.destruct(allocator);
126 }
127 
128 void destruct(To)(ref To to, RCIAllocator allocator = theAllocator) {
129 	destroy(to);
130 
131 	to = To.init;
132 }
133 
134 void destruct(To: Z[], Z)(ref To to, RCIAllocator allocator = theAllocator)
135 	if (!isSomeString!To) {
136 	allocator.dispose(to);
137 
138 	to = To.init;
139 }
140 
141 private string dumper(Node node) {
142 	import dyaml.stream;
143 	YMemoryStream stream = new YMemoryStream;
144 
145 	Dumper dumper = Dumper(stream);
146 	dumper.explicitStart = false;
147 	dumper.explicitEnd = false;
148 	dumper.dump(node);
149 
150 	return cast(string) stream.data.idup;
151 }