Files
tsl-devkit/lsp-server/src/protocol/transform/facade.hpp
T
2025-11-11 23:12:03 +08:00

83 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include "./transformer.hpp"
namespace lsp::transform
{
// ==================== JSON 序列化/反序列化 ====================
template<typename T>
std::optional<T> Deserialize(const std::string& json);
template<typename T>
std::optional<std::string> Serialize(const T& obj);
// ==================== 转换为 LSPAny ====================
/// 任意类型转换为 LSPAny
///
/// 支持的类型:
/// - 基本类型:bool, int, double, string, nullptr
/// - 容器类型:vector, map, optional
/// - LSP 类型:LSPObject, LSPArray, LSPAny
/// - 用户结构体(需要 glaze 支持)
inline constexpr auto ToLSPAny = [](const auto& value) {
return LSPAnyConverter::ToLSPAny(value);
};
/// LSPAny 转换为指定类型
///
/// 支持的类型:
/// - 基本类型:bool, int, double, string
/// - 容器类型:vector<T>, optional<T>
/// - LSP 类型:LSPObject, LSPArray
/// - 用户结构体(需要 glaze 支持)
inline constexpr auto FromLSPAny = []<typename T>(const auto& input) -> T {
using InputType = std::decay_t<decltype(input)>;
protocol::LSPAny any;
// 如果是 variant,先提取
if constexpr (std::is_same_v<InputType, std::variant<protocol::LSPArray, protocol::LSPObject>>)
any = std::visit([](const auto& v) -> protocol::LSPAny { return v; }, input);
// 否则直接用(LSPAny、LSPObject、LSPArray 都能隐式转换)
else
any = input;
return LSPAnyConverter::FromLSPAny<T>(any);
};
namespace check
{
/// 检查是否为 LSPObject
bool IsObject(const protocol::LSPAny& any);
/// 检查是否为 LSPArray
bool IsArray(const protocol::LSPAny& any);
/// 检查是否为字符串
bool IsString(const protocol::LSPAny& any);
/// 检查是否为数字(integer/uinteger/decimal
bool IsNumber(const protocol::LSPAny& any);
/// 检查是否为布尔值
bool IsBool(const protocol::LSPAny& any);
/// 检查是否为 null
bool IsNull(const protocol::LSPAny& any);
}
// ==================== 调试工具 ====================
namespace debug
{
/// 获取 LSPAny 的类型名称字符串
std::string GetTypeName(const protocol::LSPAny& any);
/// 将 variant<int, string> 转换为字符串(用于 ID)
std::string GetIdString(const std::variant<int, std::string>& id);
}
}
#include "./facade.inl"