#pragma once

#include <charconv>
#include <cmath>
#include <optional>
#include <string_view>

namespace ta::application::config::parse
{

    inline std::optional<bool> boolean(const std::string_view text) noexcept
    {
        if (text == "true" || text == "1")
            return true;
        if (text == "false" || text == "0")
            return false;
        return std::nullopt;
    }
    inline std::optional<int> integer(const std::string_view text) noexcept
    {
        int value = 0;
        const char* begin = text.data();
        const char* end = begin + text.size();
        const auto result = std::from_chars(begin, end, value);
        if (result.ec != std::errc{} || result.ptr != end)
            return std::nullopt;
        return value;
    }

    inline std::optional<float> floating(const std::string_view text) noexcept
    {
        float value = 0.0f;
        const char* begin = text.data();
        const char* end = begin + text.size();
        const auto result = std::from_chars(begin, end, value);
        if (result.ec != std::errc{} || result.ptr != end || !std::isfinite(value))
            return std::nullopt;
        return value;
    }
}
