<commit_before>(ns db-quiz-prep.mustache
  (:require [db-quiz-prep.util :refer [join-lines]]
            [clojure.set :refer [difference]]
            [clojure.string :as string]
            [stencil.parser :refer [parse]]))

(defn get-template-variables
  "Get a set of variables used in `template`."
  [template]
  (->> (parse template)
       (filter (complement string?))
       (mapcat :name)
       distinct
       set))

(defn validate
  "Validate the correctness of Mustache syntax in `template`."
  [template]
  (try (parse template) nil
       (catch Exception e (join-lines ["The provided Mustache template is invalid:"
                                       (.getMessage e)]))))

(defn all-variables-provided?
  "Validates if `config` provides all variables used in `template`."
  [config template]
  (let [map-set-names (comp set (partial map name))
        template-variables (map-set-names (difference (get-template-variables template) #{:limit :offset}))
        provided-variables (map-set-names (keys (get config :params {})))
        missing-variables (difference template-variables provided-variables)]
    (when-let [error (or (nil? (seq template-variables))
                         (seq missing-variables))]
      (join-lines ["Configuration is missing these variables used in the template:"
                   (string/join ", " missing-variables)]))))<commit_msg>Leaner Mustache functions<commit_after>(ns db-quiz-prep.mustache
  (:require [db-quiz-prep.util :refer [join-lines]]
            [clojure.set :refer [difference]]
            [clojure.string :as string]
            [stencil.parser :refer [parse]]))

(defn get-template-variables
  "Get a set of variables used in `template`."
  [template]
  (->> (parse template)
       (filter (complement string?))
       (mapcat :name)
       distinct
       set))

{'fraction_non_alphanumeric': 0.1294885745375408, 'fraction_numerical': 0.0, 'mean_word_length': 2.6927710843373496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5691754', 'n_tokens_mistral': 534, 'n_tokens_neox': 527, 'n_words': 155}