// This file Copyright © 2022-2023 Mnemosyne LLC. // It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only), // or any future license endorsed by Mnemosyne LLC. // License text can be found in the licenses/ folder. #include #include // size_t #include // int64_t, uint32_t #include #include #include #include #include #include "libtransmission/transmission.h" #include "libtransmission/log.h" // for tr_log_level #include "libtransmission/net.h" // for tr_port #include "libtransmission/utils.h" // for tr_strv_strip(), tr_strlower() #include "libtransmission/variant.h" using namespace std::literals; namespace { auto constexpr EncryptionKeys = std::array, 3>{ { { "required", TR_ENCRYPTION_REQUIRED }, { "preferred", TR_ENCRYPTION_PREFERRED }, { "allowed", TR_CLEAR_PREFERRED }, } }; auto constexpr LogKeys = std::array, 7>{ { { "critical", TR_LOG_CRITICAL }, { "debug", TR_LOG_DEBUG }, { "error", TR_LOG_ERROR }, { "info", TR_LOG_INFO }, { "off", TR_LOG_OFF }, { "trace", TR_LOG_TRACE }, { "warn", TR_LOG_WARN }, } }; auto constexpr PreallocationKeys = std::array, 5>{ { { "off", TR_PREALLOCATE_NONE }, { "none", TR_PREALLOCATE_NONE }, { "fast", TR_PREALLOCATE_SPARSE }, { "sparse", TR_PREALLOCATE_SPARSE }, { "full", TR_PREALLOCATE_FULL }, } }; auto constexpr VerifyModeKeys = std::array, 2>{ { { "fast", TR_VERIFY_ADDED_FAST }, { "full", TR_VERIFY_ADDED_FULL }, } }; } // namespace namespace libtransmission { template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto val = src.get_if(); val != nullptr) { return *val; } return {}; } template<> tr_variant VariantConverter::save(bool const& val) { return val; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto val = src.get_if(); val != nullptr) { return *val; } return {}; } template<> tr_variant VariantConverter::save(double const& val) { return val; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { static constexpr auto Keys = EncryptionKeys; if (auto const* val = src.get_if(); val != nullptr) { auto const needle = tr_strlower(tr_strv_strip(*val)); for (auto const& [key, encryption] : Keys) { if (key == needle) { return encryption; } } } if (auto const* val = src.get_if(); val != nullptr) { for (auto const& [key, encryption] : Keys) { if (encryption == *val) { return encryption; } } } return {}; } template<> tr_variant VariantConverter::save(tr_encryption_mode const& val) { return static_cast(val); } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { static constexpr auto Keys = LogKeys; if (auto const* val = src.get_if(); val != nullptr) { auto const needle = tr_strlower(tr_strv_strip(*val)); for (auto const& [name, log_level] : Keys) { if (needle == name) { return log_level; } } } if (auto const* val = src.get_if(); val != nullptr) { for (auto const& [name, log_level] : Keys) { if (log_level == *val) { return log_level; } } } return {}; } template<> tr_variant VariantConverter::save(tr_log_level const& val) { return static_cast(val); } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto const* val = src.get_if(); val != nullptr) { if (auto const mode = tr_num_parse(*val, nullptr, 8); mode) { return static_cast(*mode); } } if (auto const* val = src.get_if(); val != nullptr) { return static_cast(*val); } return {}; } template<> tr_variant VariantConverter::save(tr_mode_t const& val) { return fmt::format("{:#03o}", val); } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto const* val = src.get_if(); val != nullptr) { return tr_port::from_host(*val); } return {}; } template<> tr_variant VariantConverter::save(tr_port const& val) { return int64_t{ val.host() }; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { static constexpr auto Keys = PreallocationKeys; if (auto const* val = src.get_if(); val != nullptr) { auto const needle = tr_strlower(tr_strv_strip(*val)); for (auto const& [name, value] : Keys) { if (name == needle) { return value; } } } if (auto const* val = src.get_if(); val != nullptr) { for (auto const& [name, value] : Keys) { if (value == *val) { return value; } } } return {}; } template<> tr_variant VariantConverter::save(tr_preallocation_mode const& val) { return int64_t{ val }; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto const* val = src.get_if(); val != nullptr) { return static_cast(*val); } return {}; } template<> tr_variant VariantConverter::save(size_t const& val) { return uint64_t{ val }; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto const* val = src.get_if(); val != nullptr) { return std::string{ *val }; } return {}; } template<> tr_variant VariantConverter::save(std::string const& val) { return val; } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { if (auto const* val = src.get_if(); val != nullptr) { return tr_tos_t::from_string(*val); } if (auto const* val = src.get_if(); val != nullptr) { return tr_tos_t{ static_cast(*val) }; } return {}; } template<> tr_variant VariantConverter::save(tr_tos_t const& val) { return val.toString(); } // --- template<> std::optional VariantConverter::load(tr_variant const& src) { static constexpr auto& Keys = VerifyModeKeys; if (auto const* val = src.get_if(); val != nullptr) { auto const needle = tr_strlower(tr_strv_strip(*val)); for (auto const& [name, value] : Keys) { if (name == needle) { return value; } } } if (auto const* val = src.get_if(); val != nullptr) { for (auto const& [name, value] : Keys) { if (value == *val) { return value; } } } return {}; } template<> tr_variant VariantConverter::save(tr_verify_added_mode const& val) { for (auto const& [key, value] : VerifyModeKeys) { if (value == val) { return key; } } return static_cast(val); } } // namespace libtransmission