Skip to content

Commit

Permalink
fix: don't include namespace errors for non-namespace-aware readers
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
ianprime0509 committed Oct 15, 2023
1 parent 11f9db5 commit d9409b1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ const predefined_ns_prefixes = ComptimeStringMap([]const u8, .{
pub const NamespaceContext = struct {
scopes: ArrayListUnmanaged(StringHashMapUnmanaged([]const u8)) = .{},

pub const Error = error{
CannotUndeclareNsPrefix,
InvalidNsBinding,
InvalidQName,
UndeclaredNsPrefix,
};

pub fn deinit(self: *NamespaceContext, allocator: Allocator) void {
while (self.scopes.items.len > 0) {
self.endScope(allocator);
Expand Down Expand Up @@ -271,6 +278,8 @@ pub const NamespaceContext = struct {
/// A drop-in replacement for `NamespaceContext` which doesn't actually do any
/// namespace processing.
pub const NoOpNamespaceContext = struct {
pub const Error = error{};

pub inline fn deinit(_: *NoOpNamespaceContext, _: Allocator) void {}

pub inline fn startScope(_: *NoOpNamespaceContext, _: Allocator) !void {}
Expand Down Expand Up @@ -397,7 +406,7 @@ pub fn Reader(comptime ReaderType: type, comptime options: ReaderOptions) type {
/// A stack of element names enclosing the current context.
element_names: ArrayListUnmanaged([]u8) = .{},
/// The namespace context of the reader.
namespace_context: if (options.namespace_aware) NamespaceContext else NoOpNamespaceContext = .{},
namespace_context: NamespaceContextType = .{},
/// A pending token which has been read but has not yet been handled as
/// part of an event.
pending_token: ?Token = null,
Expand Down Expand Up @@ -425,17 +434,14 @@ pub fn Reader(comptime ReaderType: type, comptime options: ReaderOptions) type {
.enable_normalization = options.enable_normalization,
.track_location = options.track_location,
});
const NamespaceContextType = if (options.namespace_aware) NamespaceContext else NoOpNamespaceContext;

pub const Error = error{
CannotUndeclareNsPrefix,
DuplicateAttribute,
InvalidNsBinding,
InvalidQName,
MismatchedEndTag,
UndeclaredEntityReference,
UndeclaredNsPrefix,
QNameNotAllowed,
} || Allocator.Error || TokenReaderType.Error;
} || Allocator.Error || TokenReaderType.Error || NamespaceContextType.Error;

pub fn init(allocator: Allocator, r: ReaderType, decoder: options.DecoderType) Self {
return .{
Expand Down

0 comments on commit d9409b1

Please sign in to comment.