summaryrefslogtreecommitdiff
path: root/modules/codec/c++/args.hpp
blob: 0253417a05e543f4a0ce3a93bca378dd287cd620 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma once

#include "schema.hpp"
#include "data.hpp"

#include <algorithm>
#include <charconv>

namespace saw {
namespace encode {
struct Args {};
}

namespace schema {
template<typename InnerSchemaStruct, typename InnerSchemaTuple>
using Args = Struct<
	Member<String, "program">,
	Member<InnerSchemaStruct, "args">,
	Member<InnerSchemaTuple, "positionals">
>;
}

template<typename T>
class data<T, encode::Args> {
	static_assert(always_false<T>,"Not supported. Use the schema::Args alias or check how it's defined.");
};

template<typename... Str, string_literal... Lits, typename... Tup>
class data<schema::Args<schema::Struct<schema::Member<Str,Lits>...>, schema::Tuple<Tup...>>, encode::Args> {
private:
	int argc_;
	char** argv_;
public:
	data(int argc, char** argv):
		argc_{argc},
		argv_{argv}
	{}

	uint64_t size() const {
		if(argc_ < 0){
			return 0u;
		}

		static_assert(sizeof(int) <= sizeof(uint64_t), "size_t is smaller than int");

		return static_cast<uint64_t>(argc_);
	}

	std::string_view arg_view(uint64_t i){
		if(i < size()){
			return std::string_view{argv_[i]};
		}
		return "";
	}
};

namespace impl {
template<typename T, typename ToDec>
struct args_decode {
	static_assert(always_false<T,ToDec>, "Not supported");
};

template<typename T, uint64_t N, typename ToDec>
struct args_decode<schema::Primitive<T,N>, ToDec> {
	using Schema = schema::Primitive<T,N>;

	static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){
		if(view.empty()){
			return make_error<err::invalid_state>("Can't decode empty data.");
		}
		typename native_data_type<Schema>::type val{};
		auto fc_res = std::from_chars(view.data(), view.data()+view.size(), val);
		if(fc_res.ec != std::errc{}){
			auto err_bld = error_builder{[&](){
				return std::string{"Parsing of failed: "} + std::string{view};
			}};
			return err_bld.template make_error<err::invalid_state>("Parsing failed. ");
		}
		to.set(val);

		return make_void();
	}
};

template<typename ToDec>
struct args_decode<schema::String, ToDec> {
	using Schema = schema::String;

	static error_or<void> decode(data<Schema, ToDec>& to, std::string_view view){
		try{
			to.set(std::string{view});
		}catch(const std::exception&){
			return make_error<err::out_of_memory>();
		}
		return make_void();
	}
};

template<typename... Str, string_literal... Lits, typename... Tup, typename ToDec>
struct args_decode<schema::Args<schema::Struct<schema::Member<Str,Lits>...>,schema::Tuple<Tup...>>,ToDec> {
	using Schema = schema::Args<
		schema::Struct<schema::Member<Str,Lits>...>,
		schema::Tuple<Tup...>
	>;

	using SchemaStruct = schema::Struct<schema::Member<Str,Lits>...>;
	using SchemaTuple = schema::Tuple<Tup...>;

	template<uint64_t i>
	static error_or<void> decode_struct_member(data<SchemaStruct, ToDec>& to, std::string_view name, std::string_view value){
		if constexpr ( i < sizeof...(Str) ) {
			static constexpr string_literal Literal = parameter_key_pack_type<i,Lits...>::literal;
			using StrT = typename parameter_pack_type<i,Str...>::type;

			if(Literal.view() == name){
				auto& target = to.template get<Literal>();
				auto eov = args_decode<StrT, ToDec>::decode(target, value);
				return eov;
			}

			return decode_struct_member<i+1u>(to, name, value);
		}
		return make_error<err::not_found>("Couldn't decode argument");
	}
	
	template<uint64_t i>
	static error_or<void> decode_tuple_member(data<SchemaTuple, ToDec>& to, uint64_t ptr_i, std::string_view value){
		if constexpr ( i < sizeof...(Tup) ){
			if ( i == ptr_i ){
				using TupT = typename parameter_pack_type<i,Tup...>::type;
				auto& target = to.template get<i>();
				
				return args_decode<TupT, ToDec>::decode(target, value);
			}
			return decode_tuple_member<i+1u>(to, ptr_i, value);
		}
		return make_error<err::invalid_state>("Too many positional arguments.");
	}

	static error_or<void> decode(data<Schema, encode::Args>& from, data<Schema,ToDec>& to){
		if(from.size() == 0){
			return make_error<err::invalid_state>();
		}

		to.template get<"program">().set(std::string{from.arg_view(0)});
		uint64_t tuple_pos = 0;

		for(size_t i = 1; i < from.size(); ++i){
			auto view = from.arg_view(i);
			if(view.starts_with("--")){
				auto min_val = view.size() > 2u ? 2u : view.size();
				view.remove_prefix(min_val);
				++i;

				if( i >= from.size() ){
					return make_error<err::invalid_state>();
				}

				auto value_view = from.arg_view(i);

				auto eov = decode_struct_member<0>(to.template get<"args">(), view, value_view);
				if(eov.is_error()){
					return eov;
				}
			} else {
				auto eov = decode_tuple_member<0>(to.template get<"positionals">(), tuple_pos, view);
				if(eov.is_error()){
					return eov;
				}
				++tuple_pos;
			}
		}

		if(tuple_pos != sizeof...(Tup)){
			return make_error<err::invalid_state>("Didn't parse the whole set of positionals.");
		}

		return void_t{};
	}
};
}

template<typename Schema>
class codec<Schema, encode::Args> {
public:
	template<typename ToDec>
	error_or<void> decode(data<Schema, encode::Args>& from, data<Schema, ToDec>& to){
		/*
		struct name_and_value {
			std::string name;
			std::string value;
		};
		std::string program;
		std::vector<name_and_value> navs;
		std::vector<std::string> positionals;
		*/

		auto eov = impl::args_decode<Schema,ToDec>::decode(from,to);
		return eov;
	}
};
}