<commit_before>/*
 * waysome - wayland based window manager
 *
 * Copyright in alphabetical order:
 *
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 *
 * This file is part of waysome.
 *
 * waysome is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * waysome is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with waysome. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __WS_SERIALIZE_SERIALIZER_H__
#define __WS_SERIALIZE_SERIALIZER_H__

#include "util/attributes.h"

#include <malloc.h>

// Forward declarations
struct ws_serializer;
struct ws_message;


/**
 * The serialization callback takes the serializer itself, a pointer to
 * a target buffer to serialize to (`buf`), the length of the buffer in bytes
 * (`nbuf`).
 * The function should serialize the message stored in the serializer, `unref`
 * the message and set the corresponding pointer of the serializer to `NULL` and
 * finally return the number of bytes written.
 * If the buffer's length is not sufficient for writing the message, as much of
 * as possible should be written and the function should update the internal
 * state in way that future invocations can continue serialisation, not freeing
 * the message.
 * If an internal error occurred, a negative error value may be returned.
 */
typedef ssize_t (*ws_serialize_f)(struct ws_serializer* self,
                                   char* buf, size_t nbuf);


/**
 * Deserializer type
 *
 * @attention This type is abstract
 *
 * A deserializer is initialized by a preparing function, which sets the
 * callbacks and initializes the internal state to be used for parsing.
 */
struct ws_serializer {
    ws_serialize_f serialize; //!< serialization function
    void (*deinit)(void*); //!< deinitialize the internal state
    void* state; //!< internal state of the serializer
    struct ws_message* buffer; //!< storage for an incompletely written message
};

/**
 * Serialize a message<commit_msg>Fix documentation of the serializer type<commit_after>/*
 * waysome - wayland based window manager
 *
 * Copyright in alphabetical order:
 *
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 *
 * This file is part of waysome.
 *
 * waysome is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * waysome is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with waysome. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __WS_SERIALIZE_SERIALIZER_H__
#define __WS_SERIALIZE_SERIALIZER_H__

#include "util/attributes.h"

#include <malloc.h>

// Forward declarations
struct ws_serializer;
struct ws_message;


/**
 * The serialization callback takes the serializer itself, a pointer to
 * a target buffer to serialize to (`buf`), the length of the buffer in bytes
 * (`nbuf`).
 * The function should serialize the message stored in the serializer, `unref`
 * the message and set the corresponding pointer of the serializer to `NULL` and
 * finally return the number of bytes written.
 * If the buffer's length is not sufficient for writing the message, as much of
 * as possible should be written and the function should update the internal
 * state in way that future invocations can continue serialisation, not freeing
 * the message.
 * If an internal error occurred, a negative error value may be returned.
 */
typedef ssize_t (*ws_serialize_f)(struct ws_serializer* self,
                                   char* buf, size_t nbuf);


/**
 * Serializer type
 *
 * @attention This type is abstract
 *
 * A Serializer is initialized by a preparing function, which sets the
 * callbacks and initializes the internal state to be used for parsing.
 */
struct ws_serializer {
    ws_serialize_f serialize; //!< serialization function
    void (*deinit)(void*); //!< deinitialize the internal state
    void* state; //!< internal state of the serializer
    struct ws_message* buffer; //!< storage for an incompletely written message
};

/**
 * Serialize a message
{'fraction_non_alphanumeric': 0.07711981113515641, 'fraction_numerical': 0.016525673814676373, 'mean_word_length': 3.769230769230769, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '785108', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1338, 'n_words': 654}