1
0

fix: update clap validator concept and used functions

This commit is contained in:
2025-12-10 10:17:36 +08:00
parent 45cbdc1a2a
commit e8241e21b9
3 changed files with 16 additions and 5 deletions

View File

@ -45,10 +45,9 @@ namespace yycc::carton::clap::parser {
NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_option(NS_YYCC_CLAP_TYPES::Token token) const; NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_option(NS_YYCC_CLAP_TYPES::Token token) const;
template<NS_YYCC_CLAP_VALIDATOR::Validator T> template<NS_YYCC_CLAP_VALIDATOR::Validator T>
NS_YYCC_CLAP_TYPES::ClapResult<NS_YYCC_CLAP_VALIDATOR::ValidatorReturnType<T>> get_value_option( NS_YYCC_CLAP_TYPES::ClapResult<NS_YYCC_CLAP_VALIDATOR::ValidatorReturnType<T>> get_value_option(
NS_YYCC_CLAP_TYPES::Token token) const { NS_YYCC_CLAP_TYPES::Token token, const T& validator = T()) const {
auto raw_value = this->get_raw_value_option(token); auto raw_value = this->get_raw_value_option(token);
if (raw_value.has_value()) { if (raw_value.has_value()) {
T validator{};
auto value = validator.validate(raw_value.value()); auto value = validator.validate(raw_value.value());
if (value.has_value()) return value.value(); if (value.has_value()) return value.value();
else return std::unexpected(NS_YYCC_CLAP_TYPES::ClapError::BadCast); else return std::unexpected(NS_YYCC_CLAP_TYPES::ClapError::BadCast);

View File

@ -45,10 +45,9 @@ namespace yycc::carton::clap::resolver {
NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_variable(NS_YYCC_CLAP_TYPES::Token token) const; NS_YYCC_CLAP_TYPES::ClapResult<bool> get_flag_variable(NS_YYCC_CLAP_TYPES::Token token) const;
template<NS_YYCC_CLAP_VALIDATOR::Validator T> template<NS_YYCC_CLAP_VALIDATOR::Validator T>
NS_YYCC_CLAP_TYPES::ClapResult<NS_YYCC_CLAP_VALIDATOR::ValidatorReturnType<T>> get_value_variable( NS_YYCC_CLAP_TYPES::ClapResult<NS_YYCC_CLAP_VALIDATOR::ValidatorReturnType<T>> get_value_variable(
NS_YYCC_CLAP_TYPES::Token token) const { NS_YYCC_CLAP_TYPES::Token token, const T& validator = T()) const {
auto raw_value = this->get_raw_value_variable(token); auto raw_value = this->get_raw_value_variable(token);
if (raw_value.has_value()) { if (raw_value.has_value()) {
T validator{};
auto value = validator.validate(raw_value.value()); auto value = validator.validate(raw_value.value());
if (value.has_value()) return value.value(); if (value.has_value()) return value.value();
else return std::unexpected(NS_YYCC_CLAP_TYPES::ClapError::BadCast); else return std::unexpected(NS_YYCC_CLAP_TYPES::ClapError::BadCast);

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "../../macro/class_copy_move.hpp"
#include "../../num/parse.hpp" #include "../../num/parse.hpp"
#include <optional> #include <optional>
#include <string_view> #include <string_view>
@ -23,9 +24,11 @@ namespace yycc::carton::clap::validator {
* otherwise, it is the validated value. * otherwise, it is the validated value.
* *
* Finally, it must can be default initialized. * Finally, it must can be default initialized.
* Please note that this class may be initialized during each calling.
* Please make sure that its ctor will not consume too much resources.
*/ */
template<typename T> template<typename T>
concept Validator = std::default_initializable<T> && requires(const T& t, const std::u8string_view& sv) { concept Validator = requires(const T& t, const std::u8string_view& sv) {
// Check whether there is T::ReturnType type // Check whether there is T::ReturnType type
typename T::ReturnType; typename T::ReturnType;
// Check whether there is "validate" member function and it has correct signature. // Check whether there is "validate" member function and it has correct signature.
@ -38,7 +41,10 @@ namespace yycc::carton::clap::validator {
template<std::integral T, auto TMin = std::numeric_limits<T>::min(), auto TMax = std::numeric_limits<T>::max()> template<std::integral T, auto TMin = std::numeric_limits<T>::min(), auto TMax = std::numeric_limits<T>::max()>
struct IntegralValidator { struct IntegralValidator {
YYCC_DEFAULT_COPY_MOVE(IntegralValidator)
static_assert(TMin <= TMax); static_assert(TMin <= TMax);
using ReturnType = T; using ReturnType = T;
std::optional<ReturnType> validate(const std::u8string_view& sv) { std::optional<ReturnType> validate(const std::u8string_view& sv) {
auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv); auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv);
@ -52,9 +58,12 @@ namespace yycc::carton::clap::validator {
template<std::floating_point T, auto TMin = std::numeric_limits<T>::lowest(), auto TMax = std::numeric_limits<T>::max()> template<std::floating_point T, auto TMin = std::numeric_limits<T>::lowest(), auto TMax = std::numeric_limits<T>::max()>
struct FloatingPointValidator { struct FloatingPointValidator {
YYCC_DEFAULT_COPY_MOVE(FloatingPointValidator)
static_assert(std::isfinite(TMin)); static_assert(std::isfinite(TMin));
static_assert(std::isfinite(TMax)); static_assert(std::isfinite(TMax));
static_assert(TMin <= TMax); static_assert(TMin <= TMax);
using ReturnType = T; using ReturnType = T;
std::optional<ReturnType> validate(const std::u8string_view& sv) { std::optional<ReturnType> validate(const std::u8string_view& sv) {
auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv); auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv);
@ -67,6 +76,8 @@ namespace yycc::carton::clap::validator {
}; };
struct BoolValidator { struct BoolValidator {
YYCC_DEFAULT_COPY_MOVE(BoolValidator)
using ReturnType = bool; using ReturnType = bool;
std::optional<ReturnType> validate(const std::u8string_view& sv) { std::optional<ReturnType> validate(const std::u8string_view& sv) {
auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv); auto rv = NS_YYCC_NUM_PARSE::parse<ReturnType>(sv);
@ -76,6 +87,8 @@ namespace yycc::carton::clap::validator {
}; };
struct StringValidator { struct StringValidator {
YYCC_DEFAULT_COPY_MOVE(StringValidator)
using ReturnType = std::u8string; using ReturnType = std::u8string;
std::optional<ReturnType> validate(const std::u8string_view& sv) { return std::u8string(sv); } std::optional<ReturnType> validate(const std::u8string_view& sv) { return std::u8string(sv); }
}; };