refactor: finish rust parse and add testbench for it.
This commit is contained in:
@ -51,6 +51,7 @@ FILES
|
||||
yycc/rust/option.hpp
|
||||
yycc/rust/result.hpp
|
||||
yycc/rust/parse.hpp
|
||||
yycc/rust/stringify.hpp
|
||||
yycc/windows/unsafe_suppressor.hpp
|
||||
yycc/windows/import_guard_head.hpp
|
||||
yycc/windows/import_guard_tail.hpp
|
||||
|
@ -5,6 +5,9 @@
|
||||
|
||||
// Rust prelude section
|
||||
#include "../rust/primitive.hpp"
|
||||
#include "../rust/result.hpp"
|
||||
#include "../rust/option.hpp"
|
||||
#include "../rust/panic.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace yycc::prelude::rust {
|
||||
@ -35,6 +38,14 @@ namespace yycc::prelude::rust {
|
||||
using String = ::yycc::string::u8string;
|
||||
template<typename T>
|
||||
using Vec = std::vector<T>;
|
||||
|
||||
// Expose Result and Option
|
||||
using namespace ::yycc::rust::option;
|
||||
using namespace ::yycc::rust::result;
|
||||
|
||||
// Panic are introduced by including header file
|
||||
// so we do not need re-expose it.
|
||||
|
||||
} // namespace yycc::prelude::rust
|
||||
|
||||
// Expose all members
|
||||
|
@ -11,7 +11,7 @@ namespace yycc::rust::option {
|
||||
using Option = std::optional<T>;
|
||||
|
||||
template<typename OptionType, typename... Args>
|
||||
OptionType Some(Args &&... args) {
|
||||
OptionType Some(Args &&...args) {
|
||||
return OptionType(std::in_place, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
@ -20,4 +20,4 @@ namespace yycc::rust::option {
|
||||
return OptionType(std::nullopt);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace yycc::rust::option
|
||||
|
@ -4,21 +4,96 @@
|
||||
#include "panic.hpp"
|
||||
#include "result.hpp"
|
||||
|
||||
#define NS_YYCC_STRING ::yycc::string
|
||||
#define NS_YYCC_STRING_PARSE ::yycc::string::parse
|
||||
#define NS_YYCC_RUST_RESULT ::yycc::rust::result
|
||||
|
||||
/**
|
||||
* @namespace yycc::rust::parse
|
||||
* @brief Provides Rust-inspired parsing utilities for converting strings to various types.
|
||||
* @details
|
||||
* This namespace contains template functions for parsing strings into different types
|
||||
* (floating-point, integral, boolean) with Rust-like Result error handling.
|
||||
*/
|
||||
namespace yycc::rust::parse {
|
||||
|
||||
#if defined(YYCC_CPPFEAT_EXPECTED)
|
||||
|
||||
/// @brief The error type of parsing.
|
||||
using Error = NS_YYCC_STRING_PARSE::ParseError;
|
||||
|
||||
// template<typename T>
|
||||
// using Result = std::expected<T, Error>;
|
||||
/// @brief The result type of parsing.
|
||||
/// @tparam T The expected value type in result.
|
||||
template<typename T>
|
||||
using Result = NS_YYCC_RUST_RESULT::Result<T, Error>;
|
||||
|
||||
/**
|
||||
* @brief Parses a string into a floating-point value.
|
||||
* @tparam T Floating-point type to parse into (float, double, etc.)
|
||||
* @param strl String view to parse
|
||||
* @param fmt Formatting flags for parsing (default: general)
|
||||
* @return Result<T> containing either the parsed value or an error
|
||||
*/
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
|
||||
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl,
|
||||
std::chars_format fmt = std::chars_format::general) {
|
||||
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl, fmt);
|
||||
|
||||
if (const auto* ptr = std::get_if<T>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
||||
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
||||
} else {
|
||||
// Unreachable
|
||||
RS_PANIC("unreachable code.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parses a string into an integral value (excluding bool).
|
||||
* @tparam T Integral type to parse into (int, long, etc.)
|
||||
* @param strl String view to parse
|
||||
* @param base Numeric base for parsing (default: 10)
|
||||
* @return Result<T> containing either the parsed value or an error
|
||||
*/
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, int> = 0>
|
||||
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl, int base = 10) {
|
||||
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl, base);
|
||||
|
||||
if (const auto* ptr = std::get_if<T>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
||||
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
||||
} else {
|
||||
// Unreachable
|
||||
RS_PANIC("unreachable code.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parses a string into a boolean value.
|
||||
* @tparam T Must be bool type
|
||||
* @param strl String view to parse
|
||||
* @return Result<bool> containing either the parsed value or an error
|
||||
*/
|
||||
template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int> = 0>
|
||||
Result<T> parse(const NS_YYCC_STRING::u8string_view& strl) {
|
||||
auto rv = NS_YYCC_STRING_PARSE::priv_parse<T>(strl);
|
||||
|
||||
if (const auto* ptr = std::get_if<T>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Ok<Result<T>>(*ptr);
|
||||
} else if (const auto* ptr = std::get_if<Error>(&rv)) {
|
||||
return NS_YYCC_RUST_RESULT::Err<Result<T>>(*ptr);
|
||||
} else {
|
||||
// Unreachable
|
||||
RS_PANIC("unreachable code.");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#undef NS_YYCC_RUST_RESULT
|
||||
#undef NS_YYCC_STRING_PARSE
|
||||
#undef NS_YYCC_STRING
|
||||
|
@ -82,4 +82,4 @@ namespace yycc::rust::result {
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
} // namespace yycc::rust::result
|
||||
|
10
src/yycc/rust/stringify.hpp
Normal file
10
src/yycc/rust/stringify.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "../string/stringify.hpp"
|
||||
|
||||
namespace yycc::rust::stringify {
|
||||
|
||||
// There is no modification for legacy "stringify" functions like "parse".
|
||||
// So we simply expose all functions into this namespace.
|
||||
using namespace ::yycc::string::stringify;
|
||||
|
||||
} // namespace yycc::rust::stringify
|
Reference in New Issue
Block a user