Updated to new esp-idf
This commit is contained in:
@ -24,7 +24,6 @@ set(sources
|
|||||||
)
|
)
|
||||||
|
|
||||||
set(dependencies
|
set(dependencies
|
||||||
expected
|
|
||||||
fmt
|
fmt
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ std::string toString(ColorHelper color)
|
|||||||
return fmt::format("#{:02X}{:02X}{:02X}", color.r, color.g, color.b);
|
return fmt::format("#{:02X}{:02X}{:02X}", color.r, color.g, color.b);
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<ColorHelper, std::string> parseColor(std::string_view str)
|
std::expected<ColorHelper, std::string> parseColor(std::string_view str)
|
||||||
{
|
{
|
||||||
// input may be "#FFF" or "#FFFFFF" or "#FFFFFFFF"
|
// input may be "#FFF" or "#FFFFFF" or "#FFFFFFFF"
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ tl::expected<ColorHelper, std::string> parseColor(std::string_view str)
|
|||||||
return helper;
|
return helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
return tl::make_unexpected(fmt::format("invalid color {}", str));
|
return std::unexpected(fmt::format("invalid color {}", str));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace cpputils
|
} // namespace cpputils
|
||||||
|
@ -4,9 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <expected>
|
||||||
// 3rdparty lib includes
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
struct ColorHelper
|
struct ColorHelper
|
||||||
@ -53,7 +51,7 @@ struct HsvColor
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::string toString(ColorHelper color);
|
std::string toString(ColorHelper color);
|
||||||
tl::expected<ColorHelper, std::string> parseColor(std::string_view str);
|
std::expected<ColorHelper, std::string> parseColor(std::string_view str);
|
||||||
|
|
||||||
inline uint32_t colorToNumber(ColorHelper color)
|
inline uint32_t colorToNumber(ColorHelper color)
|
||||||
{
|
{
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
// system includes
|
// system includes
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
// 3rdparty lib includes
|
// 3rdparty lib includes
|
||||||
#include <tl/expected.hpp>
|
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
@ -32,12 +32,12 @@
|
|||||||
} \
|
} \
|
||||||
return fmt::format("Unknown " #Name "({})", int(value)); \
|
return fmt::format("Unknown " #Name "({})", int(value)); \
|
||||||
} \
|
} \
|
||||||
inline tl::expected<Name, std::string> parse##Name(std::string_view str) \
|
inline std::expected<Name, std::string> parse##Name(std::string_view str) \
|
||||||
{ \
|
{ \
|
||||||
using TheEnum = Name; \
|
using TheEnum = Name; \
|
||||||
if (false) {} \
|
if (false) {} \
|
||||||
Values(DECLARE_TYPESAFE_ENUM_HELPER3) \
|
Values(DECLARE_TYPESAFE_ENUM_HELPER3) \
|
||||||
return tl::make_unexpected(fmt::format("invalid " #Name " ({})", str)); \
|
return std::unexpected(fmt::format("invalid " #Name " ({})", str)); \
|
||||||
} \
|
} \
|
||||||
template<typename T> \
|
template<typename T> \
|
||||||
void iterate##Name(T &&cb) \
|
void iterate##Name(T &&cb) \
|
||||||
|
@ -5,93 +5,93 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
// 3rdparty lib includes
|
// 3rdparty lib includes
|
||||||
#include <tl/expected.hpp>
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
template<typename T> tl::expected<T, std::string> fromString(std::string_view str) = delete;
|
template<typename T> std::expected<T, std::string> fromString(std::string_view str) = delete;
|
||||||
|
|
||||||
template<> inline tl::expected<int8_t, std::string> fromString<int8_t>(std::string_view str)
|
template<> inline std::expected<int8_t, std::string> fromString<int8_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
int8_t val;
|
int8_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNi8, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNi8, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid int8_t {}", str));
|
return std::unexpected(fmt::format("invalid int8_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<uint8_t, std::string> fromString<uint8_t>(std::string_view str)
|
template<> inline std::expected<uint8_t, std::string> fromString<uint8_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
uint8_t val;
|
uint8_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNu8, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNu8, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid uint8_t {}", str));
|
return std::unexpected(fmt::format("invalid uint8_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<int16_t, std::string> fromString<int16_t>(std::string_view str)
|
template<> inline std::expected<int16_t, std::string> fromString<int16_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
int16_t val;
|
int16_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNi16, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNi16, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid int16_t {}", str));
|
return std::unexpected(fmt::format("invalid int16_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<uint16_t, std::string> fromString<uint16_t>(std::string_view str)
|
template<> inline std::expected<uint16_t, std::string> fromString<uint16_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
uint16_t val;
|
uint16_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNu16, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNu16, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid uint16_t {}", str));
|
return std::unexpected(fmt::format("invalid uint16_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<int32_t, std::string> fromString<int32_t>(std::string_view str)
|
template<> inline std::expected<int32_t, std::string> fromString<int32_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
int32_t val;
|
int32_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNi32, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNi32, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid int32_t {}", str));
|
return std::unexpected(fmt::format("invalid int32_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<uint32_t, std::string> fromString<uint32_t>(std::string_view str)
|
template<> inline std::expected<uint32_t, std::string> fromString<uint32_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
uint32_t val;
|
uint32_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNu32, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNu32, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid uint32_t {}", str));
|
return std::unexpected(fmt::format("invalid uint32_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
template<> inline tl::expected<int, std::string> fromString<int>(std::string_view str)
|
template<> inline std::expected<int, std::string> fromString<int>(std::string_view str)
|
||||||
{
|
{
|
||||||
int val;
|
int val;
|
||||||
if (std::sscanf(str.data(), "%i", &val) != 1)
|
if (std::sscanf(str.data(), "%i", &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid int {}", str));
|
return std::unexpected(fmt::format("invalid int {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<unsigned int, std::string> fromString<unsigned int>(std::string_view str)
|
template<> inline std::expected<unsigned int, std::string> fromString<unsigned int>(std::string_view str)
|
||||||
{
|
{
|
||||||
unsigned int val;
|
unsigned int val;
|
||||||
if (std::sscanf(str.data(), "%u", &val) != 1)
|
if (std::sscanf(str.data(), "%u", &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid unsigned int {}", str));
|
return std::unexpected(fmt::format("invalid unsigned int {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<> inline tl::expected<int64_t, std::string> fromString<int64_t>(std::string_view str)
|
template<> inline std::expected<int64_t, std::string> fromString<int64_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
int64_t val;
|
int64_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNi64, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNi64, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid int64_t {}", str));
|
return std::unexpected(fmt::format("invalid int64_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline tl::expected<uint64_t, std::string> fromString<uint64_t>(std::string_view str)
|
template<> inline std::expected<uint64_t, std::string> fromString<uint64_t>(std::string_view str)
|
||||||
{
|
{
|
||||||
uint64_t val;
|
uint64_t val;
|
||||||
if (std::sscanf(str.data(), "%" SCNu64, &val) != 1)
|
if (std::sscanf(str.data(), "%" SCNu64, &val) != 1)
|
||||||
return tl::make_unexpected(fmt::format("invalid uint64_t {}", str));
|
return std::unexpected(fmt::format("invalid uint64_t {}", str));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
} // namespace cpputils
|
} // namespace cpputils
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
namespace {
|
namespace {
|
||||||
tl::expected<char *, std::string> sodium_bin2hex(char * const hex, const size_t hex_maxlen,
|
std::expected<char *, std::string> sodium_bin2hex(char * const hex, const size_t hex_maxlen,
|
||||||
const unsigned char * const bin, const size_t bin_len)
|
const unsigned char * const bin, const size_t bin_len)
|
||||||
__attribute__ ((nonnull(1)));
|
__attribute__ ((nonnull(1)));
|
||||||
} // namespace
|
} // namespace
|
||||||
@ -32,10 +32,10 @@ std::string toHexString(std::basic_string_view<unsigned char> buf)
|
|||||||
return hex;
|
return hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex)
|
std::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex)
|
||||||
{
|
{
|
||||||
if (hex.size() % 2 != 0)
|
if (hex.size() % 2 != 0)
|
||||||
return tl::make_unexpected("hex length not even");
|
return std::unexpected("hex length not even");
|
||||||
|
|
||||||
std::basic_string<unsigned char> result;
|
std::basic_string<unsigned char> result;
|
||||||
result.reserve(hex.size() / 2);
|
result.reserve(hex.size() / 2);
|
||||||
@ -56,7 +56,7 @@ tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::s
|
|||||||
case 'A'...'F': nibbles.nibble0 = c - 'A' + 10; break;
|
case 'A'...'F': nibbles.nibble0 = c - 'A' + 10; break;
|
||||||
case 'a'...'f': nibbles.nibble0 = c - 'a' + 10; break;
|
case 'a'...'f': nibbles.nibble0 = c - 'a' + 10; break;
|
||||||
default:
|
default:
|
||||||
return tl::make_unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
|
return std::unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
iter++;
|
iter++;
|
||||||
@ -67,7 +67,7 @@ tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::s
|
|||||||
case 'A'...'F': nibbles.nibble1 = c - 'A' + 10; break;
|
case 'A'...'F': nibbles.nibble1 = c - 'A' + 10; break;
|
||||||
case 'a'...'f': nibbles.nibble1 = c - 'a' + 10; break;
|
case 'a'...'f': nibbles.nibble1 = c - 'a' + 10; break;
|
||||||
default:
|
default:
|
||||||
return tl::make_unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
|
return std::unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
iter++;
|
iter++;
|
||||||
@ -275,7 +275,7 @@ std::optional<std::string_view> getStringBetween(std::string_view search, std::s
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
/* Derived from original code by CodesInChaos */
|
/* Derived from original code by CodesInChaos */
|
||||||
tl::expected<char *, std::string>
|
std::expected<char *, std::string>
|
||||||
sodium_bin2hex(char *const hex, const size_t hex_maxlen,
|
sodium_bin2hex(char *const hex, const size_t hex_maxlen,
|
||||||
const unsigned char *const bin, const size_t bin_len)
|
const unsigned char *const bin, const size_t bin_len)
|
||||||
{
|
{
|
||||||
@ -285,7 +285,7 @@ sodium_bin2hex(char *const hex, const size_t hex_maxlen,
|
|||||||
int c;
|
int c;
|
||||||
|
|
||||||
if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
|
if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
|
||||||
return tl::make_unexpected("misuse because bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U");
|
return std::unexpected("misuse because bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U");
|
||||||
}
|
}
|
||||||
while (i < bin_len) {
|
while (i < bin_len) {
|
||||||
c = bin[i] & 0xf;
|
c = bin[i] & 0xf;
|
||||||
|
@ -6,9 +6,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <expected>
|
||||||
// 3rdparty lib includes
|
|
||||||
#include <tl/expected.hpp>
|
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
inline std::string toString(bool val) { return val ? "true" : "false"; }
|
inline std::string toString(bool val) { return val ? "true" : "false"; }
|
||||||
@ -46,7 +44,7 @@ inline std::string toHexString(std::string_view str)
|
|||||||
return toHexString(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
|
return toHexString(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
|
||||||
}
|
}
|
||||||
|
|
||||||
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
|
std::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
|
||||||
|
|
||||||
std::string toBase64String(std::basic_string_view<unsigned char> buf);
|
std::string toBase64String(std::basic_string_view<unsigned char> buf);
|
||||||
inline std::string toBase64String(std::string_view str)
|
inline std::string toBase64String(std::string_view str)
|
||||||
|
Reference in New Issue
Block a user