add error message for invalid type usage

This commit is contained in:
Kristin Rutenkolk 2024-10-24 11:37:40 +02:00
parent cf2decedcd
commit 020e10264f

View file

@ -1919,36 +1919,38 @@
This creates needed serialization and deserialization implementations for the new type." This creates needed serialization and deserialization implementations for the new type."
{:style/indent [:defn]} {:style/indent [:defn]}
[typename members] [typename members]
(cond (let [invalid-typenames (filter #(try (c-layout (first %)) nil (catch Exception e (first %))) (partition 2 members))]
(odd? (count members)) (throw (Exception. "uneven amount of members supplied. members have to be typed and are required to be supplied in the form of `typename member-name`. the typename has to be coffi typename, like `:coffi.mem/int` or `[:coffi.mem/array :coffi.mem/byte 3]`")) (cond
:else (odd? (count members)) (throw (Exception. "uneven amount of members supplied. members have to be typed and are required to be supplied in the form of `typename member-name`. the typename has to be coffi typename, like `:coffi.mem/int` or `[:coffi.mem/array :coffi.mem/byte 3]`"))
(let [coffi-typename (keyword (str *ns*) (str typename)) (seq invalid-typenames) (throw (Exception. (str "invalid typename/s " (print-str invalid-typenames) ". typename has to be coffi typename, like `:coffi.mem/int` or `[:coffi.mem/array :coffi.mem/byte 3]`")))
typed-symbols (->> :else
members (let [coffi-typename (keyword (str *ns*) (str typename))
(partition 2 2) typed-symbols (->>
(map (fn [[_type sym]] (with-meta sym {:tag (coffitype->typename _type)}))) members
(vec)) (partition 2 2)
struct-layout (with-c-layout [::struct (map (fn [[_type sym]] (with-meta sym {:tag (coffitype->typename _type)})))
(->> (vec))
members struct-layout (with-c-layout [::struct
(partition 2 2) (->>
(map vec) members
(map #(update % 1 keyword)) (partition 2 2)
(map reverse) (map vec)
(map vec))])] (map #(update % 1 keyword))
(register-new-struct-deserialization coffi-typename struct-layout) (map reverse)
(register-new-struct-serialization coffi-typename struct-layout) (map vec))])]
`(do (register-new-struct-deserialization coffi-typename struct-layout)
~(generate-struct-type typename typed-symbols true) (register-new-struct-serialization coffi-typename struct-layout)
(defmethod c-layout ~coffi-typename [~'_] (c-layout ~struct-layout)) `(do
(defmethod deserialize-from ~coffi-typename ~['segment '_type] ~(generate-struct-type typename typed-symbols true)
~(first (generate-deserialize coffi-typename 0))) (defmethod c-layout ~coffi-typename [~'_] (c-layout ~struct-layout))
(defmethod serialize-into ~coffi-typename ~[(with-meta 'source-obj {:tag typename}) '_type 'segment '_] (defmethod deserialize-from ~coffi-typename ~['segment '_type]
~(generate-serialize coffi-typename (with-meta 'source-obj {:tag typename}) 0)) ~(first (generate-deserialize coffi-typename 0)))
(defmethod clojure.pprint/simple-dispatch ~typename [~'obj] (clojure.pprint/simple-dispatch (into {} ~'obj))) (defmethod serialize-into ~coffi-typename ~[(with-meta 'source-obj {:tag typename}) '_type 'segment '_]
(defmethod clojure.core/print-method ~typename [~'obj ~'writer] (print-simple (into {} ~'obj) ~'writer)) ~(generate-serialize coffi-typename (with-meta 'source-obj {:tag typename}) 0))
) (defmethod clojure.pprint/simple-dispatch ~typename [~'obj] (clojure.pprint/simple-dispatch (into {} ~'obj)))
) (defmethod clojure.core/print-method ~typename [~'obj ~'writer] (print-simple (into {} ~'obj) ~'writer))
) )
)
))
) )