Skip to content

Commit

Permalink
[nop] Misc housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 8, 2023
1 parent f8e0f78 commit 1bab9b7
Showing 1 changed file with 66 additions and 68 deletions.
134 changes: 66 additions & 68 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,71 @@
(remove-ns 'taoensso.encore)
(test/run-tests))

;;;;

(defn ^:no-doc -unexpected-arg!
"Private low-level util."
{:added "v3.66.0 (2023-08-23)"}
([arg] (-unexpected-arg! arg nil))
([arg {:keys [msg] :as details}]
(throw
(ex-info (or msg (str "Unexpected argument: " arg))
(conj {:arg {:value arg, :type (type arg)}} details)))))

(defn unexpected-arg!
"Throws runtime `ExceptionInfo` to indicate an unexpected argument.
Takes optional kvs for merging into exception's data map.
(let [mode :unexpected]
(case mode
:read (do <...>)
:write (do <...>)
(unexpected-arg! mode
:expected #{:read :write}))) =>
Unexpected argument: :unexpected
{:arg {:value :unexpected, :type clojure.lang.Keyword},
:expected #{:read :write}}"

{:added "v3.51.0 (2023-03-13)"}
[arg & {:keys [msg] :as details}]
(-unexpected-arg! arg details))

(comment (unexpected-arg! :arg :expected '#{string?}))

;;;; Core macros

#?(:clj
(defmacro compile-if
"Evaluates `test`. If it returns logical true (and doesn't throw), expands
to `then`, otherwise expands to `else`."
{:style/indent 1}
([test then ] `(compile-if ~test ~then nil)) ; Back compatibility
([test then ] `(compile-if ~test ~then nil))
([test then else]
(if (try (eval test) (catch Throwable _ false))
`(do ~then)
`(do ~else)))))

#?(:clj (defmacro compile-when {:style/indent 1} [test & body] `(compile-if ~test (do ~@body) nil)))

#?(:clj
(defmacro try-eval
"Evaluates `form`. If eval doesn't throw, expands to `form`, otherwise to nil."
"If `form` can be successfully evaluated at macro-expansion time, expands to `form`.
Otherwise expands to `nil`."
{:added "v3.50.0 (2023-03-07)"}
[form] `(compile-if ~form ~form nil)))

(comment
(macroexpand '(try-eval (java.util.concurrent.atomic.AtomicLong.)))
(macroexpand '(try-eval (java.util.concurrent.Executors/newVirtualThreadPerTaskExecutor)))
(macroexpand '(try-eval (do (println "effect") :x))))
(comment (macroexpand '(try-eval (com.google.common.io.BaseEncoding/base16))))

#?(:clj (defmacro compile-when {:style/indent 1} [test & body] `(compile-if ~test (do ~@body) nil)))
#?(:clj
(compile-if
;; Avoiding for edge case where user cares about startup time and has
;; `core.async` as dependency but it never gets required anywhere else
;; (do (require 'clojure.core.async) true)
(do (or
(io/resource "clojure/core/async.clj")
(io/resource "clojure/core/async.cljc")))
(def have-core-async? true)
(def have-core-async? false)))

(comment (require '[clojure.core.async] :verbose))
(def have-core-async?
"Is `core.async` available?"
(compile-if
(boolean
(or
(io/resource "clojure/core/async.cljc")
(io/resource "clojure/core/async.clj")))
false)))

#?(:clj
(defmacro if-let
Expand Down Expand Up @@ -441,14 +469,6 @@
`(cljs.core/defonce ~sym ~@body)
`(clojure.core/defonce ~sym ~@body)))))

;;;; Core fns

(def ^:private core-merge #?(:clj clojure.core/merge :cljs cljs.core/merge))
(def ^:private core-update-in #?(:clj clojure.core/update-in :cljs cljs.core/update-in))
(declare merge update-in)

;;;; Secondary macros

#?(:clj
(defmacro identical-kw?
"Returns true iff two keywords are identical.
Expand All @@ -472,12 +492,6 @@
~@(map-indexed (fn [i# form#] (if (even? i#) (eval form#) form#)) clauses)
~(when default default)))))

#?(:clj
(do
(defmacro do-nil [& body] `(do ~@body nil))
(defmacro do-false [& body] `(do ~@body false))
(defmacro do-true [& body] `(do ~@body true))))

#?(:clj
(defmacro doto-cond
"Cross between `doto`, `cond->` and `as->`."
Expand All @@ -491,6 +505,20 @@
~@(map pstep (partition 2 clauses))
~g))))

#?(:clj
(do
(defmacro do-nil [& body] `(do ~@body nil))
(defmacro do-false [& body] `(do ~@body false))
(defmacro do-true [& body] `(do ~@body true))))

;;;;

(def ^:private core-merge #?(:clj clojure.core/merge :cljs cljs.core/merge))
(def ^:private core-update-in #?(:clj clojure.core/update-in :cljs cljs.core/update-in))
(declare merge update-in)

;;;;

#?(:clj
(defmacro declare-remote
"Declares given ns-qualified symbols, preserving metadata. Useful for
Expand Down Expand Up @@ -580,8 +608,6 @@
(when ~link? (-alias-link-var (var ~alias-sym) ~src-var ~alias-attrs))
(do (var ~alias-sym)))))))))

