Something went wrong. Try again.
A sans-I/O reader and writer for Matroska and WebM in Zig.
Something went wrong. Try again.
2.3 kB · 74 lines
Zig
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! What the metadata modules share: the error sets for reading and writing//! them, and small helpers for the types the schema uses over and over.
const std = @import("std");const Allocator = std.mem.Allocator;const ebml = @import("ebml.zig");
pub const Element = ebml.Element;pub const Writer = ebml.Writer;pub const Id = ebml.Reader.Id;
pub const ParseError = ebml.Iterator.Error || Allocator.Error || error{ /// An element the specification requires is absent. MissingElement, /// An element whose value is outside what the specification allows, /// such as a UUID that is not sixteen octets. BadValue,};
pub const WriteError = Writer.Error;
/// A copy of `s` in `arena`, so that what is parsed outlives the buffer it/// was parsed from.pub fn dupe(arena: Allocator, s: []const u8) Allocator.Error![]const u8 { return arena.dupe(u8, s);}
/// A sixteen-octet UUID element.pub fn uuid(e: Element) ParseError![16]u8 { if (e.data.len != 16) return error.BadValue; return e.data[0..16].*;}
/// The next value from a counter, scrambled by SplitMix64 so that the UIDs/// a muxer makes from consecutive seeds look nothing alike. Never zero,/// since a UID of zero is not allowed.pub fn nextUid(state: *u64) u64 { while (true) { state.* +%= 0x9E3779B97F4A7C15; var z = state.*; z = (z ^ (z >> 30)) *% 0xBF58476D1CE4E5B9; z = (z ^ (z >> 27)) *% 0x94D049BB133111EB; z ^= z >> 31; if (z != 0) return z; }}
/// Writes `v` unless it is the schema's default, `default`.pub fn uintUnless(w: *Writer, id: Id, v: u64, default: u64) WriteError!void { if (v != default) try w.uint(id, v);}
pub fn optUint(w: *Writer, id: Id, v: ?u64) WriteError!void { if (v) |x| try w.uint(id, x);}
pub fn optFloat(w: *Writer, id: Id, v: ?f64) WriteError!void { if (v) |x| try w.float(id, x);}
pub fn optString(w: *Writer, id: Id, v: ?[]const u8) WriteError!void { if (v) |x| try w.string(id, x);}
pub fn optFlag(w: *Writer, id: Id, v: ?bool) WriteError!void { if (v) |x| try w.flag(id, x);}
pub fn optUuid(w: *Writer, id: Id, v: ?[16]u8) WriteError!void { if (v) |x| try w.binary(id, &x);}