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
|
@ -7,49 +7,28 @@ struct Color
|
|||
ubyte green;
|
||||
ubyte blue;
|
||||
|
||||
const int opCmp(ref const Color c)
|
||||
Node opCast(T: Node)() const
|
||||
{
|
||||
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;
|
||||
static immutable hex = "0123456789ABCDEF";
|
||||
|
||||
//Using the color format from the Constructor example.
|
||||
string scalar;
|
||||
foreach(channel; [red, green, blue])
|
||||
{
|
||||
scalar ~= hex[channel / 16];
|
||||
scalar ~= hex[channel % 16];
|
||||
}
|
||||
|
||||
//Representing as a scalar, with custom tag to specify this data type.
|
||||
return Node(scalar, "!color");
|
||||
}
|
||||
}
|
||||
|
||||
Node representColor(ref Node node, Representer representer) @safe
|
||||
{
|
||||
//The node is guaranteed to be Color as we add representer for Color.
|
||||
Color color = node.as!Color;
|
||||
|
||||
static immutable hex = "0123456789ABCDEF";
|
||||
|
||||
//Using the color format from the Constructor example.
|
||||
string scalar;
|
||||
foreach(channel; [color.red, color.green, color.blue])
|
||||
{
|
||||
scalar ~= hex[channel / 16];
|
||||
scalar ~= hex[channel % 16];
|
||||
}
|
||||
|
||||
//Representing as a scalar, with custom tag to specify this data type.
|
||||
return representer.representScalar("!color", scalar);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto representer = new Representer;
|
||||
representer.addRepresenter!Color(&representColor);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
import std.regex;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
auto dumper = dumper(File("output.yaml", "w").lockingTextWriter);
|
||||
dumper.representer = representer;
|
||||
dumper.resolver = resolver;
|
||||
|
||||
auto document = Node([Color(255, 0, 0),
|
||||
Color(0, 255, 0),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue