Move custom types to Node (#213)
Move custom types to Node merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
parent
beb160f1eb
commit
7f913246ea
20 changed files with 1152 additions and 1679 deletions
|
@ -8,80 +8,70 @@ struct Color
|
|||
ubyte green;
|
||||
ubyte blue;
|
||||
|
||||
const int opCmp(ref const Color c)
|
||||
this(ubyte r, ubyte g, ubyte b) @safe
|
||||
{
|
||||
if(red != c.red) {return red - c.red;}
|
||||
if(green != c.green){return green - c.green;}
|
||||
if(blue != c.blue) {return blue - c.blue;}
|
||||
return 0;
|
||||
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]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Color constructColorScalar(ref Node node) @safe
|
||||
void main(string[] args)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Color result;
|
||||
result.red = cast(ubyte)(16 * hex(value[0]) + hex(value[1]));
|
||||
result.green = cast(ubyte)(16 * hex(value[2]) + hex(value[3]));
|
||||
result.blue = cast(ubyte)(16 * hex(value[4]) + hex(value[5]));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Color constructColorMapping(ref Node node) @safe
|
||||
{
|
||||
ubyte r,g,b;
|
||||
|
||||
//Might throw if a value is missing is not an integer, or is out of range.
|
||||
//If this happens, D:YAML will handle the exception and use its message
|
||||
//in a YAMLException thrown when loading.
|
||||
r = node["r"].as!ubyte;
|
||||
g = node["g"].as!ubyte;
|
||||
b = node["b"].as!ubyte;
|
||||
|
||||
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto red = Color(255, 0, 0);
|
||||
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 constructor = new Constructor;
|
||||
//both functions handle the same tag, but one handles scalar, one mapping.
|
||||
constructor.addConstructorScalar("!color", &constructColorScalar);
|
||||
constructor.addConstructorMapping("!color-mapping", &constructColorMapping);
|
||||
|
||||
auto loader = Loader.fromFile("input.yaml");
|
||||
loader.constructor = constructor;
|
||||
|
||||
auto root = loader.load();
|
||||
auto root = Loader.fromFile(path).load();
|
||||
|
||||
if(root["scalar-red"].as!Color == red &&
|
||||
root["mapping-red"].as!Color == red &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue