(defn use->require [env [lib & filters :as spec]]
(when-not (and (symbol? lib) (odd? (count spec)))
(throw
(error env
(parse-ns-error-msg spec
"Only [lib.ns :only (names)] and optionally `:rename {from to}` specs supported in :use / :use-macros"))))
(loop [fs (seq filters) ret [lib] err false]
(cond
(true? err)
(throw
(error env
(parse-ns-error-msg spec
"Only [lib.ns :only (names)] and optionally `:rename {from to}` specs supported in :use / :use-macros")))
(some? fs)
(let [kw (first fs)
only? (= kw :only)]
(if (or only? (= kw :rename))
(if (some? (some #{(if only? :refer kw)} ret))
(throw
(error env
(parse-ns-error-msg spec
"Each of :only and :rename options may only be specified once in :use / :use-macros")))
(let [refs (second fs)]
(if-not (or (and only? (sequential? refs) (every? symbol? refs))
(and (= kw :rename) (map? refs) (every? #(every? symbol? %) refs)))
(recur fs ret true)
(recur (nnext fs) (into ret [(if only? :refer kw) refs]) false))))
(recur fs ret true )))
:else (if (some? (some #{:refer} ret))
ret
(recur fs ret true)))))