luxem_rawwrite.h
typedef luxem_bool_t (*luxem_rawwrite_write_callback_t)(struct luxem_rawwrite_context_t *context, void *user_data, struct luxem_string_t const *string);
A callback that receives a chunk of rendered luxem data for custom writing.
Return `luxem_false` and set the error using `luxem_rawwrite_get_error` to indicate an error has occurred.
struct luxem_rawwrite_context_t *luxem_rawwrite_construct(void);
Returns a new writer. Before use, the writer must be configured with one of `luxem_rawwrite_set_write_callback`, `luxem_rawwrite_set_file_out`, or `luxem_rawwrite_set_buffer_out`.
void luxem_rawwrite_destroy(struct luxem_rawwrite_context_t *context);
Destroys and frees the writer.
void luxem_rawwrite_set_write_callback(struct luxem_rawwrite_context_t *context, luxem_rawwrite_write_callback_t callback, void *user_data);
Configures the writer to pass all generated text to `callback`, along with `user_data`, a pointer to arbitrary data.
void luxem_rawwrite_set_file_out(struct luxem_rawwrite_context_t *context, FILE *file);
Configures the writer to write all generated text to `file`.
luxem_bool_t luxem_rawwrite_set_buffer_out(struct luxem_rawwrite_context_t *context);
Configures the writer to write all generated text to an internal buffer. The written text can be accessed with `luxem_rawwrite_buffer_render`.
struct luxem_string_t *luxem_rawwrite_buffer_render(struct luxem_rawwrite_context_t *context);
Returns a string that contains all text generated by the writer. This only works if the writer was configured with `luxem_rawwrite_set_buffer_out`. Because the format of the internal buffer is unspecified, this function may perform taxing calculations. The user must free the returned `luxem_string_t` after use with `free`.
void luxem_rawwrite_set_pretty(struct luxem_rawwrite_context_t *context, char spacer, size_t multiple);
Enables prettification on output. This typically involves new lines after values and indentation of all elements in a block. `spacer` is the character to be used for indentation, and `multiple` specifies how many of `spacer` to use for each indentation level.
struct luxem_string_t *luxem_rawwrite_get_error(struct luxem_rawwrite_context_t *context);
Returns a pointer to the writer's internal error structure. This pointer should not be freed by the caller.
This should be used to get information about an error if a writer function fails.
Alternatively, this can be used to set information in a user callback. In this case, the call back should return luxem_false to indicate that the callback has failed and error has been set. The user must set `pointer` and `length` on the returned string.
luxem_bool_t luxem_rawwrite_object_begin(struct luxem_rawwrite_context_t *context);
Opens a new object in the output stream.
luxem_bool_t luxem_rawwrite_object_end(struct luxem_rawwrite_context_t *context);
Closes the current open object in the output stream. Returns `luxem_false` if an error occurred during the operation.
luxem_bool_t luxem_rawwrite_array_begin(struct luxem_rawwrite_context_t *context);
Opens a new array in the output stream. Returns `luxem_false` if an error occurred during the operation.
luxem_bool_t luxem_rawwrite_array_end(struct luxem_rawwrite_context_t *context);
Closes the current open object in the array stream. Returns `luxem_false` if an error occurred during the operation.
luxem_bool_t luxem_rawwrite_key(struct luxem_rawwrite_context_t *context, struct luxem_string_t const *string);
Writes `string` as a key to the output stream. This should only be called in an object. Automatically quotes the string if necessary. Returns `luxem_false` if an error occurred during the operation.
luxem_bool_t luxem_rawwrite_type(struct luxem_rawwrite_context_t *context, struct luxem_string_t const *string);
Writes `string` as a type to the output stream. This should always be immediately followed by a value. Automatically quotes the string if necessary. Returns `luxem_false` if an error occurred during the operation.
luxem_bool_t luxem_rawwrite_primitive(struct luxem_rawwrite_context_t *context, struct luxem_string_t const *string);
Writes `string` as a primitive to the output stream. Automatically quotes the string if necessary. Returns `luxem_false` if an error occurred during the operation.
struct luxem_string_t const *luxem_to_ascii16(struct luxem_string_t const *data, struct luxem_string_t *error);
Encodes `data` as ascii16 and returns the result as a `luxem_string_t`. The caller is responsible for releasing the returned `luxem_string_t` with free. If the function returns 0, error will contain an error message.