| ... | @@ -1,11 +1,10 @@ | ... | @@ -1,11 +1,10 @@ |
| 1 | //! The simplest way to parse ZON at runtime is to use `fromSlice`/`fromSliceAlloc`. | 1 | //! Use `fromSlice`/`fromSliceAlloc` to parse a ZON string into a Zig value. Human readable errors |
| | 2 | //! are written to an out parameter. |
| 2 | //! | 3 | //! |
| 3 | //! Parsing from individual Zoir nodes is also available: | 4 | //! If your data is already available in tree form, consider `fromZoir`/`fromZoirAlloc`. |
| 4 | //! * `fromZoir`/`fromZoirAlloc` | | |
| 5 | //! | 5 | //! |
| 6 | //! To update an existing values, see the `updateFrom*` variants. To get human readable errors, use | 6 | //! To parse into an existing value, see the `updateFrom*` variants. For lower level control over |
| 7 | //! the `errors` option. For lower level control over parsing, see `std.zig.Zoir`. For importing ZON | 7 | //! parsing, see `std.zig.ZonGen`. For importing ZON at compile time, use `@import`. |
| 8 | //! at compile time, use `@import`. | | |
| 9 | | 8 | |
| 10 | const std = @import("std"); | 9 | const std = @import("std"); |
| 11 | const Allocator = std.mem.Allocator; | 10 | const Allocator = std.mem.Allocator; |
| ... | @@ -91,7 +90,8 @@ pub const Error = struct { | ... | @@ -91,7 +90,8 @@ pub const Error = struct { |
| 91 | }; | 90 | }; |
| 92 | }; | 91 | }; |
| 93 | | 92 | |
| 94 | /// Errors encountered while parsing ZON. | 93 | /// Errors encountered while parsing ZON. Consider logging this using the default format method to |
| | 94 | /// provide users with feedback on the failure. |
| 95 | pub const Errors = struct { | 95 | pub const Errors = struct { |
| 96 | arena: Allocator, | 96 | arena: Allocator, |
| 97 | list: std.ArrayList(Error), | 97 | list: std.ArrayList(Error), |
| ... | @@ -160,9 +160,11 @@ pub fn fromSlice(T: type, options: FromSliceOptions) error{ OutOfMemory, ParseZo | ... | @@ -160,9 +160,11 @@ pub fn fromSlice(T: type, options: FromSliceOptions) error{ OutOfMemory, ParseZo |
| 160 | }); | 160 | }); |
| 161 | } | 161 | } |
| 162 | | 162 | |
| 163 | /// Similar to `fromSlice`, but updates an existing value `current`. Fields not specified by ZON are | 163 | /// Similar to `fromSlice`, but updates `value` in place which is assumed to hold a valid value. Any |
| 164 | /// left at their current values. Pointers are replaced with new values not mutated in place. On | 164 | /// unspecified fields are left unchanged. |
| 165 | /// failure, values that were updated prior to the failure are left updated. | 165 | /// |
| | 166 | /// Untagged unions and slices are replaced in their entiredy. If an error occurs while parsing, |
| | 167 | /// `value` remains valid but may be partially updated. |
| 166 | pub fn updateFromSlice( | 168 | pub fn updateFromSlice( |
| 167 | T: type, | 169 | T: type, |
| 168 | value: *T, | 170 | value: *T, |
| ... | @@ -181,7 +183,7 @@ pub fn updateFromSlice( | ... | @@ -181,7 +183,7 @@ pub fn updateFromSlice( |
| 181 | pub const FromSliceAllocOptions = struct { | 183 | pub const FromSliceAllocOptions = struct { |
| 182 | /// Used for scratch allocations. | 184 | /// Used for scratch allocations. |
| 183 | gpa: Allocator, | 185 | gpa: Allocator, |
| 184 | /// Owns the result. | 186 | /// Used for any allocations required by the out type. |
| 185 | arena: Allocator, | 187 | arena: Allocator, |
| 186 | /// The ZON source to parse. | 188 | /// The ZON source to parse. |
| 187 | source: [:0]const u8, | 189 | source: [:0]const u8, |
| ... | @@ -303,7 +305,7 @@ pub fn updateFromZoir( | ... | @@ -303,7 +305,7 @@ pub fn updateFromZoir( |
| 303 | } | 305 | } |
| 304 | | 306 | |
| 305 | pub const FromZoirAllocOptions = struct { | 307 | pub const FromZoirAllocOptions = struct { |
| 306 | /// Owns the result. | 308 | /// Used for any allocations required by the out type. |
| 307 | arena: Allocator, | 309 | arena: Allocator, |
| 308 | /// The AST for this ZON value. | 310 | /// The AST for this ZON value. |
| 309 | ast: *const Ast, | 311 | ast: *const Ast, |