Euphoria
default_parse.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "base/enumtostring.h"
4 #include "base/string_io.h"
5 #include "base/numparse.h"
6 
7 
8 namespace eu
9 {
10  std::string
11  add_quotes_and_combine_with_english_or(const std::vector<std::string>& matches);
12 
13 
14  template
15  <
16  typename T,
17  std::enable_if_t<StringParser<T>::value != 0, int> = 0
18  >
19  Result<T>
21  (
22  const std::string& value
23  )
24  {
25  auto r = parse_for_custom_argparser<T>(value);
26  return r;
27  }
28 
29  template
30  <
31  typename T,
32  std::enable_if_t
33  <
34  std::is_same<T, std::string>::value
35  , int> = 0
36  >
37  Result<T>
39  (
40  const std::string& value
41  )
42  {
43  return Result<T>::create_value(value);
44  }
45 
46  // default parse function for non-enums
47  template
48  <
49  typename T,
50  std::enable_if_t
51  <
52  std::is_enum<T>::value == false && StringParser<T>::value == 0 && std::is_same<T, std::string>::value == false,
53  int
54  > = 0
55  >
56  Result<T>
58  (
59  const std::string& value
60  )
61  {
62  auto parsed = locale_parse_generic<T>(value);
63  if(parsed)
64  {
65  return Result<T>::create_value(*parsed);
66  }
67  else
68  {
69  return Result<T>::create_error("");
70  }
71  }
72 
73  // default parse function for enums
74  template
75  <
76  typename T,
77  std::enable_if_t
78  <
79  std::is_enum<T>::value == true && StringParser<T>::value == 0,
80  int
81  > = 0
82  >
83  Result<T>
85  (
86  const std::string& value
87  )
88  {
89  auto matches = from_string_to_enum<T>(value);
90  if (matches.single_match)
91  {
92  return Result<T>::create_value(matches.values[0]);
93  }
94  else
95  {
96  return Result<T>::create_error(fmt::format("did you mean {}?", add_quotes_and_combine_with_english_or(matches.names)));
97  }
98  }
99 
100  // default describe function for non-enums
101  template
102  <
103  typename T,
104  std::enable_if_t<std::is_enum<T>::value == false, int> = 0
105  >
106  std::optional<std::string>
108  {
109  return std::nullopt;
110  }
111 
112  // default describe function for enums
113  template
114  <
115  typename T,
116  std::enable_if_t<std::is_enum<T>::value == true, int> = 0
117  >
118  std::optional<std::string>
120  {
121  const std::string r = fmt::format("can be either {}",
123  (
124  get_all_names_from_enum<T>()
125  )
126  );
127 
128  return r;
129  }
130 
131  template
132  <
133  typename T,
134  std::enable_if_t<StringParser<T>::value != 0, int> = 0
135  >
136  std::string
138  {
140  }
141 
142 
143  // default value for non enums
144  template
145  <
146  typename T,
147  std::enable_if_t
148  <
149  std::is_enum<T>::value == false && StringParser<T>::value == 0,
150  int
151  > = 0
152  >
153  std::string
154  from_default_value_to_string(const T& t)
155  {
156  return fmt::format("{}", t);
157  }
158 
159  // default value for enums
160  template
161  <
162  typename T,
163  std::enable_if_t
164  <
165  std::is_enum<T>::value == true && StringParser<T>::value == 0,
166  int
167  > = 0
168  >
169  std::string
170  from_default_value_to_string(const T& t)
171  {
172  return from_enum_to_string(t);
173  }
174 }
Definition: assert.h:90
std::string from_enum_to_string(T t)
Definition: enumtostring.h:116
std::string add_quotes_and_combine_with_english_or(const std::vector< std::string > &matches)
Result< T > default_parse_function(const std::string &value)
Definition: default_parse.h:21
std::string from_default_value_to_string(const T &t)
std::optional< std::string > default_describe()
std::string to_string_for_custom_argparser(const T &t)
Definition: string_io.h:23
static Self create_error(const Error &error)
Definition: result.h:21
static Self create_value(const T &t)
Definition: result.h:15