Skip to content

Commit

Permalink
Merge pull request #39 from injae/dev
Browse files Browse the repository at this point in the history
fmt version up 7.2.1 -> 8.0.1
  • Loading branch information
injae authored Sep 17, 2021
2 parents 922053b + 0355aff commit e7a4e45
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 160 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ find_cppkg(nlohmann_json 3.9.1 MODULE nlohmann_json::nlohmann_json TYPE lib OPT
find_cppkg(ryml 0.2.0 MODULE ryml::ryml TYPE lib OPTIONAL OFF)
find_cppkg(toml11 3.7.0 MODULE toml11::toml11 TYPE lib OPTIONAL OFF)
find_cppkg(yaml-cpp 0.6.3 MODULE yaml-cpp TYPE lib OPTIONAL OFF)
find_cppkg(fmt 7.1.3 MODULE fmt::fmt TYPE lib OPTIONAL OFF)
find_cppkg(fmt 8.0.1 MODULE fmt::fmt-header-only TYPE lib OPTIONAL OFF)
find_cppkg(magic_enum 0.7.3 MODULE magic_enum::magic_enum TYPE lib)
find_cppkg(nameof 0.10.0 MODULE nameof::nameof TYPE lib)

Expand Down
2 changes: 1 addition & 1 deletion cppm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
[dependencies]
nameof = { version="0.10.0", link="public"}
magic_enum = { version= "0.7.3", link="public"}
fmt = {version="7.1.3", link="public", optional=true}
fmt = {version="8.0.1", link="public", optional=true}
yaml-cpp = { version="0.6.3", link="public", optional=true}
toml11 = { version="3.7.0", link="public", optional=true}
ryml = { version= "0.2.0", link="public", optional=true}
Expand Down
1 change: 0 additions & 1 deletion examples/parse_file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "serdepp/serde.hpp"

#include "serdepp/adaptor/nlohmann_json.hpp"
#include "serdepp/adaptor/rapidjson.hpp"
#include "serdepp/adaptor/toml11.hpp"
Expand Down
22 changes: 11 additions & 11 deletions include/serdepp/adaptor/fmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
template <typename T>
struct fmt::formatter<T, std::enable_if_t<serde::type::is_struct_v<T>, char>>
: fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const T& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde::to_string(serde_type), ctx);
}
};

template<>
struct fmt::formatter<serde::serde_sstream> : fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const serde::serde_sstream& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.str(), ctx);
template <typename FormatCtx>
auto format(const T& serde_type, FormatCtx& ctx) {
return fmt::formatter<std::string>::format(serde::to_string(serde_type), ctx);
}
};
//
//template<>
//struct fmt::formatter<serde::serde_sstream> : fmt::formatter<std::string> {
// template <typename FormatCtx>
// auto format(const serde::serde_sstream& serde_type, FormatCtx& ctx) {
// return fmt::formatter<std::string>::format(serde_type.str(), ctx);
// }
//};

#endif
62 changes: 35 additions & 27 deletions include/serdepp/adaptor/sstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@
#include <sstream>

