<commit_before>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 <NAME>
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef _FNORD_MSG_MSG_H
#define _FNORD_MSG_MSG_H
#include <fnord-base/stdtypes.h>
#include <fnord-base/buffer.h>

namespace fnord {
namespace msg {

template <typename ProtoType>
ProtoType decode(const Buffer& buffer);

template <typename ProtoType>
void decode(const Buffer& buffer, ProtoType* target);

template <typename ProtoType>
ProtoType decode(const BufferRef& buffer);

template <typename ProtoType>
void decode(const BufferRef& buffer, ProtoType* target);

template <typename ProtoType>
ProtoType decode(const String& buffer);

template <typename ProtoType>
ProtoType decode(const void* data, size_t size);

template <typename ProtoType>
void decode(const void* data, size_t size, ProtoType* target);

template <typename ProtoType>
BufferRef encode(const ProtoType& proto);

template <typename ProtoType>
void encode(const ProtoType& proto, Buffer* target);

template <typename ProtoType>
void encode(const ProtoType& proto, BufferRef target);

// impl...

template <typename ProtoType>
ProtoType decode(const Buffer& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer.data(), buffer.size(), &proto);
  return proto;
}

template <typename ProtoType>
void decode(const Buffer& buffer, ProtoType* target) {
  decode<ProtoType>(buffer.data(), buffer.size(), target);
}

template <typename ProtoType>
ProtoType decode(const BufferRef& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer->data(), buffer->size(), &proto);
  return proto;
}

template <typename ProtoType>
void decode(const BufferRef& buffer, ProtoType* target) {
  decode<ProtoType>(buffer->data(), buffer->size(), target);
}

template <typename ProtoType>
ProtoType decode(const String& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer.data(), buffer.size(), &proto);
  return proto;
}

template <typename ProtoType>
ProtoType decode(const void* data, size_t size) {
  ProtoType proto;
  decode<ProtoType>(data, size, &proto);
  return proto;
}

template <typename ProtoType>
void decode(const void* data, size_t size, ProtoType* target) {
  target->ParseFromArray(data, size);
}

template <typename ProtoType>
BufferRef encode(const ProtoType& proto) {
  BufferRef buf(new Buffer());
  encode<ProtoType>(proto, buf);
  return buf;
}

template <typename ProtoType>
void encode(const ProtoType& proto, BufferRef target) {
  encode<ProtoType>(proto, target.get());
}
<commit_msg>raise on invalid proto<commit_after>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 <NAME>
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef _FNORD_MSG_MSG_H
#define _FNORD_MSG_MSG_H
#include <fnord-base/stdtypes.h>
#include <fnord-base/buffer.h>

namespace fnord {
namespace msg {

template <typename ProtoType>
ProtoType decode(const Buffer& buffer);

template <typename ProtoType>
void decode(const Buffer& buffer, ProtoType* target);

template <typename ProtoType>
ProtoType decode(const BufferRef& buffer);

template <typename ProtoType>
void decode(const BufferRef& buffer, ProtoType* target);

template <typename ProtoType>
ProtoType decode(const String& buffer);

template <typename ProtoType>
ProtoType decode(const void* data, size_t size);

template <typename ProtoType>
void decode(const void* data, size_t size, ProtoType* target);

template <typename ProtoType>
BufferRef encode(const ProtoType& proto);

template <typename ProtoType>
void encode(const ProtoType& proto, Buffer* target);

template <typename ProtoType>
void encode(const ProtoType& proto, BufferRef target);

// impl...

template <typename ProtoType>
ProtoType decode(const Buffer& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer.data(), buffer.size(), &proto);
  return proto;
}

template <typename ProtoType>
void decode(const Buffer& buffer, ProtoType* target) {
  decode<ProtoType>(buffer.data(), buffer.size(), target);
}

template <typename ProtoType>
ProtoType decode(const BufferRef& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer->data(), buffer->size(), &proto);
  return proto;
}

template <typename ProtoType>
void decode(const BufferRef& buffer, ProtoType* target) {
  decode<ProtoType>(buffer->data(), buffer->size(), target);
}

template <typename ProtoType>
ProtoType decode(const String& buffer) {
  ProtoType proto;
  decode<ProtoType>(buffer.data(), buffer.size(), &proto);
  return proto;
}

template <typename ProtoType>
ProtoType decode(const void* data, size_t size) {
  ProtoType proto;
  decode<ProtoType>(data, size, &proto);
  return proto;
}

template <typename ProtoType>
void decode(const void* data, size_t size, ProtoType* target) {
  if (!target->ParseFromArray(data, size)) {
    RAISE(kRuntimeError, "invalid protobuf message");
  }
}

template <typename ProtoType>
BufferRef encode(const ProtoType& proto) {
  BufferRef buf(new Buffer());
  encode<ProtoType>(proto, buf);
  return buf;
}

template <typename ProtoType>
void encode(const ProtoType& proto, BufferRef target) {
  encode<ProtoType>(proto, target.get());
}

{'fraction_non_alphanumeric': 0.11542610571736785, 'fraction_numerical': 0.002157497303128371, 'mean_word_length': 5.726723095525998, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3897649', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1690, 'n_words': 550}