Browse Source

refactor: compress details in vocabulary::Metadata w/ enum

Sam Jaffe 3 months ago
parent
commit
900d939c13
2 changed files with 14 additions and 11 deletions
  1. 2 2
      include/jvalidate/detail/vocabulary.h
  2. 12 9
      include/jvalidate/vocabulary.h

+ 2 - 2
include/jvalidate/detail/vocabulary.h

@@ -86,7 +86,7 @@ public:
    * @param word The "key"word being looked up (e.g. "if", "properties", ...)
    * @param word The "key"word being looked up (e.g. "if", "properties", ...)
    */
    */
   bool is_keyword(std::string_view word) const {
   bool is_keyword(std::string_view word) const {
-    return has(word) && metadata_.at(word).is_keyword;
+    return has(word) && metadata_.at(word).type != vocabulary::KeywordType::None;
   }
   }
 
 
   /**
   /**
@@ -96,7 +96,7 @@ public:
    * @param word The "key"word being looked up (e.g. "if", "properties", ...)
    * @param word The "key"word being looked up (e.g. "if", "properties", ...)
    */
    */
   bool is_property_keyword(std::string_view word) const {
   bool is_property_keyword(std::string_view word) const {
-    return has(word) && metadata_.at(word).is_keyword_map;
+    return has(word) && metadata_.at(word).type == vocabulary::KeywordType::KeywordMap;
   }
   }
 
 
   /**
   /**

+ 12 - 9
include/jvalidate/vocabulary.h

@@ -70,6 +70,8 @@ constexpr struct {
  */
  */
 struct DependentKeyword : std::string_view {};
 struct DependentKeyword : std::string_view {};
 
 
+enum class KeywordType : char { None, Keyword, KeywordMap };
+
 /**
 /**
  * @brief This type represents a union of several different "parsing handler"
  * @brief This type represents a union of several different "parsing handler"
  * states, aligned to a specific starting version:
  * states, aligned to a specific starting version:
@@ -95,25 +97,26 @@ struct DependentKeyword : std::string_view {};
 template <Adapter A> struct Metadata {
 template <Adapter A> struct Metadata {
   using pConstraint = std::unique_ptr<constraint::Constraint>;
   using pConstraint = std::unique_ptr<constraint::Constraint>;
 
 
-  Metadata(decltype(Removed)) {}
+  Metadata(decltype(Removed)) : valid(false) {}
   Metadata(decltype(Literal)) {}
   Metadata(decltype(Literal)) {}
-  Metadata(decltype(KeywordMap)) : is_keyword(true), is_keyword_map(true) {}
-  Metadata(DependentKeyword dep) : is_keyword(true) {}
+  Metadata(decltype(KeywordMap)) : type(KeywordType::KeywordMap) {}
+  Metadata(DependentKeyword dep) : type(KeywordType::Keyword) {}
 
 
   template <typename F> Metadata(F make) : make(make) {}
   template <typename F> Metadata(F make) : make(make) {}
-  template <typename F> Metadata(F make, decltype(Keyword)) : make(make), is_keyword(true) {}
   template <typename F>
   template <typename F>
-  Metadata(F make, decltype(KeywordMap)) : make(make), is_keyword(true), is_keyword_map(true) {}
+  Metadata(F make, decltype(Keyword)) : make(make), type(KeywordType::Keyword) {}
+  template <typename F>
+  Metadata(F make, decltype(KeywordMap)) : make(make), type(KeywordType::KeywordMap) {}
   template <typename F>
   template <typename F>
   Metadata(F make, decltype(PostConstraint))
   Metadata(F make, decltype(PostConstraint))
-      : make(make), is_keyword(true), is_post_constraint(true) {}
+      : make(make), type(KeywordType::Keyword), is_post_constraint(true) {}
 
 
-  explicit operator bool() const { return make || is_keyword; }
+  explicit operator bool() const { return valid; }
   operator std::function<pConstraint(detail::ParserContext<A> const &)>() const { return make; }
   operator std::function<pConstraint(detail::ParserContext<A> const &)>() const { return make; }
 
 
   std::function<pConstraint(detail::ParserContext<A> const &)> make = nullptr;
   std::function<pConstraint(detail::ParserContext<A> const &)> make = nullptr;
-  bool is_keyword = false;
-  bool is_keyword_map = false;
+  bool valid = true;
+  KeywordType type = KeywordType::None;
   bool is_post_constraint = false;
   bool is_post_constraint = false;
 };
 };
 }
 }