Loader ctor from buffer, deprecated old API, constructing Reader from buffer.
This commit is contained in:
parent
a23c41385a
commit
d18a41999e
|
@ -9,7 +9,8 @@ module dyaml.loader;
|
||||||
|
|
||||||
|
|
||||||
import std.exception;
|
import std.exception;
|
||||||
import std.stream;
|
import std.file;
|
||||||
|
import std.string;
|
||||||
|
|
||||||
import dyaml.composer;
|
import dyaml.composer;
|
||||||
import dyaml.constructor;
|
import dyaml.constructor;
|
||||||
|
@ -112,11 +113,14 @@ struct Loader
|
||||||
this(string filename) @trusted
|
this(string filename) @trusted
|
||||||
{
|
{
|
||||||
name_ = filename;
|
name_ = filename;
|
||||||
try { this(new File(filename)); }
|
try
|
||||||
catch(StreamException e)
|
|
||||||
{
|
{
|
||||||
throw new YAMLException("Unable to open file " ~ filename ~
|
this(std.file.read(filename));
|
||||||
" for YAML loading: " ~ e.msg);
|
}
|
||||||
|
catch(FileException e)
|
||||||
|
{
|
||||||
|
throw new YAMLException("Unable to open file %s for YAML loading: %s"
|
||||||
|
.format(filename, e.msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,30 +132,35 @@ struct Loader
|
||||||
///
|
///
|
||||||
/// Throws:
|
/// Throws:
|
||||||
///
|
///
|
||||||
/// YAMLException if data could not be parsed (e.g. a decoding error)
|
/// YAMLException if data could not be read (e.g. a decoding error)
|
||||||
|
deprecated("Loader.fromString(string) is deprecated. Use Loader.fromString(char[]) instead.")
|
||||||
static Loader fromString(string data)
|
static Loader fromString(string data)
|
||||||
{
|
{
|
||||||
return Loader(new MemoryStream(cast(char[])data));
|
return Loader(cast(ubyte[])data.dup);
|
||||||
}
|
}
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
assert(Loader.fromString("42").load().as!int == 42);
|
assert(Loader.fromString("42").load().as!int == 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import std.stream;
|
||||||
/// Construct a Loader to load YAML from a _stream.
|
/// Construct a Loader to load YAML from a _stream.
|
||||||
///
|
///
|
||||||
/// Params: stream = Stream to read from. Must be readable and seekable.
|
/// Params: stream = Stream to read from. Must be readable and seekable.
|
||||||
///
|
///
|
||||||
/// Throws: YAMLException if stream could not be read.
|
/// Throws: YAMLException if stream could not be read.
|
||||||
|
deprecated("Loader(Stream) is deprecated. Use Loader(ubyte[]) instead.")
|
||||||
this(Stream stream) @safe
|
this(Stream stream) @safe
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
reader_ = new Reader(stream);
|
import dyaml.streamcompat;
|
||||||
scanner_ = new Scanner(reader_);
|
auto streamBytes = streamToBytesGC(stream);
|
||||||
parser_ = new Parser(scanner_);
|
reader_ = new Reader(streamBytes);
|
||||||
resolver_ = new Resolver();
|
scanner_ = new Scanner(reader_);
|
||||||
constructor_ = new Constructor();
|
parser_ = new Parser(scanner_);
|
||||||
|
resolver_ = new Resolver();
|
||||||
|
constructor_ = new Constructor();
|
||||||
}
|
}
|
||||||
catch(YAMLException e)
|
catch(YAMLException e)
|
||||||
{
|
{
|
||||||
|
@ -160,6 +169,36 @@ struct Loader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct a Loader to load YAML from a buffer.
|
||||||
|
///
|
||||||
|
/// Params: yamlData = Buffer with YAML data to load. This may be e.g. a file
|
||||||
|
/// loaded to memory or a string with YAML data. Note that
|
||||||
|
/// buffer $(B will) be overwritten, as D:YAML minimizes
|
||||||
|
/// memory allocations by reusing the input _buffer.
|
||||||
|
///
|
||||||
|
/// D:YAML looks for byte-order-makrs YAML files encoded in UTF-16/UTF-32
|
||||||
|
/// (and sometimes UTF-8) use to specify the encoding and endianness, so it
|
||||||
|
/// should be enough to load an entire file to a buffer and pass it to D:YAML,
|
||||||
|
/// regardless of Unicode encoding.
|
||||||
|
///
|
||||||
|
/// Throws: YAMLException if yamlData contains data illegal in YAML.
|
||||||
|
this(void[] yamlData) @safe
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
reader_ = new Reader(cast(ubyte[])yamlData);
|
||||||
|
scanner_ = new Scanner(reader_);
|
||||||
|
parser_ = new Parser(scanner_);
|
||||||
|
resolver_ = new Resolver();
|
||||||
|
constructor_ = new Constructor();
|
||||||
|
}
|
||||||
|
catch(YAMLException e)
|
||||||
|
{
|
||||||
|
throw new YAMLException("Unable to open " ~ name_ ~
|
||||||
|
" for YAML loading: " ~ e.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Destroy the Loader.
|
/// Destroy the Loader.
|
||||||
@trusted ~this()
|
@trusted ~this()
|
||||||
{
|
{
|
||||||
|
@ -318,13 +357,13 @@ struct Loader
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.stream;
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
|
||||||
string yaml_input = "red: '#ff0000'\n"
|
string yaml_input = "red: '#ff0000'\n"
|
||||||
"green: '#00ff00'\n"
|
"green: '#00ff00'\n"
|
||||||
"blue: '#0000ff'";
|
"blue: '#0000ff'";
|
||||||
|
|
||||||
|
import std.stream;
|
||||||
auto colors = Loader(new MemoryStream(cast(char[])yaml_input)).load();
|
auto colors = Loader(new MemoryStream(cast(char[])yaml_input)).load();
|
||||||
|
|
||||||
foreach(string color, string value; colors)
|
foreach(string color, string value; colors)
|
||||||
|
|
Loading…
Reference in a new issue