Skip to content

Commit 50e69c2

Browse files
committed
♻️ Removed old-fashioned defstruct and structure
Data structures were deprecated ages ago, replaced these with plain old maps.
1 parent 240bf0f commit 50e69c2

File tree

2 files changed

+39
-69
lines changed

2 files changed

+39
-69
lines changed

src/com/nervestaple/hl7_parser/message.clj

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,31 +142,30 @@
142142
field-value (if (coll? value) value [value])]
143143

144144
;; throw an error if we have an illegal HL7 index
145-
(if (< field-index-fixed 0)
145+
(when (< field-index-fixed 0)
146146
(throw (Exception. "The first field is at index 1")))
147147

148148
;; create a whole new message
149-
(struct-map message-struct
150-
:delimiters (:delimiters message)
149+
{:delimiters (:delimiters message)
151150

152151
;; map over our segments looking for the one we're changing
153-
:segments (map (fn [segment]
152+
:segments (map (fn [segment]
154153

155-
(if (= segment-id (:id segment))
154+
(if (= segment-id (:id segment))
156155

157156
;; associate our new fields
158-
(assoc segment :fields
157+
(assoc segment :fields
159158

160159
;; associate our new value with the
161160
;; field collections
162-
(assoc (:fields segment)
163-
field-index-fixed
164-
(create-field field-value)))
161+
(assoc (:fields segment)
162+
field-index-fixed
163+
(create-field field-value)))
165164

166165
;; return the segment unaltered
167-
segment))
166+
segment))
168167

169-
(:segments message)))))
168+
(:segments message))}))
170169

171170
(defn extract-text-from-segments
172171
"Extracts the text from the parsed message for the supplied index of

src/com/nervestaple/hl7_parser/parser.clj

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,18 @@
2121
;; HL7 Messaging v2.x segment delimiter
2222
(def SEGMENT-DELIMITER ASCII_CR)
2323

24-
;;
25-
;; Data structures used to build a message
26-
;;
27-
28-
(defstruct
29-
#^{:doc "Structure for HL7 message delimiters"}
30-
delimiters-struct :field :component :subcomponent :repeating :escape)
31-
32-
(defstruct
33-
#^{:doc "Structure for an HL7 message"}
34-
message-struct :delimiters :segments)
35-
36-
(defstruct
37-
#^{:doc "Structure for an HL7 segment"}
38-
segment-struct :id :fields)
39-
40-
(defstruct
41-
#^{:doc "Structure for an HL7 field. The :content will either be
42-
at atom, an array of atoms (indicating a field with components),
43-
an array of arrays of atoms (indicating a field with components
44-
and sub-components) or an array of more field
45-
structures (indicating a repeating field)."}
46-
field-struct :content)
47-
4824
;;
4925
;; Emit methods used to output messages
5026
;;
5127

5228
(defn pr-delimiters
5329
"Prints an HL7 compatible text representation of the provided
5430
delimiters to the current *out* stream."
55-
[delimiters-struct]
56-
(str (char (:component delimiters-struct))
57-
(char (:repeating delimiters-struct))
58-
(char (:escape delimiters-struct))
59-
(char (:subcomponent delimiters-struct))))
31+
[delimiters]
32+
(str (char (:component delimiters))
33+
(char (:repeating delimiters))
34+
(char (:escape delimiters))
35+
(char (:subcomponent delimiters))))
6036

6137
(defn- do-pr-content
6238
"Returns an HL7 compatible String representation of the provided
@@ -151,42 +127,41 @@
151127
[values]))
152128

153129
(defn create-empty-message
154-
"Returns a new, empty message structure."
130+
"Returns a new, empty message map."
155131
[]
156-
(struct-map message-struct :delimiters nil :segments []))
132+
{:delimiters nil :segments []})
157133

158134
(defn create-message
159-
"Returns a new, empty message structure."
135+
"Returns a new, empty message map."
160136
[delimiters & segments]
161-
(struct-map message-struct :delimiters delimiters
162-
:segments (if (< 0 (count segments)) (vec segments) [])))
137+
{:delimiters delimiters
138+
:segments (if (< 0 (count segments)) (vec segments) [])})
163139

