aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ext_depends/D-YAML/examples/constructor
diff options
context:
space:
mode:
authorRalph Amissah <ralph.amissah@gmail.com>2021-02-19 17:10:51 -0500
committerRalph Amissah <ralph.amissah@gmail.com>2021-02-24 16:46:47 -0500
commit02ca32ae0a5bc290918d2b2a3288e385b9cc6b11 (patch)
tree06379785e8a0165a7deb981c2eba362894820634 /src/ext_depends/D-YAML/examples/constructor
parentbuild from static source-tree pre fetch depends (diff)
external & build dependences in src tree
- external & build dependences boost licensed - ext_depends (external depends) - D-YAML - tinyendian - d2sqlite3 - imageformats - build_depends - dub2nix
Diffstat (limited to 'src/ext_depends/D-YAML/examples/constructor')
-rw-r--r--src/ext_depends/D-YAML/examples/constructor/dub.json10
-rw-r--r--src/ext_depends/D-YAML/examples/constructor/input.yaml8
-rw-r--r--src/ext_depends/D-YAML/examples/constructor/main.d91
3 files changed, 109 insertions, 0 deletions
diff --git a/src/ext_depends/D-YAML/examples/constructor/dub.json b/src/ext_depends/D-YAML/examples/constructor/dub.json
new file mode 100644
index 0000000..1aca4ee
--- /dev/null
+++ b/src/ext_depends/D-YAML/examples/constructor/dub.json
@@ -0,0 +1,10 @@
+{
+ "name": "constructor",
+ "targetType": "executable",
+ "sourceFiles": ["main.d"],
+ "mainSourceFile": "main.d",
+ "dependencies":
+ {
+ "dyaml": { "version" : "*"}
+ }
+}
diff --git a/src/ext_depends/D-YAML/examples/constructor/input.yaml b/src/ext_depends/D-YAML/examples/constructor/input.yaml
new file mode 100644
index 0000000..8d56926
--- /dev/null
+++ b/src/ext_depends/D-YAML/examples/constructor/input.yaml
@@ -0,0 +1,8 @@
+scalar-red: !color FF0000
+scalar-orange: !color FFFF00
+mapping-red: !color-mapping {r: 255, g: 0, b: 0}
+mapping-orange:
+ !color-mapping
+ r: 255
+ g: 255
+ b: 0
diff --git a/src/ext_depends/D-YAML/examples/constructor/main.d b/src/ext_depends/D-YAML/examples/constructor/main.d
new file mode 100644
index 0000000..50826f1
--- /dev/null
+++ b/src/ext_depends/D-YAML/examples/constructor/main.d
@@ -0,0 +1,91 @@
+import std.stdio;
+import std.string;
+import dyaml;
+
+struct Color
+{
+ ubyte red;
+ ubyte green;
+ ubyte blue;
+
+ this(ubyte r, ubyte g, ubyte b) @safe
+ {
+ red = r;
+ green = g;
+ blue = b;
+ }
+
+ this(const Node node, string tag) @safe
+ {
+ if (tag == "!color-mapping")
+ {
+ //Will throw if a value is missing, is not an integer, or is out of range.
+ red = node["r"].as!ubyte;
+ green = node["g"].as!ubyte;
+ blue = node["b"].as!ubyte;
+ }
+ else
+ {
+ string value = node.as!string;
+
+ if(value.length != 6)
+ {
+ throw new Exception("Invalid color: " ~ value);
+ }
+ //We don't need to check for uppercase chars this way.
+ value = value.toLower();
+
+ //Get value of a hex digit.
+ uint hex(char c)
+ {
+ import std.ascii;
+ if(!std.ascii.isHexDigit(c))
+ {
+ throw new Exception("Invalid color: " ~ value);
+ }
+
+ if(std.ascii.isDigit(c))
+ {
+ return c - '0';
+ }
+ return c - 'a' + 10;
+ }
+
+ red = cast(ubyte)(16 * hex(value[0]) + hex(value[1]));
+ green = cast(ubyte)(16 * hex(value[2]) + hex(value[3]));
+ blue = cast(ubyte)(16 * hex(value[4]) + hex(value[5]));
+ }
+ }
+}
+
+void main(string[] args)
+{
+ auto red = Color(255, 0, 0);
+ auto orange = Color(255, 255, 0);
+
+ string path = "input.yaml";
+ if (args.length > 1)
+ {
+ path = args[1];
+ }
+
+ try
+ {
+ auto root = Loader.fromFile(path).load();
+
+ if(root["scalar-red"].as!Color == red &&
+ root["mapping-red"].as!Color == red &&
+ root["scalar-orange"].as!Color == orange &&
+ root["mapping-orange"].as!Color == orange)
+ {
+ writeln("SUCCESS");
+ return;
+ }
+ }
+ catch(YAMLException e)
+ {
+ writeln(e.msg);
+ }
+
+ writeln("FAILURE");
+}