Skip to content

Commit

Permalink
Fix Loader#name to show the file name in exception message (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan authored Dec 28, 2021
1 parent f7dc530 commit da75f59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions source/dyaml/loader.d
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ struct Loader
void name(string name) pure @safe nothrow @nogc
{
name_ = name;
scanner_.name = name;
}

/// Specify custom Resolver to use.
Expand Down Expand Up @@ -392,3 +393,19 @@ struct Loader
auto doc = Loader.fromString(yaml).load();
assert(doc.isValid);
}

@safe unittest
{
import std.exception : collectException;

auto yaml = q"EOS
value: invalid: string
EOS";
auto filename = "invalid.yml";
auto loader = Loader.fromString(yaml);
loader.name = filename;

Node unused;
auto e = loader.load().collectException!ScannerException(unused);
assert(e.mark.name == filename);
}
3 changes: 3 additions & 0 deletions source/dyaml/reader.d
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ final class Reader
/// Get file name.
string name() const @safe pure nothrow @nogc { return name_; }

/// Set file name.
void name(string name) pure @safe nothrow @nogc { name_ = name; }

/// Get current line number.
uint line() const @safe pure nothrow @nogc { return line_; }

Expand Down
6 changes: 6 additions & 0 deletions source/dyaml/scanner.d
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ struct Scanner
return tokens_.empty;
}

/// Set file name.
void name(string name) @safe pure nothrow @nogc
{
reader_.name = name;
}

private:
/// Most scanning error messages have the same format; so build them with this
/// function.
Expand Down

0 comments on commit da75f59

Please sign in to comment.