164140
(defn create-segment
165-
"Returns a new, empty segment structure with the provided id."
141+
"Returns a new, empty segment map with the provided id."
166142
[id & fields]
167-
(struct-map segment-struct :id id
168-
:fields (if (< 0 (count fields)) (vec fields) [])))
143+
{:id id :fields (if (< 0 (count fields)) (vec fields) [])})
169144

170145
(defn create-field
171-
"Returns a new field structure populated with the provided data."
146+
"Returns a new field map populated with the provided data."
172147
[data]
173-
(struct-map field-struct :content (convert-values data)))
148+
{:content (convert-values data)})
174149

175150
(defn add-segment
176-
"Adds the provided segment structure to the provided message
177-
structure and returns a new message."
151+
"Adds the provided segment map to the provided message map and returns a new
152+
message."
178153
[message segment]
179154
(assoc message :segments (conj (:segments message) segment)))
180155

181156
(defn add-field
182-
"Adds the provided field structure to the provided segment structure
183-
and returns a new segment."
157+
"Adds the provided field map to the provided segment map and returns a new
158+
segment."
184159
[segment field]
185160
(assoc segment :fields (conj (:fields segment) field)))
186161

187162
(defn add-fields
188-
"Adds the provided field structures to the provided segment
189-
structure and returns a new segment."
163+
"Adds the provided field maps to the provided segment map and returns a new
164+
segment."
190165
[segment fields]
191166
(assoc segment :fields (into (:fields segment) fields)))
192167

@@ -251,15 +226,14 @@
251226
true false))
252227

253228
(defn- read-delimiters
254-
"Parses through the delimiters and returns a map with those
255-
delimiters (delimiter-struct)."
229+
"Parses through the delimiters and returns a map with those delimiters."
256230
[reader]
257231

258232
;; loop through the reader, buffer the message id and build up the delimiters
259233
(loop [int-in (.read reader)
260234
buffer []
261235
segment-id nil
262-
delimiters (struct-map delimiters-struct)
236+
delimiters {}
263237
char-index 0]
264238

265239
(cond
@@ -310,14 +284,14 @@
310284
(defn- read-segment-delimiters
311285
"Parsers through the MSH or FHS segment up until the end of the first field (the
312286
list of delimiters) and returns a map with the segment id (:segment-id) and
313-
the the delimiter values (:delimiters with the delimiter-struct)."
287+
the the delimiter values (a map)."
314288
[reader]
315289

316290
;; loop through the reader, buffer the message id and build up the delimiters
317291
(loop [int-in (.read reader)
318292
buffer []
319293
segment-id nil
320-
delimiters (struct-map delimiters-struct)
294+
delimiters {}
321295
char-index 0]
322296

323297
(cond
@@ -546,9 +520,8 @@
546520

547521
(defn- read-msh-fhs-segment
548522
"Adds the \"MSH\" or \"BHS\" segment and its first field of data to the provided
549-
message-struct and returns the new message. This first field will be the list
550-
of delimiters, the provided message must already have a valid set of
551-
delimiters."
523+
message map and returns the new message. This first field will be the list of
524+
delimiters, the provided message must already have a valid set of delimiters."
552525
[segment-id reader message]
553526

554527
;; instantiate our new MSH segment and fill the first field with our
@@ -623,8 +596,7 @@
623596
(recur (.read reader) fields))))))
624597

625598
(defn- parse-message
626-
"Parsers the data read by the reader into a valid HL7 message data
627-
structure (message-struct)."
599+
"Parses the data read by the reader into a valid HL7 message data map."
628600
[reader]
629601

630602
;; loop through the reader and parse the delimiters, the MSH segment
@@ -659,8 +631,7 @@
659631
(recur (.read reader) parsing segment-id message))))
660632

661633
(defn parse
662-
"Reads data from the provided source (a Reader, String, etc.) and
663-
parses that data into a hash-map (hl7-struct) that represents the
664-
content of the message."
634+
"Reads data from the provided source (a Reader, String, etc.) and parses that
635+
data into a map that represents the content of the message."
665636
[message-source]
666637
(parse-message (get-reader message-source)))

0 commit comments

Comments
 (0)