2025-06-27 21:53:40 +01:00
|
|
|
#include "serial.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/serial.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
extern const struct b_serial_format_ops z__b_bitcode_format_ops;
|
|
|
|
|
extern const struct b_serial_format_ops z__b_json_format_ops;
|
2025-09-22 10:54:33 +01:00
|
|
|
extern const struct b_serial_format_ops z__b_toml_format_ops;
|
2025-06-27 21:53:40 +01:00
|
|
|
|
|
|
|
|
static const struct b_serial_format_ops *format_ops[] = {
|
|
|
|
|
[B_SERIAL_FORMAT_NONE] = NULL,
|
|
|
|
|
[B_SERIAL_FORMAT_BITCODE] = &z__b_bitcode_format_ops,
|
|
|
|
|
[B_SERIAL_FORMAT_JSON] = &z__b_json_format_ops,
|
2025-09-22 10:54:33 +01:00
|
|
|
[B_SERIAL_FORMAT_TOML] = &z__b_toml_format_ops,
|
2025-06-27 21:53:40 +01:00
|
|
|
};
|
|
|
|
|
static const size_t nr_format_ops = sizeof format_ops / sizeof format_ops[0];
|
|
|
|
|
|
|
|
|
|
enum b_status b_serial_ctx_create(struct b_serial_ctx **out)
|
|
|
|
|
{
|
|
|
|
|
struct b_serial_ctx *ctx = malloc(sizeof *ctx);
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
return B_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(ctx, 0x0, sizeof *ctx);
|
|
|
|
|
|
2025-10-24 12:45:43 +01:00
|
|
|
ctx->ctx_streambuf = b_stream_buffer_create_dynamic(2048);
|
|
|
|
|
if (!ctx->ctx_streambuf) {
|
2025-06-27 21:53:40 +01:00
|
|
|
free(ctx);
|
2025-10-24 12:45:43 +01:00
|
|
|
return B_ERR_NO_MEMORY;
|
2025-06-27 21:53:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*out = ctx;
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum b_status b_serial_ctx_destroy(struct b_serial_ctx *ctx)
|
|
|
|
|
{
|
2025-10-24 12:45:43 +01:00
|
|
|
b_stream_buffer_unref(ctx->ctx_streambuf);
|
2025-06-27 21:53:40 +01:00
|
|
|
free(ctx);
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct b_serial_format_ops *get_formatter(enum b_serial_format fmt)
|
|
|
|
|
{
|
|
|
|
|
if (fmt < 0 || fmt >= nr_format_ops) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return format_ops[fmt];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum b_status b_serial_ctx_serialise(
|
2025-10-19 21:01:27 +01:00
|
|
|
struct b_serial_ctx *ctx, enum b_serial_format fmt, b_object *src,
|
2025-10-24 12:45:43 +01:00
|
|
|
b_stream *dest, enum b_serial_flags flags)
|
2025-06-27 21:53:40 +01:00
|
|
|
{
|
|
|
|
|
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
|
|
|
|
if (!ops) {
|
|
|
|
|
return B_ERR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ops->fmt_serialise) {
|
|
|
|
|
return B_ERR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ops->fmt_serialise(ctx, src, dest, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum b_status b_serial_ctx_deserialise(
|
2025-10-24 12:45:43 +01:00
|
|
|
struct b_serial_ctx *ctx, enum b_serial_format fmt, b_stream *src,
|
|
|
|
|
b_object **dest, enum b_serial_flags flags)
|
2025-06-27 21:53:40 +01:00
|
|
|
{
|
|
|
|
|
const struct b_serial_format_ops *ops = get_formatter(fmt);
|
|
|
|
|
if (!ops) {
|
|
|
|
|
return B_ERR_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ops->fmt_deserialise) {
|
|
|
|
|
return B_ERR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ops->fmt_deserialise(ctx, src, dest, flags);
|
|
|
|
|
}
|