types: Add constexpr InAddrAny / IfAddr parser
Change-Id: I08d27f3e52a703d2f3c45a886c69b081ffd69558
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/types.hpp b/src/types.hpp
index 70d9e0a..73d2778 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -369,6 +369,34 @@
}
};
+template <>
+struct ToAddr<InAddrAny>
+{
+ constexpr InAddrAny operator()(std::string_view str) const
+ {
+ if (str.find(':') == str.npos)
+ {
+ return ToAddr<in_addr>{}(str);
+ }
+ return ToAddr<in6_addr>{}(str);
+ }
+};
+
+template <>
+struct ToAddr<IfAddr>
+{
+ constexpr IfAddr operator()(std::string_view str) const
+ {
+ auto pos = str.rfind('/');
+ if (pos == str.npos)
+ {
+ throw std::invalid_argument("Invalid IfAddr");
+ }
+ return {ToAddr<InAddrAny>{}(str.substr(0, pos)),
+ DecodeInt<uint8_t, 10>{}(str.substr(pos + 1))};
+ }
+};
+
namespace detail
{