(declare -unexpected-arg!)

#?(:clj
(defmacro defaliases
"Bulk version of `defalias`.
Expand Down Expand Up @@ -622,9 +648,11 @@
"Private, used by other Taoensso libs."
{:added "v3.57.0 (2023-03-29)"}
[& types]
`(do ~@(map (fn [type]
`(defmethod print-method ~type [~'x ~(with-meta 'w {:tag 'java.io.Writer})]
(.write ~'w (str ~(str "#" *ns* ".") ~'x)))) types))))
`(do
~@(map
(fn [type]
`(defmethod print-method ~type [~'x ~(with-meta 'w {:tag 'java.io.Writer})]
(.write ~'w (str ~(str "#" *ns* ".") ~'x)))) types))))

(comment
(do
Expand Down Expand Up @@ -1438,36 +1466,6 @@

;;;;

(defn ^:no-doc -unexpected-arg!
"Private low-level util."
{:added "v3.66.0 (2023-08-23)"}
([arg] (-unexpected-arg! arg nil))
([arg {:keys [msg] :as details}]
(throw
(ex-info (or msg (str "Unexpected argument: " arg))
(conj {:arg {:value arg, :type (type arg)}} details)))))

(defn unexpected-arg!
"Throws runtime `ExceptionInfo` to indicate an unexpected argument.
Takes optional kvs for merging into exception's data map.
(let [mode :unexpected]
(case mode
:read (do <...>)
:write (do <...>)
(unexpected-arg! mode
:expected #{:read :write}))) =>
Unexpected argument: :unexpected
{:arg {:value :unexpected, :type clojure.lang.Keyword},
:expected #{:read :write}}"

{:added "v3.51.0 (2023-03-13)"}
[arg & {:keys [msg] :as details}]
(-unexpected-arg! arg details))

(comment (unexpected-arg! :arg :expected '#{string?}))

(defn instance!
"If (instance? class arg) is true, returns arg.
Otherwise throws runtime `ExceptionInfo` with `unexpected-arg!`.
Expand Down Expand Up @@ -5672,7 +5670,7 @@
((:fn (tf-state (after-timeout 200 (println *foo*) *foo*))))))

;;;; DEPRECATED
;; {:deprecated "v<x.y.z> (<yyyy-mm-dd>)" :doc "Prefer `<replacement>`"}
;; {:deprecated "v<X.Y.Z> (<YYYY-MM-DD>)" :doc "Prefer `<replacement>`."}

#?(:clj
(defmacro deprecated
Expand Down

0 comments on commit 1bab9b7

Please sign in to comment.