-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodelist.rs
More file actions
1288 lines (1080 loc) · 43.3 KB
/
codelist.rs
File metadata and controls
1288 lines (1080 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This file contains the core functionality for the codelist
use std::{
collections::{BTreeMap, HashSet},
str::FromStr,
};
use csv::Writer;
use serde::{Deserialize, Serialize};
use crate::{
codelist_options::CodeListOptions, errors::CodeListError, metadata::Metadata,
types::CodeListType,
logging::{LogEntry, LogType},
};
use crate::logging::{AddType, CodelistLog, RemoveType, EditType};
/// Struct to represent a codelist
///
/// # Fields
/// * `name` - The name of the codelist
/// * `entries` - The set of code entries
/// * `codelist_type` - The type of codelist
/// * `metadata` - Metadata about the codelist
/// * `log` - log of anything that happened during the codelist creation
/// * `codelist_options` - Options for the codelist
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CodeList {
pub name: String,
pub entries: BTreeMap<String, (Option<String>, Option<String>)>,
pub codelist_type: CodeListType,
pub metadata: Metadata,
pub log: CodelistLog,
pub codelist_options: CodeListOptions,
}
impl CodeList {
/// Create a new CodeList
///
/// # Arguments
/// * `name` - The name of the codelist
/// * `codelist_type` - The type of codelist
/// * `metadata` - Metadata describing the codelist
/// * `log` - log of anything that happened during the codelist creation
/// * `options` - Customisable options for the codelist
///
/// # Returns
/// * `CodeList` - The new CodeList
pub fn new(
name: String,
codelist_type: CodeListType,
metadata: Metadata,
options: Option<CodeListOptions>,
) -> Self {
CodeList {
name,
entries: BTreeMap::new(),
codelist_type,
metadata,
log: CodelistLog::default(),
codelist_options: options.unwrap_or_default(),
}
}
/// Get the type of the codelist
///
/// # Returns
/// * `&CodeListType` - The type of the codelist
pub fn codelist_type(&self) -> &CodeListType {
&self.codelist_type
}
/// Add an entry to the codelist
///
/// # Arguments
/// * `code` - The code to add
/// * `term` - The optional term to add
/// * `comment` - The optional comment to add
pub fn add_entry(
&mut self,
code: String,
term: Option<String>,
comment: Option<String>,
) -> Result<(), CodeListError> {
if code.is_empty() {
return Err(CodeListError::empty_code("Empty code supplied"));
}
let log_msg = format!(
"Added entry with code: {code}, term: {:?}, comment: {:?}",
term, comment
);
self.entries.insert(code.clone(), (term, comment));
self.log.add_entry(LogEntry::new(LogType::Add(AddType::Code), log_msg));
Ok(())
}
/// Remove an entry from the codelist
///
/// # Arguments
/// * `code` - The code to remove
///
/// # Errors
/// * `CodeListError::EntryNotFound` - If the entry to be removed is not
/// found
pub fn remove_entry(&mut self, code: &str) -> Result<(), CodeListError> {
let removed = self.entries.remove(code);
if removed.is_some() {
self.log.add_entry(LogEntry::new(
LogType::Remove(RemoveType::Code),
format!("Removed entry with code: {code}"),
));
Ok(())
} else {
Err(CodeListError::entry_not_found(code))
}
}
/// Get the full entries of the codelist, including code, optional term and
/// optional comment
///
/// # Returns
/// * `&BTreeMap<String, (Option<String>, Option<String>)` - The entries of
/// the codelist
pub fn full_entries(&self) -> &BTreeMap<String, (Option<String>, Option<String>)> {
&self.entries
}
/// Get the code and term of the codelist
///
/// # Returns
/// * `BTreeMap<&String, Option<&String>>` - The codes and terms of the
/// codelist
pub fn code_term_entries(&self) -> BTreeMap<&String, Option<&String>> {
self.entries.iter().map(|(code, (term_opt, _))| (code, term_opt.as_ref())).collect()
}
/// Get the codes of the codelist
///
/// # Returns
/// * `HashSet<&String>` - The codes of the codelist
pub fn codes(&self) -> HashSet<&String> {
self.entries.keys().collect()
}
/// Save the codelist entries to a CSV file
///
/// # Arguments
/// * `file_path` - The path to the file to save the codelist entries to
///
/// # Errors
/// * `CodeListError::IOError` - If an error occurs when writing to the file
pub fn save_to_csv(&mut self, file_path: &str) -> Result<(), CodeListError> {
let mut wtr = Writer::from_path(file_path)?;
// use column names from options
wtr.write_record([
&self.codelist_options.code_field_name,
&self.codelist_options.term_field_name,
])?;
for (code, (term, _)) in self.entries.iter() {
wtr.write_record([code, term.as_deref().unwrap_or("")])?;
}
wtr.flush()?;
self.log.add_entry(LogEntry::new(
LogType::Save,
format!("Saved codelist to CSV file: {file_path}"),
));
Ok(())
}
/// Save the codelist struct to a JSON file
///
/// # Arguments
/// * `file_path` - The path to the file to save the codelist struct to
///
/// # Errors
/// * `CodeListError::IOError` - If an error occurs when writing to the file
pub fn save_to_json(&mut self, file_path: &str) -> Result<(), CodeListError> {
let json = serde_json::to_string_pretty(self)?;
std::fs::write(file_path, json)?;
self.log.add_entry(LogEntry::new(
LogType::Save,
format!("Saved codelist to JSON file: {file_path}"),
));
Ok(())
}
/// Save the log to a file
///
/// # Arguments
/// * `file_path` - The path to the file to save the log to
///
/// # Errors
/// * `CodeListError::IOError` - If an error occurs when writing to the file
pub fn save_log(&self, file_path: &str) -> Result<(), CodeListError> {
self.log.write_to_file(file_path).map_err(|e| {
CodeListError::IOError(e)
})
}
/// Add a log message to the codelist
///
/// # Arguments
/// * `message` - The message to add to the log
pub fn add_log(&mut self, message: String) {
self.log.add_entry(LogEntry::new(
LogType::Note,
message.clone(),
));
}
/// Get the metadata
///
/// # Returns
/// * `&Metadata` - The metadata
pub fn metadata(&self) -> &Metadata {
&self.metadata
}
/// Add a comment to a code entry
///
/// # Arguments
/// * `code` - The code of the entry to add the comment to
/// * `comment` - The comment to add
///
/// # Returns
/// * `Result<(), CodeListError>`
///
/// # Errors
/// * `CodeListError::CodeEntryCommentAlreadyExists` - If the comment
/// already exists
pub fn add_comment(&mut self, code: String, comment: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((_, comment_opt)) => {
if comment_opt.is_some() {
Err(CodeListError::code_entry_comment_already_exists(
code,
"Please use update comment instead",
))
} else {
self.log.add_entry(LogEntry::new(
LogType::Add(AddType::Comment),
format!("Added comment to entry with code: {code}, comment: {comment}"),
));
*comment_opt = Some(comment);
Ok(())
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Update the comment for the code entry
///
/// # Arguments
/// * `code` - The code of the entry to update the comment for
/// * `comment` - The comment to update
///
/// # Returns
/// * `Result<(), CodeListError>`
///
/// # Errors
/// * `CodeListError::CodeEntryCommentDoesNotExist` - If the comment does
/// not exist
pub fn update_comment(&mut self, code: String, comment: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((_, comment_opt)) => {
if comment_opt.is_some() {
// Move `comment` into a temporary variable so we can use it for both logging and assignment
let log_msg = format!(
"Updated comment for entry with code: {code}, comment: {:?}",
comment
);
*comment_opt = Some(comment);
self.log.add_entry(LogEntry::new(LogType::Edit(EditType::Comment), log_msg));
Ok(())
} else {
Err(CodeListError::code_entry_comment_does_not_exist(
code,
"Please use add comment instead",
))
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Remove the comment from the code entry
///
/// # Arguments
/// * `code` - The code of the entry to remove the comment from
///
/// # Returns
/// * `Result<(), CodeListError>`
///
/// # Errors
/// * `CodeListError::CodeEntryCommentDoesNotExist` - If there is no comment
/// to remove
pub fn remove_comment(&mut self, code: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((_, comment_opt)) => {
if comment_opt.is_some() {
*comment_opt = None;
self.log.add_entry(LogEntry::new(
LogType::Remove(RemoveType::Comment),
format!("Removed comment from entry with code: {code}"),
));
Ok(())
} else {
Err(CodeListError::code_entry_comment_does_not_exist(
code,
"Unable to remove comment",
))
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Add a term to the code entry
///
/// # Arguments
/// * `code` - The code of the entry to add the term to
/// * `term` - The term to add
///
/// # Errors
/// * `CodeListError::CodeEntryTermAlreadyExists` - If the term already
/// exists
pub fn add_term(&mut self, code: String, term: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((term_opt, _)) => {
if term_opt.is_some() {
Err(CodeListError::code_entry_term_already_exists(
code,
"Please use update term instead",
))
} else {
*term_opt = Some(term);
Ok(())
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Update the term for the code entry
///
/// # Arguments
/// * `code` - The code of the entry to update the term for
/// * `term` - The term to update
///
/// # Errors
/// * `CodeListError::CodeEntryTermDoesNotExist` - If the term does not
/// exist
pub fn update_term(&mut self, code: String, term: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((term_opt, _)) => {
if term_opt.is_some() {
*term_opt = Some(term);
Ok(())
} else {
Err(CodeListError::code_entry_term_does_not_exist(
code,
"Please use add term instead",
))
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Remove the term from the code entry
///
/// # Arguments
/// * `code` - The code of the entry to remove the term from
///
/// # Errors
/// * `CodeListError::CodeEntryTermDoesNotExist` - If there is no term to
/// remove
pub fn remove_term(&mut self, code: String) -> Result<(), CodeListError> {
match self.entries.get_mut(&code) {
Some((term_opt, _)) => {
if term_opt.is_some() {
*term_opt = None;
Ok(())
} else {
Err(CodeListError::code_entry_term_does_not_exist(
code,
"Unable to remove term",
))
}
}
None => Err(CodeListError::entry_not_found(&code)),
}
}
/// Truncate codelist entries to 3 digits
///
/// # Arguments
/// * `term_management` - How to handle ambiguous terms
///
/// # Errors
/// * `CodeListError::CodeListNotTruncatable` - If the codelist is not ICD10
pub fn truncate_to_3_digits(
&mut self,
term_management: TermManagement,
) -> Result<(), CodeListError> {
if !self.codelist_type.is_truncatable() {
return Err(CodeListError::CodeListNotTruncatable {
codelist_type: self.codelist_type.to_string(),
});
}
// Keep track of all the three-digit codes
let mut threes = self
.entries
.keys()
.filter(|code| code.len() == 3)
.cloned()
.collect::<HashSet<String>>();
let mut adds = vec![];
let mut removes = vec![];
for (code, (term, _)) in self.entries.iter() {
// Codes of 3 or less will not be truncated
if code.len() <= 3 {
continue;
}
// We'll remove this one later
removes.push(code.clone());
// truncate the entry's code
let truncated_code = code[..3].to_string();
// If we already have this one, then go on to the next one
if threes.contains(&truncated_code) {
continue;
}
// Note that we've seen it
threes.insert(truncated_code.clone());
// The term and comment that goes with it to make the
// entry depends on the term_management
let (term, comment) = match term_management {
TermManagement::DropTerm => {
(None, Some("Truncated to 3 digits, term discarded".to_string()))
}
TermManagement::First => (
term.clone(),
Some(format!("{code} truncated to 3 digits, term first encountered")),
),
};
// We'll add this one later
adds.push((truncated_code, term, comment));
}
// Add the new three-digit codes
for (code, term, comment) in &adds {
self.add_entry(code.clone(), term.clone(), comment.clone())?;
}
// Remove the longer codes
for code in &removes {
self.remove_entry(code)?;
}
Ok(())
}
/// Add X to codelist entries that are 3 digits
///
/// # Errors
/// * `CodeListError::CodeListNotXAddable` - If the codelist is not ICD10
pub fn add_x_codes(&mut self) -> Result<(), CodeListError> {
if !self.codelist_type.is_x_addable() {
return Err(CodeListError::CodeListNotXAddable {
codelist_type: self.codelist_type.to_string(),
});
}
// Keep track of all the four-digit codes ending in X
let mut exes = self
.entries
.keys()
.filter(|code| code.len() == 4 && code.ends_with("X"))
.cloned()
.collect::<HashSet<String>>();
let mut adds = vec![];
for (code, (term, comment)) in &self.entries {
if code.len() == 3 {
let mut new_code = code.clone();
new_code.push('X');
if exes.contains(&new_code) {
continue;
}
exes.insert(new_code.clone());
adds.push((new_code, term.clone(), comment.clone()));
}
}
for (code, term, comment) in &adds {
self.add_entry(code.clone(), term.clone(), comment.clone())?;
}
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum TermManagement {
DropTerm,
First,
}
/// Map Term Management from string
impl FromStr for TermManagement {
type Err = CodeListError;
/// Map TermManagement from a string
fn from_str(s: &str) -> Result<Self, CodeListError> {
match s.to_lowercase().as_str() {
"drop_term" => Ok(TermManagement::DropTerm),
"first" => Ok(TermManagement::First),
_ => Err(CodeListError::TermManagementNotKnown { term_management: s.to_string() }),
}
}
}
#[cfg(test)]
mod tests {
use chrono::Utc;
use indexmap::IndexSet;
use tempfile::TempDir;
use super::*;
use crate::metadata::{Metadata, Source};
// Helper function to create a test codelist with two entries, default options
// and test metadata
fn create_test_codelist() -> Result<CodeList, CodeListError> {
let mut codelist = CodeList::new(
"test_codelist".to_string(),
CodeListType::ICD10,
Metadata::default(),
None,
);
codelist.add_entry("R65.2".to_string(), None, None)?;
codelist.add_entry(
"A48.51".to_string(),
Some("Infant botulism".to_string()),
Some("test comment".to_string()),
)?;
Ok(codelist)
}
// helper function to get the time difference between the current time and the
// given date
fn get_time_difference(date: chrono::DateTime<Utc>) -> i64 {
let now = chrono::Utc::now();
(date - now).num_milliseconds().abs()
}
#[test]
fn test_create_codelist_default_options() -> Result<(), CodeListError> {
let codelist = create_test_codelist()?;
assert_eq!(codelist.codelist_type(), &CodeListType::ICD10);
assert_eq!(codelist.entries.len(), 2);
assert_eq!(&codelist.codelist_options, &CodeListOptions::default());
assert_eq!(codelist.metadata().provenance.source, Source::ManuallyCreated);
let time_difference = get_time_difference(codelist.metadata().provenance.created_date);
assert!(time_difference < 1000);
let time_difference =
get_time_difference(codelist.metadata().provenance.last_modified_date);
assert!(time_difference < 1000);
assert_eq!(codelist.metadata().provenance.contributors, IndexSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.tags, HashSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.usage, HashSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.license, None);
assert_eq!(codelist.metadata().purpose_and_context.purpose, None);
assert_eq!(codelist.metadata().purpose_and_context.target_audience, None);
assert_eq!(codelist.metadata().purpose_and_context.use_context, None);
assert!(!codelist.metadata().validation_and_review.reviewed);
assert_eq!(codelist.metadata().validation_and_review.reviewer, None);
assert_eq!(codelist.metadata().validation_and_review.review_date, None);
assert_eq!(codelist.metadata().validation_and_review.status, None);
assert_eq!(codelist.metadata().validation_and_review.validation_notes, None);
Ok(())
}
#[test]
fn test_create_codelist_custom_options() -> Result<(), CodeListError> {
let codelist_options = CodeListOptions {
allow_duplicates: true,
code_column_name: "test_code".to_string(),
term_column_name: "test_term".to_string(),
code_field_name: "test_code".to_string(),
term_field_name: "test_term".to_string(),
};
let codelist = CodeList::new(
"test_codelist".to_string(),
CodeListType::ICD10,
Default::default(),
Some(codelist_options),
);
assert!(codelist.codelist_options.allow_duplicates);
assert_eq!(codelist.codelist_options.code_field_name, "test_code".to_string());
assert_eq!(codelist.codelist_options.term_field_name, "test_term".to_string());
assert_eq!(codelist.codelist_options.code_column_name, "test_code".to_string());
assert_eq!(codelist.codelist_options.term_column_name, "test_term".to_string());
assert_eq!(codelist.metadata().provenance.source, Source::ManuallyCreated);
let time_difference = get_time_difference(codelist.metadata().provenance.created_date);
assert!(time_difference < 1000);
let time_difference =
get_time_difference(codelist.metadata().provenance.last_modified_date);
assert!(time_difference < 1000);
assert_eq!(codelist.metadata().provenance.contributors, IndexSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.tags, HashSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.usage, HashSet::new());
assert_eq!(codelist.metadata().categorisation_and_usage.license, None);
assert_eq!(codelist.metadata().purpose_and_context.purpose, None);
assert_eq!(codelist.metadata().purpose_and_context.target_audience, None);
assert_eq!(codelist.metadata().purpose_and_context.use_context, None);
assert!(!codelist.metadata().validation_and_review.reviewed);
assert_eq!(codelist.metadata().validation_and_review.reviewer, None);
assert_eq!(codelist.metadata().validation_and_review.review_date, None);
assert_eq!(codelist.metadata().validation_and_review.status, None);
assert_eq!(codelist.metadata().validation_and_review.validation_notes, None);
assert_eq!(codelist.codelist_type(), &CodeListType::ICD10);
assert_eq!(codelist.entries.len(), 0);
assert_eq!(codelist.log.len(), 0);
Ok(())
}
#[test]
fn test_duplicate_entries() -> Result<(), CodeListError> {
let mut codelist = CodeList::new(
"test_codelist".to_string(),
CodeListType::ICD10,
Default::default(),
None,
);
codelist.add_entry("R65.2".to_string(), Some("Severe sepsis".to_string()), None)?;
codelist.add_entry("R65.2".to_string(), Some("Severe sepsis".to_string()), None)?;
assert_eq!(codelist.entries.len(), 1);
Ok(())
}
#[test]
fn test_get_codelist_type() -> Result<(), CodeListError> {
let codelist = create_test_codelist()?;
assert_eq!(codelist.codelist_type(), &CodeListType::ICD10);
Ok(())
}
#[test]
fn test_add_entry() -> Result<(), CodeListError> {
let codelist = create_test_codelist()?;
let code1 = "R65.2".to_string();
let code2 = "A48.51".to_string();
let first_entry = codelist.entries.get(&code1);
let (term1, comment1) =
first_entry.ok_or_else(|| CodeListError::entry_not_found(&code1))?;
let second_entry = codelist.entries.get(&code2);
let (term2, comment2) =
second_entry.ok_or_else(|| CodeListError::entry_not_found(&code1))?;
assert!(first_entry.is_some());
assert!(comment1.is_none());
assert_eq!(term1.as_deref(), None);
assert!(second_entry.is_some());
assert_eq!(comment2.as_deref(), Some("test comment"));
assert_eq!(term2.as_deref(), Some("Infant botulism"));
Ok(())
}
#[test]
fn test_add_entry_with_empty_code_returns_error() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let error = codelist.add_entry("".to_string(), None, None).unwrap_err();
let error_string = error.to_string();
assert_eq!(&error_string, "Empty code: Empty code supplied");
Ok(())
}
#[test]
fn test_remove_entry_that_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
codelist.remove_entry("R65.2")?;
let entry = codelist.entries.get("A48.51");
let (term, comment) = entry.ok_or_else(|| CodeListError::entry_not_found("A48.51"))?;
assert_eq!(codelist.entries.len(), 1);
assert!(entry.is_some());
assert_eq!(comment.as_deref(), Some("test comment"));
assert_eq!(term.as_deref(), Some("Infant botulism"));
Ok(())
}
#[test]
fn test_remove_entry_that_doesnt_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let error = codelist.remove_entry("A48.52").unwrap_err();
assert!(matches!(error, CodeListError::EntryNotFound { code } if code == "A48.52"));
assert_eq!(codelist.entries.len(), 2);
Ok(())
}
#[test]
fn test_get_code_term_entries() -> Result<(), CodeListError> {
let codelist = create_test_codelist()?;
let entries = codelist.code_term_entries();
let expected_term = "Infant botulism".to_string();
let key1 = "R65.2".to_string();
let key2 = "A48.51".to_string();
assert_eq!(entries.len(), 2);
assert_eq!(entries.get(&key1), Some(&None));
assert_eq!(entries.get(&key2), Some(&Some(&expected_term)));
Ok(())
}
#[test]
fn test_get_codes() -> Result<(), CodeListError> {
let codelist = create_test_codelist()?;
let codes = codelist.codes();
let test_code_1 = "R65.2".to_string();
let test_code_2 = "A48.51".to_string();
assert_eq!(codes.len(), 2);
assert!(codes.contains(&test_code_1));
assert!(codes.contains(&test_code_2));
Ok(())
}
#[test]
fn test_save_to_csv() -> Result<(), CodeListError> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test.csv");
let file_path_str = file_path
.to_str()
.ok_or(CodeListError::invalid_file_path("Path contains invalid Unicode characters"))?;
let mut codelist = create_test_codelist()?;
codelist.save_to_csv(file_path_str)?;
let content = std::fs::read_to_string(file_path_str)?;
let lines: Vec<&str> = content.lines().collect();
let mut data_lines = lines[1..].to_vec();
data_lines.sort();
assert_eq!(lines[0], "code,term");
assert_eq!(data_lines, vec!["A48.51,Infant botulism", "R65.2,"]);
Ok(())
}
#[test]
fn test_save_to_json() -> Result<(), CodeListError> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test_codelist.json");
let file_path_str = file_path
.to_str()
.ok_or(CodeListError::invalid_file_path("Path contains invalid Unicode characters"))?;
let mut original_codelist = create_test_codelist()?;
original_codelist.save_to_json(file_path_str)?;
let json_content = std::fs::read_to_string(file_path_str)?;
let loaded_codelist: CodeList = serde_json::from_str(&json_content)?;
assert_eq!(original_codelist.name, loaded_codelist.name);
assert_eq!(original_codelist.codelist_type, loaded_codelist.codelist_type);
assert_eq!(original_codelist.entries, loaded_codelist.entries);
// Note we are not testing log here because saving a log in of itself creates
// a new log entry, and since we are writing to a temporary file, the log will
// not match the original log and is not guessable.
Ok(())
}
#[test]
fn test_save_log() -> Result<(), CodeListError> {
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("test.txt");
let file_path_str = file_path
.to_str()
.ok_or(CodeListError::invalid_file_path("Path contains invalid Unicode characters"))?;
let mut codelist = create_test_codelist()?;
codelist.add_log("Test log message".to_string());
codelist.save_log(file_path_str)?;
let content = std::fs::read_to_string(file_path_str)?;
assert!(content.contains("Add(Code): Added entry with code: R65.2, term: None, comment: None"));
assert!(content.contains("Add(Code): Added entry with code: A48.51, term: Some(\"Infant botulism\"), comment: Some(\"test comment\")"));
assert!(content.contains("Note: Test log message"));
Ok(())
}
#[test]
fn test_get_metadata() {
let metadata: Metadata = Default::default();
let codelist =
CodeList::new("test".to_string(), CodeListType::ICD10, metadata.clone(), None);
assert_eq!(codelist.metadata(), &metadata);
}
#[test]
fn test_add_comment_when_comment_does_not_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let expected_comment = "test comment";
let key = "R65.2";
codelist.add_comment(key.to_string(), expected_comment.to_string())?;
let entry = codelist.entries.get(key);
let (_, actual_comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_comment.as_deref(), Some(expected_comment));
Ok(())
}
#[test]
fn test_add_comment_when_comment_already_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let key = "A48.51";
let error = codelist.add_comment(key.to_string(), "test".to_string()).unwrap_err();
let error_string = error.to_string();
let entry = codelist.entries.get(key);
let (_, actual_comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_comment.as_deref(), Some("test comment"));
assert_eq!(
&error_string,
"Comment for entry with code A48.51 already exists. Please use update comment instead."
);
Ok(())
}
#[test]
fn test_update_comment_when_comment_already_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let expected_comment = "new test comment";
let key = "A48.51";
codelist.update_comment(key.to_string(), expected_comment.to_string())?;
let entry = codelist.entries.get(key);
let (_, actual_comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_comment.as_deref(), Some(expected_comment));
Ok(())
}
#[test]
fn test_update_comment_when_comment_does_not_already_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let key = "R65.2";
let error = codelist.update_comment(key.to_string(), "test".to_string()).unwrap_err();
let error_string = error.to_string();
let entry = codelist.entries.get(key);
let (_, actual_comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_comment.as_deref(), None);
assert_eq!(
&error_string,
"Comment for entry with code R65.2 does not exist. Please use add comment instead."
);
Ok(())
}
#[test]
fn test_remove_comment_when_comment_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let key = "A48.51";
codelist.remove_comment(key.to_string())?;
let entry = codelist.entries.get(key);
let (_, comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(comment.as_deref(), None);
Ok(())
}
#[test]
fn test_remove_comment_when_comment_does_not_already_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let key = "R65.2";
let error = codelist.remove_comment(key.to_string()).unwrap_err();
let error_string = error.to_string();
let entry = codelist.entries.get(key);
let (_, actual_comment) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_comment.as_deref(), None);
assert_eq!(
&error_string,
"Comment for entry with code R65.2 does not exist. Unable to remove comment."
);
Ok(())
}
#[test]
fn test_add_term_when_term_does_not_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let expected_term = "test term";
let key = "R65.2";
codelist.add_term(key.to_string(), expected_term.to_string())?;
let entry = codelist.entries.get(key);
let (actual_term, _) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_term.as_deref(), Some(expected_term));
Ok(())
}
#[test]
fn test_add_term_when_term_already_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let key = "A48.51";
let error = codelist.add_term(key.to_string(), "test".to_string()).unwrap_err();
let error_string = error.to_string();
let entry = codelist.entries.get(key);
let (actual_term, _) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_term.as_deref(), Some("Infant botulism"));
assert_eq!(
&error_string,
"Term for entry with code A48.51 already exists. Please use update term instead."
);
Ok(())
}
#[test]
fn test_update_term_when_term_already_exists() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;
let expected_term = "new test term";
let key = "A48.51";
codelist.update_term(key.to_string(), expected_term.to_string())?;
let entry = codelist.entries.get(key);
let (actual_term, _) = entry.ok_or_else(|| CodeListError::entry_not_found(key))?;
assert_eq!(actual_term.as_deref(), Some(expected_term));
Ok(())
}
#[test]
fn test_update_term_when_term_does_not_already_exist() -> Result<(), CodeListError> {
let mut codelist = create_test_codelist()?;