Simplified the Constructor API.
This commit is contained in:
parent
ab154480fb
commit
fbc962623d
16 changed files with 217 additions and 297 deletions
|
@ -3,13 +3,13 @@ Custom YAML data types
|
|||
======================
|
||||
|
||||
Sometimes you need to serialize complex data types such as classes. To do this
|
||||
you could use plain nodes such as mappings with class data members. However,
|
||||
YAML supports custom types with identifiers called *tags*. That is the topic of
|
||||
this tutorial.
|
||||
you could use plain nodes such as mappings with class data members. YAML also
|
||||
supports custom types with identifiers called *tags*. That is the topic of this
|
||||
tutorial.
|
||||
|
||||
Each YAML node has a tag specifying its type. For instance: strings use the tag
|
||||
``tag:yaml.org,2002:str``. Tags of most default types are *implicitly resolved*
|
||||
during parsing, so you don't need to specify tag for each float, integer, etc.
|
||||
during parsing - you don't need to specify tag for each float, integer, etc.
|
||||
D:YAML can also implicitly resolve custom tags, as we will show later.
|
||||
|
||||
|
||||
|
@ -24,7 +24,7 @@ the *addConstructorXXX()* methods, where *XXX* is *Scalar*, *Sequence* or
|
|||
*Mapping*. *Constructor* is then passed to *Loader*, which parses YAML input.
|
||||
|
||||
Struct types have no specific requirements for YAML support. Class types should
|
||||
define the *opEquals()* operator, as this is used in equality comparisons of
|
||||
define the *opEquals()* operator - this is used in equality comparisons of
|
||||
nodes. Default class *opEquals()* compares references, which means two identical
|
||||
objects might be considered unequal. (Default struct *opEquals()* compares
|
||||
byte-by-byte, sometimes you might want to override that as well.)
|
||||
|
@ -41,24 +41,26 @@ following struct:
|
|||
ubyte blue;
|
||||
}
|
||||
|
||||
First, we need a function to construct our data type. It must take two *Mark*
|
||||
structs, which store position of the node in the file, and a reference to *Node*
|
||||
to construct from. The node is guaranteed to contain either a *string*, an array
|
||||
of *Node* or of *Node.Pair*, depending on whether we're constructing our value
|
||||
from a scalar, sequence, or mapping, respectively. In this tutorial, we have
|
||||
functions to construct a color from a scalar, using CSS-like format, RRGGBB, or
|
||||
from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
|
||||
of these functions:
|
||||
First, we need a function to construct our data type. The function will take a
|
||||
reference to *Node* to construct from. The node is guaranteed to contain either
|
||||
a *string*, an array of *Node* or of *Node.Pair*, depending on whether we're
|
||||
constructing our value from a scalar, sequence, or mapping, respectively.
|
||||
If this function throws any exception, D:YAML handles it and adds its message
|
||||
to a *YAMLException* that will be thrown when loading the file.
|
||||
|
||||
In this tutorial, we have functions to construct a color from a scalar, using
|
||||
CSS-like format, RRGGBB, or from a mapping, where we use the following format:
|
||||
{r:RRR, g:GGG, b:BBB} . Code of these functions:
|
||||
|
||||
.. code-block:: d
|
||||
|
||||
Color constructColorScalar(Mark start, Mark end, ref Node node)
|
||||
Color constructColorScalar(ref Node node)
|
||||
{
|
||||
string value = node.as!string;
|
||||
|
||||
if(value.length != 6)
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
throw new Exception("Invalid color: " ~ value);
|
||||
}
|
||||
//We don't need to check for uppercase chars this way.
|
||||
value = value.toLower();
|
||||
|
@ -68,7 +70,7 @@ of these functions:
|
|||
{
|
||||
if(!std.ascii.isHexDigit(c))
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
throw new Exception("Invalid color: " ~ value);
|
||||
}
|
||||
|
||||
if(std.ascii.isDigit(c))
|
||||
|
@ -86,21 +88,16 @@ of these functions:
|
|||
return result;
|
||||
}
|
||||
|
||||
Color constructColorMapping(Mark start, Mark end, ref Node node)
|
||||
Color constructColorMapping(ref Node node)
|
||||
{
|
||||
ubyte r,g,b;
|
||||
|
||||
//Might throw if a value is missing is not an integer, or is out of range.
|
||||
try
|
||||
{
|
||||
r = node["r"].as!ubyte;
|
||||
g = node["g"].as!ubyte;
|
||||
b = node["b"].as!ubyte;
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ e.msg, start, end);
|
||||
}
|
||||
//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);
|
||||
}
|
||||
|
|
|
@ -95,14 +95,20 @@
|
|||
</table></div>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructorScalar"></a><span class="ddoc_psymbol">addConstructorScalar</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
<dt class="d_decl">void <a name="addConstructorScalar"></a><span class="ddoc_psymbol">addConstructorScalar</span>(T)(in string <b>tag</b>, T function(ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function from scalar.
|
||||
</p>
|
||||
<p>The function must take two Marks (start and end positions of
|
||||
the node in file) and a reference to Node to construct from.
|
||||
<p>The function must take a reference to Node to construct from.
|
||||
The node contains a string for scalars, Node[] for sequences and
|
||||
Node.Pair[] for mappings.
|
||||
<br>
|
||||
|
||||
Any exception thrown by this function will be caught by D:YAML and
|
||||
its message will be added to a YAMLException that will also tell the
|
||||
user which type failed to construct, and position in the file.
|
||||
<br>
|
||||
|
||||
The value returned by this function will be stored in the resulting node.
|
||||
<br>
|
||||
|
||||
|
@ -124,20 +130,13 @@
|
|||
<span class="d_keyword">int</span> x, y, z;
|
||||
}
|
||||
|
||||
MyStruct constructMyStructScalar(Mark start, Mark end, <span class="d_keyword">ref</span> Node node)
|
||||
MyStruct constructMyStructScalar(<span class="d_keyword">ref</span> Node node)
|
||||
{
|
||||
<span class="d_comment">//Guaranteed to be string as we construct from scalar.
|
||||
</span> <span class="d_comment">//!mystruct x:y:z
|
||||
</span> <span class="d_keyword">auto</span> parts = node.as!string().split(<span class="d_string">":"</span>);
|
||||
<span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(to!<span class="d_keyword">int</span>(parts[0]), to!<span class="d_keyword">int</span>(parts[1]), to!<span class="d_keyword">int</span>(parts[2]));
|
||||
}
|
||||
<span class="d_keyword">catch</span>(Exception e)
|
||||
{
|
||||
<span class="d_keyword">throw</span> <span class="d_keyword">new</span> ConstructorException(<span class="d_string">"Could not construct MyStruct: "</span> ~ e.msg,
|
||||
start, end);
|
||||
}
|
||||
<span class="d_comment">//If this throws, the D:YAML will handle it and throw a YAMLException.
|
||||
</span> <span class="d_keyword">return</span> MyStruct(to!<span class="d_keyword">int</span>(parts[0]), to!<span class="d_keyword">int</span>(parts[1]), to!<span class="d_keyword">int</span>(parts[2]));
|
||||
}
|
||||
|
||||
<span class="d_keyword">void</span> main()
|
||||
|
@ -152,7 +151,7 @@
|
|||
</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructorSequence"></a><span class="ddoc_psymbol">addConstructorSequence</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
<dt class="d_decl">void <a name="addConstructorSequence"></a><span class="ddoc_psymbol">addConstructorSequence</span>(T)(in string <b>tag</b>, T function(ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function from sequence.
|
||||
</p>
|
||||
|
@ -169,19 +168,11 @@
|
|||
<span class="d_keyword">int</span> x, y, z;
|
||||
}
|
||||
|
||||
MyStruct constructMyStructSequence(Mark start, Mark end, <span class="d_keyword">ref</span> Node node)
|
||||
MyStruct constructMyStructSequence(<span class="d_keyword">ref</span> Node node)
|
||||
{
|
||||
<span class="d_comment">//node is guaranteed to be sequence.
|
||||
</span> <span class="d_comment">//!mystruct [x, y, z]
|
||||
</span> <span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(node[0].as!<span class="d_keyword">int</span>, node[1].as!<span class="d_keyword">int</span>, node[2].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
<span class="d_keyword">catch</span>(NodeException e)
|
||||
{
|
||||
<span class="d_keyword">throw</span> <span class="d_keyword">new</span> ConstructorException(<span class="d_string">"Could not construct MyStruct: "</span> ~ e.msg,
|
||||
start, end);
|
||||
}
|
||||
</span> <span class="d_keyword">return</span> MyStruct(node[0].as!<span class="d_keyword">int</span>, node[1].as!<span class="d_keyword">int</span>, node[2].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
|
||||
<span class="d_keyword">void</span> main()
|
||||
|
@ -196,7 +187,7 @@
|
|||
</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">void <a name="addConstructorMapping"></a><span class="ddoc_psymbol">addConstructorMapping</span>(T)(in string <b>tag</b>, T function(Mark, Mark, ref Node) <b>ctor</b>);
|
||||
<dt class="d_decl">void <a name="addConstructorMapping"></a><span class="ddoc_psymbol">addConstructorMapping</span>(T)(in string <b>tag</b>, T function(ref Node) <b>ctor</b>);
|
||||
</dt>
|
||||
<dd><p>Add a constructor function from a mapping.
|
||||
</p>
|
||||
|
@ -213,19 +204,11 @@
|
|||
<span class="d_keyword">int</span> x, y, z;
|
||||
}
|
||||
|
||||
MyStruct constructMyStructMapping(Mark start, Mark end, <span class="d_keyword">ref</span> Node node)
|
||||
MyStruct constructMyStructMapping(<span class="d_keyword">ref</span> Node node)
|
||||
{
|
||||
<span class="d_comment">//node is guaranteed to be mapping.
|
||||
</span> <span class="d_comment">//!mystruct {"x": x, "y": y, "z": z}
|
||||
</span> <span class="d_keyword">try</span>
|
||||
{
|
||||
<span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
<span class="d_keyword">catch</span>(NodeException e)
|
||||
{
|
||||
<span class="d_keyword">throw</span> <span class="d_keyword">new</span> ConstructorException(<span class="d_string">"Could not construct MyStruct: "</span> ~ e.msg,
|
||||
start, end);
|
||||
}
|
||||
</span> <span class="d_keyword">return</span> MyStruct(node[<span class="d_string">"x"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"y"</span>].as!<span class="d_keyword">int</span>, node[<span class="d_string">"z"</span>].as!<span class="d_keyword">int</span>);
|
||||
}
|
||||
|
||||
<span class="d_keyword">void</span> main()
|
||||
|
@ -242,72 +225,72 @@
|
|||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
<dt class="d_decl">YAMLNull <a name="constructNull"></a><span class="ddoc_psymbol">constructNull</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">YAMLNull <a name="constructNull"></a><span class="ddoc_psymbol">constructNull</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a <b>null</b> <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">YAMLMerge <a name="constructMerge"></a><span class="ddoc_psymbol">constructMerge</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">YAMLMerge <a name="constructMerge"></a><span class="ddoc_psymbol">constructMerge</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a merge <b>node</b> - a <b>node</b> that merges another <b>node</b> into a mapping.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">bool <a name="constructBool"></a><span class="ddoc_psymbol">constructBool</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">bool <a name="constructBool"></a><span class="ddoc_psymbol">constructBool</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a boolean <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">long <a name="constructLong"></a><span class="ddoc_psymbol">constructLong</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">long <a name="constructLong"></a><span class="ddoc_psymbol">constructLong</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an integer (long) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">real <a name="constructReal"></a><span class="ddoc_psymbol">constructReal</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">real <a name="constructReal"></a><span class="ddoc_psymbol">constructReal</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a floating point (real) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">ubyte[] <a name="constructBinary"></a><span class="ddoc_psymbol">constructBinary</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">ubyte[] <a name="constructBinary"></a><span class="ddoc_psymbol">constructBinary</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a binary (base64) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">SysTime <a name="constructTimestamp"></a><span class="ddoc_psymbol">constructTimestamp</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">SysTime <a name="constructTimestamp"></a><span class="ddoc_psymbol">constructTimestamp</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a timestamp (SysTime) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">string <a name="constructString"></a><span class="ddoc_psymbol">constructString</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">string <a name="constructString"></a><span class="ddoc_psymbol">constructString</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a string <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="getPairs"></a><span class="ddoc_psymbol">getPairs</span>(string <b>type</b>, Mark <b>start</b>, Mark <b>end</b>, Node[] <b>nodes</b>);
|
||||
<dt class="d_decl">Pair[] <a name="getPairs"></a><span class="ddoc_psymbol">getPairs</span>(string <b>type</b>, Node[] <b>nodes</b>);
|
||||
</dt>
|
||||
<dd><p>Convert a sequence of single-element mappings into a sequence of pairs.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructOrderedMap"></a><span class="ddoc_psymbol">constructOrderedMap</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructOrderedMap"></a><span class="ddoc_psymbol">constructOrderedMap</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an ordered map (ordered sequence of key:value pairs without duplicates) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructPairs"></a><span class="ddoc_psymbol">constructPairs</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructPairs"></a><span class="ddoc_psymbol">constructPairs</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a pairs (ordered sequence of key: value pairs allowing duplicates) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Node[] <a name="constructSet"></a><span class="ddoc_psymbol">constructSet</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">Node[] <a name="constructSet"></a><span class="ddoc_psymbol">constructSet</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a set <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Node[] <a name="constructSequence"></a><span class="ddoc_psymbol">constructSequence</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">Node[] <a name="constructSequence"></a><span class="ddoc_psymbol">constructSequence</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct a sequence (array) <b>node</b>.</p>
|
||||
|
||||
</dd>
|
||||
<dt class="d_decl">Pair[] <a name="constructMap"></a><span class="ddoc_psymbol">constructMap</span>(Mark <b>start</b>, Mark <b>end</b>, ref Node <b>node</b>);
|
||||
<dt class="d_decl">Pair[] <a name="constructMap"></a><span class="ddoc_psymbol">constructMap</span>(ref Node <b>node</b>);
|
||||
</dt>
|
||||
<dd><p>Construct an unordered map (unordered set of key: value pairs without duplicates) <b>node</b>.</p>
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ struct appears in Phobos.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -104,7 +104,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -87,7 +87,7 @@
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -48,12 +48,12 @@
|
|||
<div class="section" id="custom-yaml-data-types">
|
||||
<h1>Custom YAML data types<a class="headerlink" href="#custom-yaml-data-types" title="Permalink to this headline">¶</a></h1>
|
||||
<p>Sometimes you need to serialize complex data types such as classes. To do this
|
||||
you could use plain nodes such as mappings with class data members. However,
|
||||
YAML supports custom types with identifiers called <em>tags</em>. That is the topic of
|
||||
this tutorial.</p>
|
||||
you could use plain nodes such as mappings with class data members. YAML also
|
||||
supports custom types with identifiers called <em>tags</em>. That is the topic of this
|
||||
tutorial.</p>
|
||||
<p>Each YAML node has a tag specifying its type. For instance: strings use the tag
|
||||
<tt class="docutils literal"><span class="pre">tag:yaml.org,2002:str</span></tt>. Tags of most default types are <em>implicitly resolved</em>
|
||||
during parsing, so you don’t need to specify tag for each float, integer, etc.
|
||||
during parsing - you don’t need to specify tag for each float, integer, etc.
|
||||
D:YAML can also implicitly resolve custom tags, as we will show later.</p>
|
||||
<div class="section" id="constructor">
|
||||
<h2>Constructor<a class="headerlink" href="#constructor" title="Permalink to this headline">¶</a></h2>
|
||||
|
@ -63,7 +63,7 @@ functions to process each supported tag. These are supplied by the user using
|
|||
the <em>addConstructorXXX()</em> methods, where <em>XXX</em> is <em>Scalar</em>, <em>Sequence</em> or
|
||||
<em>Mapping</em>. <em>Constructor</em> is then passed to <em>Loader</em>, which parses YAML input.</p>
|
||||
<p>Struct types have no specific requirements for YAML support. Class types should
|
||||
define the <em>opEquals()</em> operator, as this is used in equality comparisons of
|
||||
define the <em>opEquals()</em> operator - this is used in equality comparisons of
|
||||
nodes. Default class <em>opEquals()</em> compares references, which means two identical
|
||||
objects might be considered unequal. (Default struct <em>opEquals()</em> compares
|
||||
byte-by-byte, sometimes you might want to override that as well.)</p>
|
||||
|
@ -77,21 +77,22 @@ following struct:</p>
|
|||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>First, we need a function to construct our data type. It must take two <em>Mark</em>
|
||||
structs, which store position of the node in the file, and a reference to <em>Node</em>
|
||||
to construct from. The node is guaranteed to contain either a <em>string</em>, an array
|
||||
of <em>Node</em> or of <em>Node.Pair</em>, depending on whether we’re constructing our value
|
||||
from a scalar, sequence, or mapping, respectively. In this tutorial, we have
|
||||
functions to construct a color from a scalar, using CSS-like format, RRGGBB, or
|
||||
from a mapping, where we use the following format: {r:RRR, g:GGG, b:BBB} . Code
|
||||
of these functions:</p>
|
||||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
|
||||
<p>First, we need a function to construct our data type. The function will take a
|
||||
reference to <em>Node</em> to construct from. The node is guaranteed to contain either
|
||||
a <em>string</em>, an array of <em>Node</em> or of <em>Node.Pair</em>, depending on whether we’re
|
||||
constructing our value from a scalar, sequence, or mapping, respectively.
|
||||
If this function throws any exception, D:YAML handles it and adds its message
|
||||
to a <em>YAMLException</em> that will be thrown when loading the file.</p>
|
||||
<p>In this tutorial, we have functions to construct a color from a scalar, using
|
||||
CSS-like format, RRGGBB, or from a mapping, where we use the following format:
|
||||
{r:RRR, g:GGG, b:BBB} . Code of these functions:</p>
|
||||
<div class="highlight-d"><div class="highlight"><pre><span class="n">Color</span> <span class="n">constructColorScalar</span><span class="p">(</span><span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="nb">string</span> <span class="n">value</span> <span class="p">=</span> <span class="n">node</span><span class="p">.</span><span class="n">as</span><span class="p">!</span><span class="nb">string</span><span class="p">;</span>
|
||||
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">value</span><span class="p">.</span><span class="n">length</span> <span class="p">!=</span> <span class="mi">6</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="n">ConstructorException</span><span class="p">(</span><span class="s">"Invalid color: "</span> <span class="p">~</span> <span class="n">value</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="n">Exception</span><span class="p">(</span><span class="s">"Invalid color: "</span> <span class="p">~</span> <span class="n">value</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="c1">//We don't need to check for uppercase chars this way.</span>
|
||||
<span class="n">value</span> <span class="p">=</span> <span class="n">value</span><span class="p">.</span><span class="n">toLower</span><span class="p">();</span>
|
||||
|
@ -101,7 +102,7 @@ of these functions:</p>
|
|||
<span class="p">{</span>
|
||||
<span class="k">if</span><span class="p">(!</span><span class="n">std</span><span class="p">.</span><span class="n">ascii</span><span class="p">.</span><span class="n">isHexDigit</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="n">ConstructorException</span><span class="p">(</span><span class="s">"Invalid color: "</span> <span class="p">~</span> <span class="n">value</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="n">Exception</span><span class="p">(</span><span class="s">"Invalid color: "</span> <span class="p">~</span> <span class="n">value</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="k">if</span><span class="p">(</span><span class="n">std</span><span class="p">.</span><span class="n">ascii</span><span class="p">.</span><span class="n">isDigit</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>
|
||||
|
@ -119,21 +120,16 @@ of these functions:</p>
|
|||
<span class="k">return</span> <span class="n">result</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="n">Color</span> <span class="n">constructColorMapping</span><span class="p">(</span><span class="n">Mark</span> <span class="n">start</span><span class="p">,</span> <span class="n">Mark</span> <span class="n">end</span><span class="p">,</span> <span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
|
||||
<span class="n">Color</span> <span class="n">constructColorMapping</span><span class="p">(</span><span class="k">ref</span> <span class="n">Node</span> <span class="n">node</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="kt">ubyte</span> <span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">;</span>
|
||||
|
||||
<span class="c1">//Might throw if a value is missing is not an integer, or is out of range.</span>
|
||||
<span class="k">try</span>
|
||||
<span class="p">{</span>
|
||||
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"r"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"g"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"b"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">catch</span><span class="p">(</span><span class="n">NodeException</span> <span class="n">e</span><span class="p">)</span>
|
||||
<span class="p">{</span>
|
||||
<span class="k">throw</span> <span class="k">new</span> <span class="n">ConstructorException</span><span class="p">(</span><span class="s">"Invalid color: "</span> <span class="p">~</span> <span class="n">e</span><span class="p">.</span><span class="n">msg</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
<span class="c1">//If this happens, D:YAML will handle the exception and use its message</span>
|
||||
<span class="c1">//in a YAMLException thrown when loading.</span>
|
||||
<span class="n">r</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"r"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">g</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"g"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
<span class="n">b</span> <span class="p">=</span> <span class="n">node</span><span class="p">[</span><span class="s">"b"</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">ubyte</span><span class="p">;</span>
|
||||
|
||||
<span class="k">return</span> <span class="n">Color</span><span class="p">(</span><span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">r</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">g</span><span class="p">,</span> <span class="k">cast</span><span class="p">(</span><span class="kt">ubyte</span><span class="p">)</span><span class="n">b</span><span class="p">);</span>
|
||||
<span class="p">}</span>
|
||||
|
@ -368,7 +364,7 @@ directory of the D:YAML package.</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -237,7 +237,7 @@ example in the <tt class="docutils literal"><span class="pre">example/getting_st
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -330,7 +330,7 @@ Some of these might change in the future (especially !!map and !!set).</p>
|
|||
</div>
|
||||
<div class="footer">
|
||||
© Copyright 2011, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
|
||||
Last updated on Nov 16, 2011.
|
||||
Last updated on Nov 17, 2011.
|
||||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue