DoxigAlpha

fromSlice

Parses the given slice as ZON.

Returns error.OutOfMemory on allocation failure, or error.ParseZon error if the ZON is invalid or can not be deserialized into type T.

When the parser returns error.ParseZon, it will also store a human readable explanation in diag if non null. If diag is not null, it must be initialized to .{}.

Function parameters

Parameters

#
T:type
The type to deserialize into.
source:[:0]const u8
diag:?*Diagnostics

Configuration for the runtime parser.

Types

#
Options
Configuration for the runtime parser.
Diagnostics
Information about the success or failure of a parse.

Parses the given slice as ZON.

Functions

#
fromSlice
Parses the given slice as ZON.
fromZoir
Like `fromSlice`, but operates on `Zoir` instead of ZON source.
fromZoirNode
Like `fromZoir`, but the parse starts on `node` instead of root.
free
Frees ZON values.

Source

Implementation

#
pub fn fromSlice(
    /// The type to deserialize into. May not be or contain any of the following types:
    /// * Any comptime-only type, except in a comptime field
    /// * `type`
    /// * `void`, except as a union payload
    /// * `noreturn`
    /// * An error set/error union
    /// * A many-pointer or C-pointer
    /// * An opaque type, including `anyopaque`
    /// * An async frame type, including `anyframe` and `anyframe->T`
    /// * A function
    ///
    /// All other types are valid. Unsupported types will fail at compile time.
    T: type,
    gpa: Allocator,
    source: [:0]const u8,
    diag: ?*Diagnostics,
    options: Options,
) error{ OutOfMemory, ParseZon }!T {
    if (diag) |s| s.assertEmpty();

    var ast = try std.zig.Ast.parse(gpa, source, .zon);
    defer if (diag == null) ast.deinit(gpa);
    if (diag) |s| s.ast = ast;

    // If there's no diagnostics, Zoir exists for the lifetime of this function. If there is a
    // diagnostics, ownership is transferred to diagnostics.
    var zoir = try ZonGen.generate(gpa, ast, .{ .parse_str_lits = false });
    defer if (diag == null) zoir.deinit(gpa);

    if (diag) |s| s.* = .{};
    return fromZoir(T, gpa, ast, zoir, diag, options);
}