Skip to content

Commit

Permalink
feat: util specs
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Sep 6, 2023
1 parent deaa47c commit d10a921
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 93 deletions.
2 changes: 2 additions & 0 deletions include/mrdox/Dom/Kind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ namespace dom {
*/
enum class Kind
{
Undefined,
Null,
Boolean,
Integer,
String,
SafeString,
Array,
Object,
Function
Expand Down
42 changes: 24 additions & 18 deletions include/mrdox/Dom/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,17 @@

namespace clang {
namespace mrdox {

dom::Value
safeString(std::string_view str);

namespace dom {

/** A variant container for any kind of Dom value.
*/
class MRDOX_DECL
Value
{
enum class Kind
{
Null,
Boolean,
Integer,
String,
Array,
Object,
Function
};

Kind kind_;

union
Expand All @@ -56,6 +49,7 @@ class MRDOX_DECL

friend class Array;
friend class Object;
friend Value clang::mrdox::safeString(std::string_view s);

public:
~Value();
Expand All @@ -65,13 +59,15 @@ class MRDOX_DECL
Value& operator=(Value const&);
Value& operator=(Value&&) noexcept;

Value(dom::Kind kind) noexcept;
Value(std::nullptr_t) noexcept;
Value(std::int64_t) noexcept;
Value(String str) noexcept;
Value(Array arr) noexcept;
Value(Object obj) noexcept;
Value(Function fn) noexcept;


template <std::integral T>
requires (!std::same_as<T, bool>)
Value(T v) noexcept : Value(std::int64_t(v)) {}
Expand All @@ -85,7 +81,7 @@ class MRDOX_DECL
}

template<class Enum>
requires std::is_enum_v<Enum>
requires std::is_enum_v<Enum> && (!std::same_as<Enum, dom::Kind>)
Value(Enum v) noexcept
: Value(static_cast<
std::underlying_type_t<Enum>>(v))
Expand All @@ -99,16 +95,12 @@ class MRDOX_DECL
}

// VFALCO Should this be a literal?
#if 0
Value(char const* s)
: Value(String(s))
{
}
#endif

template<class StringLike>
requires std::convertible_to<
StringLike, String>
template <std::convertible_to<String> StringLike>
Value(StringLike const& s)
: Value(String(s))
{
Expand Down Expand Up @@ -136,6 +128,13 @@ class MRDOX_DECL
*/
dom::Kind kind() const noexcept;

/** Return true if this is undefined.
*/
bool isUndefined() const noexcept
{
return kind_ == Kind::Undefined;
}

/** Return true if this is null.
*/
bool isNull() const noexcept
Expand Down Expand Up @@ -164,6 +163,13 @@ class MRDOX_DECL
return kind_ == Kind::String;
}

/** Return true if this is a safe string.
*/
bool isSafeString() const noexcept
{
return kind_ == Kind::SafeString;
}

/** Return true if this is an array.
*/
bool isArray() const noexcept
Expand Down Expand Up @@ -203,7 +209,7 @@ class MRDOX_DECL
String const&
getString() const noexcept
{
MRDOX_ASSERT(isString());
MRDOX_ASSERT(isString() || isSafeString());
return str_;
}

Expand Down
4 changes: 2 additions & 2 deletions include/mrdox/Support/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ Expected(
U&& u)
: has_error_(false)
{
static_assert(std::is_nothrow_convertible_v<U, T>);
static_assert(! std::convertible_to<U, Error>);
static_assert(std::is_convertible_v<U, T>);
static_assert(!std::convertible_to<U, Error>);

std::construct_at(&v_, std::forward<U>(u));
}
Expand Down
53 changes: 30 additions & 23 deletions include/mrdox/Support/Handlebars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ namespace detail {
st.append( sv.data(), sv.data() + sv.size() );
};

struct MRDOX_DECL safeStringWrapper {
std::string v_;
};

struct RenderState;

// Heterogeneous lookup support
Expand Down Expand Up @@ -1037,8 +1033,7 @@ class Handlebars {
using R = std::invoke_result_t<F, dom::Array const&, HandlebarsCallback const&>;
static_assert(
std::same_as<R, void> ||
std::convertible_to<R, dom::Value> ||
std::same_as<R, detail::safeStringWrapper>);
std::convertible_to<R, dom::Value>);
registerHelperImpl(name, helper_type([helper = std::forward<F>(helper)](
dom::Array const& args, HandlebarsCallback const& options)
-> std::pair<dom::Value, HelperBehavior> {
Expand All @@ -1051,10 +1046,6 @@ class Handlebars {
helper(args, options);
return {nullptr, HelperBehavior::NO_RENDER};
}
else if constexpr (std::same_as<R, detail::safeStringWrapper>)
{
return {helper(args, options).v_, HelperBehavior::RENDER_RESULT_NOESCAPE};
}
}));
}

Expand Down Expand Up @@ -1084,8 +1075,7 @@ class Handlebars {
using R = std::invoke_result_t<F, dom::Array const&>;
static_assert(
std::same_as<R, void> ||
std::convertible_to<R, dom::Value> ||
std::same_as<R, detail::safeStringWrapper>);
std::convertible_to<R, dom::Value>);
registerHelperImpl(name, helper_type([helper = std::forward<F>(helper)](
dom::Array const& args, HandlebarsCallback const&)
-> std::pair<dom::Value, HelperBehavior> {
Expand All @@ -1098,10 +1088,6 @@ class Handlebars {
helper(args);
return {nullptr, HelperBehavior::NO_RENDER};
}
else if constexpr (std::same_as<R, detail::safeStringWrapper>)
{
return {helper(args).v_, HelperBehavior::RENDER_RESULT_NOESCAPE};
}
}));
}

Expand Down Expand Up @@ -1134,8 +1120,7 @@ class Handlebars {
using R = std::invoke_result_t<F>;
static_assert(
std::same_as<R, void> ||
std::convertible_to<R, dom::Value> ||
std::same_as<R, detail::safeStringWrapper>);
std::convertible_to<R, dom::Value>);
registerHelperImpl(name, helper_type([helper = std::forward<F>(helper)](
dom::Array const&, HandlebarsCallback const&)
-> std::pair<dom::Value, HelperBehavior> {
Expand All @@ -1147,10 +1132,6 @@ class Handlebars {
{
helper();
return {nullptr, HelperBehavior::NO_RENDER};
}
else if constexpr (std::same_as<R, detail::safeStringWrapper>)
{
return {helper().v_, HelperBehavior::RENDER_RESULT_NOESCAPE};
}
}));
}
Expand Down Expand Up @@ -1354,7 +1335,7 @@ createFrame(dom::Object const& parent);
@see https://handlebarsjs.com/api-reference/utilities.html#handlebars-safestring-string
*/
MRDOX_DECL
detail::safeStringWrapper
dom::Value
safeString(std::string_view str);

/** HTML escapes the specified string
Expand Down Expand Up @@ -1388,6 +1369,32 @@ escapeExpression(
OutputRef out,
std::string_view str);

template <std::convertible_to<dom::Value> DomValue>
requires (!std::convertible_to<DomValue, std::string_view>)
std::string
escapeExpression(
DomValue const& val)
{
dom::Value v = val;
if (v.isString())
{
return escapeExpression(std::string_view(v.getString()));
}
if (v.isObject() && v.getObject().exists("toHTML"))
{
dom::Value fn = v.getObject().find("toHTML");
if (fn.isFunction()) {
return toString(fn.getFunction()());
}
}
if (v.isNull() || v.isUndefined())
{
return {};
}
return toString(v);
}


/** An error thrown or returned by Handlebars
An error returned or thrown by Handlebars environment when
Expand Down
14 changes: 8 additions & 6 deletions share/gdb/mrdox_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ def __init__(self, value):
def children(self):
# Get kind enum
kind = self.value['kind_']
if kind == 1:
if kind == 2:
yield 'Boolean', self.value['b_']
elif kind == 2:
yield 'Integer', self.value['i_']
elif kind == 3:
yield 'String', self.value['str_']
yield 'Integer', self.value['i_']
elif kind == 4:
yield 'Array', self.value['arr_']
yield 'String', self.value['str_']
elif kind == 5:
yield 'Object', self.value['obj_']['impl_']['_M_ptr']
yield 'SafeString', self.value['str_']
elif kind == 6:
yield 'Array', self.value['arr_']
elif kind == 7:
yield 'Object', self.value['obj_']['impl_']['_M_ptr']
elif kind == 8:
yield 'Function', self.value['fn_']
else:
yield 'kind_', self.value['kind_']
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Dom/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ Array(
{
}

Array::
Array(storage_type elements)
: impl_(std::make_shared<DefaultArrayImpl>())
{
for(auto const& e : elements)
impl_->emplace_back(e);
}

Array&
Array::
operator=(
Expand Down
Loading

0 comments on commit d10a921

Please sign in to comment.