1
0

feat: add lost functions for env namespace.

- add some lost functions for env namespace according to Rust std library. hiwever some functions are not implemented.
- change some function's signatures located in env namespace.
- reduce some useless error kine in env namespace.
This commit is contained in:
2025-12-07 21:47:54 +08:00
parent f76eabee7a
commit 8b7ab2c870
4 changed files with 114 additions and 35 deletions

View File

@@ -1,11 +1,11 @@
#pragma once
#include <string>
#include <string_view>
#include <filesystem>
#include <expected>
/**
* @brief The namespace providing runtime environment operations.
* @details
* When I programming with Rust, I was astonished that
* Rust standard library have so much robust environment-related operations,
@@ -20,18 +20,24 @@ namespace yycc::env {
/// @brief The error occurs in environment variable operations.
enum class VarError {
SysCall, ///< Error occurs when calling backend functions.
NoSuchName, ///< The variable with given name is not presented.
BadEncoding, ///< Error when performing encoding convertion.
BadArithmetic, ///< Error when performing arithmetic operations.
BadCall, ///< Error occurs when calling backend functions.
BadName, ///< Given name is ill-formated (empty string or has "=" character).
BadEncoding, ///< Error when performing encoding convertion.
NoMemory, ///< No enough memory to finish this operation.
Others, ///< Any other error types.
};
/// @brief The result type in environment variable operations.
template<typename T>
using VarResult = std::expected<T, VarError>;
/**
* @brief The pair representing an environment variable.
* @details The left side is the name of variable. The right side is the content of variable.
*/
using VarPair = std::pair<std::u8string, std::u8string>;
/**
* @brief Get the value of given environment variable name.
* @param[in] name The name of environment variable
@@ -61,17 +67,21 @@ namespace yycc::env {
*/
VarResult<void> del_var(const std::u8string_view& name);
/**
* @brief Returns an list of (variable, value) pairs of strings,
* for all the environment variables of the current process.
* @return The list holding all variables.
*/
VarResult<std::vector<VarPair>> get_vars();
#pragma endregion
#pragma region Environment Path
/// @brief Error occurs when operating path related functions.
enum class PathError {
Win32, ///< Underlying Win32 function error.
Posix, ///< Underlying POSIX failed.
BadSize, ///< Written size if not matched with expected size.
BadCast, ///< Error occurs when casting values.
Overflow, ///< Some arithmetic operation overflow.
SysCall, ///< Underlying system calling error.
Others, ///< Any other error types.
};
/// @brief The result type used for path related functions;
@@ -79,10 +89,49 @@ namespace yycc::env {
using PathResult = std::expected<T, PathError>;
/**
* @brief Get the path of the current running executable.
* @return Gotten path (no absolute path guaranteed) or error occurs.
* @brief Returns the current working directory.
* @return Current working directory path or error occurs.
*/
PathResult<std::u8string> current_exe();
PathResult<std::filesystem::path> current_dir();
/**
* @brief Returns the path of the current running executable.
* @return Current running executable path or error occurs.
* Please note that there is no guarantee that return path is absolute path.
*/
PathResult<std::filesystem::path> current_exe();
/**
* @brief Returns the path of the current user's home directory if known.
* @return Current user's home directory path or error occurs.
*/
PathResult<std::filesystem::path> home_dir();
/**
* @brief Returns the path of a temporary directory.
* @return The path of a temporary directory.
*/
PathResult<std::filesystem::path> temp_dir();
#pragma endregion
#pragma region Environment Argument
/// @brief Error occurs when operating argument related functions.
enum class ArgError {
SysCall, ///< Underlying system calling error.
Others, ///< Any other error types.
};
/// @brief The result type used for argument related functions;
template<typename T>
using ArgResult = std::expected<T, ArgError>;
/**
* @brief Returns the arguments that this program was started with (normally passed via the command line).
* @return The list holding all argument one by one.
*/
ArgResult<std::vector<std::u8string>> get_args();
#pragma endregion