add File overload for Loader.fromFile (#163)
add File overload for Loader.fromFile merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
parent
894982e32d
commit
aee64a5161
|
@ -10,6 +10,7 @@ module dyaml.loader;
|
|||
|
||||
import std.exception;
|
||||
import std.file;
|
||||
import std.stdio : File;
|
||||
import std.string;
|
||||
|
||||
import dyaml.composer;
|
||||
|
@ -55,6 +56,7 @@ struct Loader
|
|||
/** Construct a Loader to load YAML from a file.
|
||||
*
|
||||
* Params: filename = Name of the file to load from.
|
||||
* file = Already-opened file to load from.
|
||||
*
|
||||
* Throws: YAMLException if the file could not be opened or read.
|
||||
*/
|
||||
|
@ -72,6 +74,13 @@ struct Loader
|
|||
.format(filename, e.msg));
|
||||
}
|
||||
}
|
||||
/// ditto
|
||||
static Loader fromFile(File file) @system
|
||||
{
|
||||
auto loader = Loader(file.byChunk(4096).join);
|
||||
loader.name_ = file.name;
|
||||
return loader;
|
||||
}
|
||||
|
||||
/** Construct a Loader to load YAML from a string.
|
||||
*
|
||||
|
@ -332,6 +341,19 @@ struct Loader
|
|||
auto rootNode = Loader.fromFile("example.yaml").load();
|
||||
assert(rootNode == "Hello world!");
|
||||
}
|
||||
/// Load single YAML document from an already-opened file:
|
||||
@system unittest
|
||||
{
|
||||
// Open a temporary file
|
||||
auto file = File.tmpfile;
|
||||
// Write valid YAML
|
||||
file.write("Hello world!");
|
||||
// Return to the beginning
|
||||
file.seek(0);
|
||||
// Load document
|
||||
auto rootNode = Loader.fromFile(file).load();
|
||||
assert(rootNode == "Hello world!");
|
||||
}
|
||||
/// Load all YAML documents from a file:
|
||||
@safe unittest
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue