summaryrefslogtreecommitdiff
path: root/modules/codec/c++/csv.h
blob: 37278297dfcae075f69c882a12bca7fd5fadb76e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

#include <forstio/error.h>

#include "data.h"

namespace saw {
namespace encode {
struct Csv {};
}

namespace impl {
template<typename Schema, typename FromDecode>
struct csv_encode {
	static_assert(always_false<T>, "Case not supported");
};

template<typename  T, size_t Dim>
struct csv_encode<schema::Array<T,Dim>, FromDecode> {
	static_assert(Dim == 1, "Only one dimension is allowed.");
	static_assert(!is_array<T>::value, "Array of an array is not allowed.");
	static_assert(is_tuple<T>::value || is_struct<T>::value, "Only struct or tuples allowed inside a csv array");

	using Schema = schema::Array<T,Dim>;

	static error_or<void> encode(const data<Schema, FromDecode>& from, data<Schema, encode::Csv>& to){
		if constexpr (is_struct<T>::value){
			auto eov = csv_encode<T>::encode_header(to);
			if(eov.is_error()){
				return eov;
			}
		}

		for(std::size_t i = 0; i < from.size(); ++i){
			auto eov = csv_encode<T>::encode(from.at(i), to);
			if(eov.is_error()){
				return eov;
			}
		}
		
		return void_t{};
	}
};

template<>
struct csv_encode<schema::String, FromDecode> {
	using Schema = schema::String;

	static error_or<void> encode(const data<Schema, FromDecode>& from, data<Schema, encode::Csv>& to){

		return void_t{};
	}
};
}

template<typename Schema>
class codec<Schema, encode::Csv> {
	static_assert(is_array<Schema>::value, "Only an Array is allowed as a base value");
public:
	template<typename FromEncode>
	static error_or<void> encode(const data<Schema, FromEncode>& from, data<Schema,encode::Csv>& to){

		return make_error<err::not_implemented>();
	}
	
	template<typename ToDecode>
	static error_or<void> decode(data<Schema,encode::Csv>& from, data<Schema, FromEncode>& to){

		return make_error<err::not_implemented>();
	}
};
}