|
21 | 21 | ;; HL7 Messaging v2.x segment delimiter |
22 | 22 | (def SEGMENT-DELIMITER ASCII_CR) |
23 | 23 |
|
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 | | - |
48 | 24 | ;; |
49 | 25 | ;; Emit methods used to output messages |
50 | 26 | ;; |
51 | 27 |
|
52 | 28 | (defn pr-delimiters |
53 | 29 | "Prints an HL7 compatible text representation of the provided |
54 | 30 | 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)))) |
60 | 36 |
|
61 | 37 | (defn- do-pr-content |
62 | 38 | "Returns an HL7 compatible String representation of the provided |
|
151 | 127 | [values])) |
152 | 128 |
|
153 | 129 | (defn create-empty-message |
154 | | - "Returns a new, empty message structure." |
| 130 | + "Returns a new, empty message map." |
155 | 131 | [] |
156 | | - (struct-map message-struct :delimiters nil :segments [])) |
| 132 | + {:delimiters nil :segments []}) |
157 | 133 |
|
158 | 134 | (defn create-message |
159 | | - "Returns a new, empty message structure." |
| 135 | + "Returns a new, empty message map." |
160 | 136 | [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) [])}) |
163 | 139 |
|
164 | 140 | (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." |
166 | 142 | [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) [])}) |
169 | 144 |
|
170 | 145 | (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." |
172 | 147 | [data] |
173 | | - (struct-map field-struct :content (convert-values data))) |
| 148 | + {:content (convert-values data)}) |
174 | 149 |
|
175 | 150 | (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." |
178 | 153 | [message segment] |
179 | 154 | (assoc message :segments (conj (:segments message) segment))) |
180 | 155 |
|
181 | 156 | (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." |
184 | 159 | [segment field] |
185 | 160 | (assoc segment :fields (conj (:fields segment) field))) |
186 | 161 |
|
187 | 162 | (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." |
190 | 165 | [segment fields] |
191 | 166 | (assoc segment :fields (into (:fields segment) fields))) |
192 | 167 |
|
|
251 | 226 | true false)) |
252 | 227 |
|
253 | 228 | (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." |
256 | 230 | [reader] |
257 | 231 |
|
258 | 232 | ;; loop through the reader, buffer the message id and build up the delimiters |
259 | 233 | (loop [int-in (.read reader) |
260 | 234 | buffer [] |
261 | 235 | segment-id nil |
262 | | - delimiters (struct-map delimiters-struct) |
| 236 | + delimiters {} |
263 | 237 | char-index 0] |
264 | 238 |
|
265 | 239 | (cond |
|
310 | 284 | (defn- read-segment-delimiters |
311 | 285 | "Parsers through the MSH or FHS segment up until the end of the first field (the |
312 | 286 | 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)." |
314 | 288 | [reader] |
315 | 289 |
|
316 | 290 | ;; loop through the reader, buffer the message id and build up the delimiters |
317 | 291 | (loop [int-in (.read reader) |
318 | 292 | buffer [] |
319 | 293 | segment-id nil |
320 | | - delimiters (struct-map delimiters-struct) |
| 294 | + delimiters {} |
321 | 295 | char-index 0] |
322 | 296 |
|
323 | 297 | (cond |
|
546 | 520 |
|
547 | 521 | (defn- read-msh-fhs-segment |
548 | 522 | "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." |
552 | 525 | [segment-id reader message] |
553 | 526 |
|
554 | 527 | ;; instantiate our new MSH segment and fill the first field with our |
|
623 | 596 | (recur (.read reader) fields)))))) |
624 | 597 |
|
625 | 598 | (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." |
628 | 600 | [reader] |
629 | 601 |
|
630 | 602 | ;; loop through the reader and parse the delimiters, the MSH segment |
|
659 | 631 | (recur (.read reader) parsing segment-id message)))) |
660 | 632 |
|
661 | 633 | (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." |
665 | 636 | [message-source] |
666 | 637 | (parse-message (get-reader message-source))) |
0 commit comments