types: Add constexpr ether_addr parser

Change-Id: I0ad21490239ad9b0f25dfff84726b16a9cfda927
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index b1ada82..9795a7e 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -324,7 +324,7 @@
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipAddress"),
                               Argument::ARGUMENT_VALUE(ipAddress.c_str()));
     }
-    if (!mac_address::isUnicast(mac_address::fromString(macAddress)))
+    if (!mac_address::isUnicast(ToAddr<ether_addr>{}(macAddress)))
     {
         log<level::ERR>("Not a valid MAC address",
                         entry("MACADDRESS=%s", ipAddress.c_str()));
@@ -737,7 +737,7 @@
     std::optional<ether_addr> mac;
     if (!macStr.empty())
     {
-        mac.emplace(mac_address::fromString(macStr));
+        mac.emplace(ToAddr<ether_addr>{}(macStr));
     }
     auto info = system::InterfaceInfo{
         .idx = 0, // TODO: Query the correct value after creation
@@ -927,7 +927,7 @@
     ether_addr newMAC;
     try
     {
-        newMAC = mac_address::fromString(value);
+        newMAC = ToAddr<ether_addr>{}(value);
     }
     catch (const std::invalid_argument&)
     {
@@ -948,7 +948,7 @@
     auto validMAC = std::to_string(newMAC);
 
     // We don't need to update the system if the address is unchanged
-    ether_addr oldMAC = mac_address::fromString(MacAddressIntf::macAddress());
+    ether_addr oldMAC = ToAddr<ether_addr>{}(MacAddressIntf::macAddress());
     if (newMAC != oldMAC)
     {
         // Update everything that depends on the MAC value
diff --git a/src/types.hpp b/src/types.hpp
index 2bd895a..3c0b6d9 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -249,6 +249,43 @@
     }
 };
 
+template <typename T>
+struct ToAddr
+{
+};
+
+template <>
+struct ToAddr<ether_addr>
+{
+    constexpr ether_addr operator()(std::string_view str) const
+    {
+        constexpr DecodeInt<uint8_t, 16> di;
+        ether_addr ret;
+        if (str.size() == 12 && str.find(":") == str.npos)
+        {
+            for (size_t i = 0; i < 6; ++i)
+            {
+                ret.ether_addr_octet[i] = di(str.substr(i * 2, 2));
+            }
+        }
+        else
+        {
+            for (size_t i = 0; i < 5; ++i)
+            {
+                auto loc = str.find(":");
+                ret.ether_addr_octet[i] = di(str.substr(0, loc));
+                str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
+                if (str.empty())
+                {
+                    throw std::invalid_argument("Missing mac data");
+                }
+            }
+            ret.ether_addr_octet[5] = di(str);
+        }
+        return ret;
+    }
+};
+
 namespace detail
 {
 
diff --git a/src/util.cpp b/src/util.cpp
index e65c17d..23ab4bf 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -415,45 +415,7 @@
 
     std::variant<std::string> value;
     reply.read(value);
-    return fromString(std::get<std::string>(value));
-}
-
-static uint8_t decodeHex(std::string_view str)
-{
-    uint8_t ret;
-    auto res = std::from_chars(str.begin(), str.end(), ret, 16);
-    if (res.ptr != str.end() || res.ec != std::errc())
-    {
-        throw std::invalid_argument("Not hex");
-    }
-    return ret;
-}
-
-ether_addr fromString(std::string_view str)
-{
-    ether_addr ret;
-    if (str.size() == 12 && str.find(":") == str.npos)
-    {
-        for (size_t i = 0; i < 6; ++i)
-        {
-            ret.ether_addr_octet[i] = decodeHex(str.substr(i * 2, 2));
-        }
-    }
-    else
-    {
-        for (size_t i = 0; i < 5; ++i)
-        {
-            auto loc = str.find(":");
-            ret.ether_addr_octet[i] = decodeHex(str.substr(0, loc));
-            str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
-            if (str.empty())
-            {
-                throw std::invalid_argument("Missing mac data");
-            }
-        }
-        ret.ether_addr_octet[5] = decodeHex(str);
-    }
-    return ret;
+    return ToAddr<ether_addr>{}(std::get<std::string>(value));
 }
 
 bool isEmpty(const ether_addr& mac)
diff --git a/src/util.hpp b/src/util.hpp
index 19eb108..ea9d767 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -36,13 +36,6 @@
  */
 ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
 
-/** @brief Converts the given mac address into byte form
- *  @param[in] str - The mac address in human readable form
- *  @returns A mac address in network byte order
- *  @throws std::runtime_error for bad mac
- */
-ether_addr fromString(std::string_view str);
-
 /** @brief Determines if the mac address is empty
  *  @param[in] mac - The mac address
  *  @return True if 00:00:00:00:00:00