namespace serde {
struct serde_sstream {
std::vector<std::string> members;
serde_sstream() : members() {}
class serde_sstream {
public:
serde_sstream(char begin = '{', char end = '}') : members(), begin_(begin), end_(end) {}
void set_wrapper(char begin, char end) { begin_ = begin; end_ = end; }

serde_sstream& add(const std::string& data, std::string_view key="") {
serde_sstream &add(const std::string &data, std::string_view key = "") {
members.push_back(key.empty() ? data : "\"" + std::string{key} + "\": " + data);
return *this;
}

inline std::string str(char begin = '{', char end = '}') const {
inline std::string str() const {
std::string str;
if (members.size() == 1) {
str = members.front();
} else {
str = begin + str;
str = begin_ + str;
for (auto &it : members) { str.append(it + ", "); }
str.pop_back();
str.back() = end;
str.back() = end_;
}
return str;
}
private:
std::vector<std::string> members;
char begin_, end_;
};


template<> struct serde_adaptor_helper<serde_sstream>: derive_serde_adaptor_helper<serde_sstream> {
inline constexpr static bool is_null(serde_sstream& adaptor, std::string_view key) { return false; }
inline constexpr static size_t size(serde_sstream& adaptor) { return 1; }
inline constexpr static bool is_struct(serde_sstream& adaptor) { return true; }
template<>
struct serde_adaptor_helper<serde_sstream> : derive_serde_adaptor_helper<serde_sstream> {
inline constexpr static bool is_null(serde_sstream &adaptor, std::string_view key) { return false; }
inline constexpr static size_t size(serde_sstream &adaptor) { return 1; }
inline constexpr static bool is_struct(serde_sstream &adaptor) { return true; }
};

template<typename T> struct serde_adaptor<serde_sstream, T> {
Expand All @@ -50,8 +54,7 @@ namespace serde {

template<typename T> struct serde_adaptor<serde_sstream, T, type::struct_t> {
inline static void from(serde_sstream& s, std::string_view key, T& data) {
throw serde::unimplemented_error("serde_adaptor<{}>::from(serde_sstream, key data)"
+ std::string(nameof::nameof_short_type<serde_sstream>()));
throw serde::unimplemented_error("serde_adaptor::from(serde_sstream, key data)");
}
inline static void into(serde_sstream &s, std::string_view key, const T& data) {
s.add(serialize<serde_sstream>(data).str(), key);
Expand All @@ -61,37 +64,42 @@ namespace serde {
template<typename... T>
struct serde_adaptor<serde_sstream, std::variant<T...>> {
constexpr static void from(serde_sstream& s, std::string_view key, std::variant<T...>& data) {
throw serde::unimplemented_error("serde_adaptor<{}>::from(serde_sstream, key data)"
+ std::string(nameof::nameof_short_type<serde_sstream>()));
throw serde::unimplemented_error("serde_adaptor<{}>::from(serde_sstream, key data)");
}
constexpr static void into(serde_sstream& s, std::string_view key, const std::variant<T...>& data) {
std::visit([&](auto& type){ serialize_to(type, s, key); }, data);
}
};

template<typename T> struct serde_adaptor<serde_sstream, T, type::seq_t> {
using E = type::seq_e<T>;
static void from(serde_sstream& s, std::string_view key, T& arr) {
throw serde::unimplemented_error("serde_adaptor<{}>::from(serde_sstream, key data)"
+ std::string(nameof::nameof_short_type<serde_sstream>()));
throw serde::unimplemented_error("serde_adaptor::from(serde_sstream, key data)");
}
static void into(serde_sstream& s, std::string_view key, const T& data) {
serde_sstream ss;
for(auto& it : data) { ss.add(serialize<serde_sstream>(it).str()); }
s.add(ss.str('[', ']'), key);
if(key.empty()) {
s.set_wrapper('[', ']');
for(auto& it : data) { s.add(serialize<serde_sstream>(it).str()); }
} else {
serde_sstream ss('[',']');
for(auto& it : data) { ss.add(serialize<serde_sstream>(it).str()); }
s.add(ss.str(), key);
}
}
};

template <typename Map> struct serde_adaptor<serde_sstream, Map, type::map_t> {
using E = type::map_e<Map>;
inline static void from(serde_sstream& s, std::string_view key, Map& map) {
throw serde::unimplemented_error("serde_adaptor<{}>::from(serde_sstream, key data)"
+ std::string(nameof::nameof_short_type<serde_sstream>()));
throw serde::unimplemented_error("serde_adaptor::from(serde_sstream, key data)");
}
inline static void into(serde_sstream& s, std::string_view key, const Map& data) {
serde_sstream ss;
for(auto& [key_, it] : data) { ss.add(serialize<serde_sstream>(it).str(), key_); }
s.add(ss.str(), key);
if(key.empty()) {
for(auto& [key_, it] : data) { s.add(serialize<serde_sstream>(it).str(), key_); }
} else {
serde_sstream ss;
for(auto& [key_, it] : data) { ss.add(serialize<serde_sstream>(it).str(), key_); }
s.add(ss.str(), key);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/serdepp/attribute/flatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace serde::attribute {
constexpr inline void from(serde_ctx& ctx, T& data, std::string_view key,
Next&& next_attr, Attributes&& ...remains) const{
using Helper = serde_adaptor_helper<typename serde_ctx::Adaptor>;
if(Helper::is_null(ctx.adaptor,key)) {
if(Helper::is_null(ctx.adaptor, key)) {
next_attr.from(ctx, data, "", remains...);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion include/serdepp/ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace serde::ostream {
template< class CharT, class Traits, typename T>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT,Traits>& os, const T& x) {
if constexpr (type::is_struct_v<T>) {
return os << serde::to_string(x);
return os << serialize<serde_sstream>(x).str();
} else {
return os << x;
}
Expand Down
Loading

0 comments on commit e7a4e45

Please sign in to comment.