|
|
@@ -0,0 +1,77 @@
|
|
|
+#pragma once
|
|
|
+
|
|
|
+#include <map>
|
|
|
+#include <stdexcept>
|
|
|
+#include <string_view>
|
|
|
+#include <vector>
|
|
|
+
|
|
|
+#include <jvalidate/adapter.h>
|
|
|
+#include <jvalidate/detail/number.h>
|
|
|
+#include <jvalidate/detail/simple_adapter.h>
|
|
|
+#include <jvalidate/enum.h>
|
|
|
+#include <jvalidate/status.h>
|
|
|
+
|
|
|
+namespace jvalidate::detail {
|
|
|
+template <typename CRTP> class UnsupportedArrayAdapter {
|
|
|
+public:
|
|
|
+ size_t size() const { return 0; }
|
|
|
+ CRTP operator[](size_t) const { throw std::runtime_error("stub implementation"); }
|
|
|
+ std::vector<CRTP>::const_iterator begin() const { return {}; }
|
|
|
+ std::vector<CRTP>::const_iterator end() const { return {}; }
|
|
|
+};
|
|
|
+
|
|
|
+template <typename CRTP> class UnsupportedObjectAdapter {
|
|
|
+public:
|
|
|
+ size_t size() const { return 0; }
|
|
|
+ bool contains(std::string_view) const { return false; }
|
|
|
+ CRTP operator[](std::string_view) const { throw std::runtime_error("stub implementation"); }
|
|
|
+ std::map<std::string, CRTP>::const_iterator begin() const { return {}; }
|
|
|
+ std::map<std::string, CRTP>::const_iterator end() const { return {}; }
|
|
|
+};
|
|
|
+
|
|
|
+class StringAdapter final : public adapter::Adapter {
|
|
|
+public:
|
|
|
+ using value_type = std::string_view;
|
|
|
+
|
|
|
+ StringAdapter(std::string_view value) : value_(value) {}
|
|
|
+
|
|
|
+ adapter::Type type() const { return adapter::Type::String; }
|
|
|
+ bool as_boolean() const { die("boolean"); }
|
|
|
+ int64_t as_integer() const { die("integer"); }
|
|
|
+ double as_number() const { die("number"); }
|
|
|
+ std::string as_string() const { return std::string(value_); }
|
|
|
+
|
|
|
+ size_t array_size() const { die("array"); }
|
|
|
+ UnsupportedArrayAdapter<StringAdapter> as_array() const { die("array"); }
|
|
|
+ Status apply_array(adapter::AdapterCallback const &) const { return Status::Noop; }
|
|
|
+
|
|
|
+ size_t object_size() const { die("object"); }
|
|
|
+ UnsupportedObjectAdapter<StringAdapter> as_object() const { die("object"); }
|
|
|
+ Status apply_object(adapter::ObjectAdapterCallback const &) const { return Status::Noop; }
|
|
|
+
|
|
|
+ bool equals(adapter::Adapter const & rhs, bool strict) const {
|
|
|
+ if (std::optional str = rhs.maybe_string(strict)) {
|
|
|
+ return str == value_;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::unique_ptr<adapter::Const const> freeze() const final {
|
|
|
+ return std::make_unique<adapter::detail::GenericConst<std::string_view>>(value_);
|
|
|
+ }
|
|
|
+
|
|
|
+private:
|
|
|
+ [[noreturn]] static void die(std::string expected) {
|
|
|
+ throw std::runtime_error("StringAdapter is not an " + expected);
|
|
|
+ }
|
|
|
+
|
|
|
+private:
|
|
|
+ std::string_view value_;
|
|
|
+};
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+template <> struct jvalidate::adapter::AdapterTraits<std::string_view> {
|
|
|
+ template <typename> using Adapter = jvalidate::detail::StringAdapter;
|
|
|
+ using ConstAdapter = jvalidate::detail::StringAdapter;
|
|
|
+};
|