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
use crate::Error;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::str;

/// Orthanc entity kinds (types).
///
/// Orthanc operates with 4 entity kinds, which correspond to the ones, available in DICOM.
/// In descending hierarchical order: Patient, Study, Series, Instance
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub enum EntityKind {
    Patient,
    Study,
    Series,
    Instance,
}

impl TryFrom<bytes::Bytes> for EntityKind {
    type Error = Error;

    fn try_from(value: bytes::Bytes) -> Result<EntityKind, Error> {
        let v = value.to_vec();
        let s = str::from_utf8(&v)?;
        match s {
            "Patient" => Ok(EntityKind::Patient),
            "Study" => Ok(EntityKind::Study),
            "Series" => Ok(EntityKind::Series),
            "Instance" => Ok(EntityKind::Instance),
            _ => Err(Error::new(&format!("Unknown entity kind: {}", s), None)),
        }
    }
}

/// A trait, that implements common methods for all entity kinds
pub trait Entity: serde::de::DeserializeOwned {
    /// Entity kind
    fn kind() -> EntityKind;

    /// The ID of the entity
    fn id(&self) -> &str;

    /// The ID of the entity's parent ([`Patient`] for [`Study`], [`Study`] for [`Series`],
    /// [`Series`] for [`Instance`]. [`None`] if the Entity does not have a parent (e.g. is a
    /// [`Patient`])
    fn parent_id(&self) -> Option<&str> {
        None
    }

    /// Get the value of a DICOM tag from `main_dicom_tags`
    fn main_dicom_tag(&self, tag: &str) -> Option<&str>;

    /// The list of ID of the entity's children (studies for [`Patient`], series for [`Study`],
    /// instances for [`Series`])
    fn children(&self) -> &[String] {
        &[]
    }

    /// Number of children that the entity has
    fn children_len(&self) -> usize {
        0
    }

    /// Index of the instance in the series in case the entity is an [`Instance`], [`None`]
    /// otherwise
    fn index(&self) -> Option<u32> {
        None
    }

    /// Size of the instance file in case the entity is an [`Instance`], [`None`] otherwise
    fn size(&self) -> u64 {
        0
    }

    /// The kind of the entity's parent entity. [`None`] if the entity does not have a parent (e.g.
    /// is a `Patient`)
    fn parent_kind(&self) -> Option<EntityKind> {
        None
    }

    /// The name of the entity's parent entity. [`None`] if the entity does not have a parent (e.g.
    /// is a `Patient`)
    fn parent_kind_name(&self) -> Option<String> {
        self.parent_kind().map(|k| format!("{:?}", k))
    }

    /// Then name of the entity's child entity, pluralized (e.g. "Studies", "Series", "Instances").
    /// [`None`] if the entity does not have children (e.g. is an [`Instance`])
    fn children_kind_name(&self) -> Option<&str> {
        None
    }
}

/// Patient
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct Patient {
    #[serde(rename = "ID")]
    pub id: String,
    pub is_stable: bool,
    #[serde(with = "datetime_format")]
    pub last_update: NaiveDateTime,
    pub main_dicom_tags: HashMap<String, String>,
    pub studies: Vec<String>,
    #[serde(rename = "Type")]
    pub entity: EntityKind,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anonymized_from: Option<String>,
}

impl Entity for Patient {
    /// Returns the [`EntityKind::Patient`] variant
    fn kind() -> EntityKind {
        EntityKind::Patient
    }

    /// The ID of the patient
    fn id(&self) -> &str {
        &self.id
    }

    /// Returns "studies"
    fn children_kind_name(&self) -> Option<&str> {
        Some("Studies")
    }

    /// Get the value of a DICOM tag from `main_dicom_tags`
    fn main_dicom_tag(&self, tag: &str) -> Option<&str> {
        self.main_dicom_tags.get(tag).map(AsRef::as_ref)
    }

    /// Returns the list of IDs of all studies that belong to this patient
    fn children(&self) -> &[String] {
        &self.studies
    }

    /// Number of studies that belong to this patient
    fn children_len(&self) -> usize {
        self.children().len()
    }
}

/// Study
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct Study {
    #[serde(rename = "ID")]
    pub id: String,
    pub is_stable: bool,
    #[serde(with = "datetime_format")]
    pub last_update: NaiveDateTime,
    pub main_dicom_tags: HashMap<String, String>,
    pub parent_patient: String,
    pub patient_main_dicom_tags: HashMap<String, String>,
    pub series: Vec<String>,
    #[serde(rename = "Type")]
    pub entity: EntityKind,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anonymized_from: Option<String>,
}

impl Entity for Study {
    /// Returns the [`EntityKind::Study`] variant
    fn kind() -> EntityKind {
        EntityKind::Study
    }
    /// The ID of the study
    fn id(&self) -> &str {
        &self.id
    }

    /// The ID of the patient, that is the parent of this study
    fn parent_id(&self) -> Option<&str> {
        Some(&self.parent_patient)
    }

    /// Returns "series"
    fn children_kind_name(&self) -> Option<&str> {
        Some("Series")
    }

    /// Get the value of a DICOM tag from `main_dicom_tags`, or if the tag is absent there, from
    /// `patient_main_dicom_tags`.
    fn main_dicom_tag(&self, tag: &str) -> Option<&str> {
        match self.main_dicom_tags.get(tag).map(AsRef::as_ref) {
            Some(v) => Some(v),
            None => self.patient_main_dicom_tags.get(tag).map(AsRef::as_ref),
        }
    }

    /// Returns the list of IDs of all series that belong to this study
    fn children(&self) -> &[String] {
        &self.series
    }

    /// Number of series that belong to this study
    fn children_len(&self) -> usize {
        self.children().len()
    }

    /// Returns [`EntityKind::Patient`]
    fn parent_kind(&self) -> Option<EntityKind> {
        Some(EntityKind::Patient)
    }
}

/// Series
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct Series {
    #[serde(rename = "ID")]
    pub id: String,
    pub status: String,
    pub is_stable: bool,
    #[serde(with = "datetime_format")]
    pub last_update: NaiveDateTime,
    pub main_dicom_tags: HashMap<String, String>,
    pub parent_study: String,
    pub expected_number_of_instances: Option<u32>,
    pub instances: Vec<String>,
    #[serde(rename = "Type")]
    pub entity: EntityKind,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anonymized_from: Option<String>,
}

impl Entity for Series {
    fn kind() -> EntityKind {
        EntityKind::Series
    }
    /// The ID of the series
    fn id(&self) -> &str {
        &self.id
    }

    /// The ID of the study, that is the parent of this series
    fn parent_id(&self) -> Option<&str> {
        Some(&self.parent_study)
    }

    /// Returns "instances"
    fn children_kind_name(&self) -> Option<&str> {
        Some("Instances")
    }

    /// Get the value of a DICOM tag from `main_dicom_tags`
    fn main_dicom_tag(&self, tag: &str) -> Option<&str> {
        self.main_dicom_tags.get(tag).map(AsRef::as_ref)
    }

    /// Returns the list of IDs of all instances that belong to this series
    fn children(&self) -> &[String] {
        &self.instances
    }

    /// Number of instances that belong to this series
    fn children_len(&self) -> usize {
        self.children().len()
    }

    /// Returns [`EntityKind::Study`]
    fn parent_kind(&self) -> Option<EntityKind> {
        Some(EntityKind::Study)
    }
}

/// Instance
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct Instance {
    #[serde(rename = "ID")]
    pub id: String,
    pub main_dicom_tags: HashMap<String, String>,
    pub parent_series: String,
    pub index_in_series: Option<u32>,
    pub file_uuid: String,
    pub file_size: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub modified_from: Option<String>,
    #[serde(rename = "Type")]
    pub entity: EntityKind,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub anonymized_from: Option<String>,
}

impl Entity for Instance {
    /// Returns the [`EntityKind::Instance`] variant
    fn kind() -> EntityKind {
        EntityKind::Instance
    }
    /// The ID of the instance
    fn id(&self) -> &str {
        &self.id
    }

    /// The ID of the series, that is the parent of this instance
    fn parent_id(&self) -> Option<&str> {
        Some(&self.parent_series)
    }

    /// Get the value of a DICOM tag from `main_dicom_tags`
    fn main_dicom_tag(&self, tag: &str) -> Option<&str> {
        self.main_dicom_tags.get(tag).map(AsRef::as_ref)
    }

    /// Returns [`EntityKind::Series`]
    fn parent_kind(&self) -> Option<EntityKind> {
        Some(EntityKind::Series)
    }

    /// Index of the instance in the series
    fn index(&self) -> Option<u32> {
        self.index_in_series
    }

    /// Size of the instance file
    fn size(&self) -> u64 {
        self.file_size
    }
}

mod datetime_format {
    use chrono::NaiveDateTime;
    use serde::{self, Deserialize, Deserializer, Serializer};

    const FORMAT: &str = "%Y%m%dT%H%M%S";

    pub fn serialize<S>(date: &NaiveDateTime, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = format!("{}", date.format(FORMAT));
        serializer.serialize_str(&s)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDateTime, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        NaiveDateTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use maplit::hashmap;

    #[test]
    fn test_entity_kind_try_from_bytes() {
        assert_eq!(
            EntityKind::try_from(bytes::Bytes::from_static(b"Patient")).unwrap(),
            EntityKind::Patient
        );
        assert_eq!(
            EntityKind::try_from(bytes::Bytes::from_static(b"Study")).unwrap(),
            EntityKind::Study
        );
        assert_eq!(
            EntityKind::try_from(bytes::Bytes::from_static(b"Series")).unwrap(),
            EntityKind::Series
        );
        assert_eq!(
            EntityKind::try_from(bytes::Bytes::from_static(b"Instance")).unwrap(),
            EntityKind::Instance
        );
        assert_eq!(
            EntityKind::try_from(bytes::Bytes::from_static(b"Foobar")).unwrap_err(),
            Error::new("Unknown entity kind: Foobar", None)
        );
    }

    #[test]
    fn test_entity_trait_patient() {
        assert_eq!(Patient::kind(), EntityKind::Patient);

        let patient = Patient {
            id: "f88cbd3f-a00dfc59-9ca1ac2d-7ce9851a-40e5b493".to_string(),
            is_stable: true,
            last_update: NaiveDate::from_ymd(2020, 1, 1).and_hms(15, 46, 17),
            main_dicom_tags: hashmap! {
                "PatientName".to_string() => "Rick Sanchez".to_string(),
            },
            studies: ["e8cafcbe-caf08c39-6e205f15-18554bb8-b3f9ef04".to_string()].to_vec(),
            entity: EntityKind::Patient,
            anonymized_from: None,
        };

        assert_eq!(patient.id(), "f88cbd3f-a00dfc59-9ca1ac2d-7ce9851a-40e5b493");
        assert_eq!(patient.parent_id(), None);
        assert_eq!(patient.main_dicom_tag("PatientName"), Some("Rick Sanchez"));
        assert_eq!(
            patient.children(),
            ["e8cafcbe-caf08c39-6e205f15-18554bb8-b3f9ef04".to_string()]
        );
        assert_eq!(patient.children_len(), 1);
        assert_eq!(patient.index(), None);
        assert_eq!(patient.size(), 0);
        assert_eq!(patient.parent_kind(), None);
        assert_eq!(patient.parent_kind_name(), None);
        assert_eq!(patient.children_kind_name(), Some("Studies"));
    }

    #[test]
    fn test_entity_trait_study() {
        assert_eq!(Study::kind(), EntityKind::Study);

        let study = Study {
            id: "63bf5d42-b5382159-01971752-e0ceea3d-399bbca5".to_string(),
            is_stable: true,
            last_update: NaiveDate::from_ymd(2020, 8, 30).and_hms(19, 11, 09),
            main_dicom_tags: hashmap! {
                "AccessionNumber".to_string() => "foobar".to_string(),
            },
            parent_patient: "7e43f8d3-e50280e6-470079e9-02241af1-d286bdbe".to_string(),
            patient_main_dicom_tags: hashmap! {
                "PatientName".to_string() => "Rick Sanchez".to_string(),
            },
            series: [
                "cd00fffc-db25be29-0c6da430-c56796a5-ba06933c".to_string(),
                "2ab7dbe7-f1a18a78-86145443-18a8ff93-0b65f2b2".to_string(),
            ]
            .to_vec(),
            entity: EntityKind::Study,
            anonymized_from: None,
        };

        assert_eq!(study.id(), "63bf5d42-b5382159-01971752-e0ceea3d-399bbca5");
        assert_eq!(
            study.parent_id(),
            Some("7e43f8d3-e50280e6-470079e9-02241af1-d286bdbe")
        );
        assert_eq!(study.main_dicom_tag("AccessionNumber"), Some("foobar"));
        assert_eq!(study.main_dicom_tag("PatientName"), Some("Rick Sanchez"));
        assert_eq!(
            study.children(),
            [
                "cd00fffc-db25be29-0c6da430-c56796a5-ba06933c".to_string(),
                "2ab7dbe7-f1a18a78-86145443-18a8ff93-0b65f2b2".to_string()
            ]
        );
        assert_eq!(study.children_len(), 2);
        assert_eq!(study.index(), None);
        assert_eq!(study.size(), 0);
        assert_eq!(study.parent_kind(), Some(EntityKind::Patient));
        assert_eq!(study.parent_kind_name(), Some("Patient".to_string()));
        assert_eq!(study.children_kind_name(), Some("Series"));
    }

    #[test]
    fn test_entity_trait_series() {
        assert_eq!(Series::kind(), EntityKind::Series);

        let series = Series {
            id: "cd00fffc-db25be29-0c6da430-c56796a5-ba06933c".to_string(),
            status: "Unknown".to_string(),
            is_stable: true,
            last_update: NaiveDate::from_ymd(2020, 8, 30).and_hms(19, 11, 09),
            main_dicom_tags: hashmap! {
                "BodyPartExamined".to_string() => "ABDOMEN".to_string(),
            },
            parent_study: "63bf5d42-b5382159-01971752-e0ceea3d-399bbca5".to_string(),
            expected_number_of_instances: Some(17),
            instances: [
                "556530b5-de7c487b-110b9d0e-12cfdbb9-f06b546e".to_string(),
                "c46605db-836489fa-cb55fbbc-13c8a913-b0bad6ac".to_string(),
            ]
            .to_vec(),
            entity: EntityKind::Series,
            anonymized_from: None,
        };

        assert_eq!(series.id(), "cd00fffc-db25be29-0c6da430-c56796a5-ba06933c");
        assert_eq!(
            series.parent_id(),
            Some("63bf5d42-b5382159-01971752-e0ceea3d-399bbca5")
        );
        assert_eq!(series.main_dicom_tag("BodyPartExamined"), Some("ABDOMEN"));
        assert_eq!(
            series.children(),
            [
                "556530b5-de7c487b-110b9d0e-12cfdbb9-f06b546e".to_string(),
                "c46605db-836489fa-cb55fbbc-13c8a913-b0bad6ac".to_string()
            ]
        );
        assert_eq!(series.children_len(), 2);
        assert_eq!(series.index(), None);
        assert_eq!(series.size(), 0);
        assert_eq!(series.parent_kind(), Some(EntityKind::Study));
        assert_eq!(series.parent_kind_name(), Some("Study".to_string()));
        assert_eq!(series.children_kind_name(), Some("Instances"));
    }

    #[test]
    fn test_entity_trait_instance() {
        assert_eq!(Instance::kind(), EntityKind::Instance);

        let instance = Instance {
            id: "29fa4d9d-51a69d1d-70e2b29a-fd824316-50850d0c".to_string(),
            main_dicom_tags: hashmap! {
                "SOPInstanceUID".to_string() => "1.2.3.4.5.6789".to_string(),
            },
            parent_series: "82081568-b6f8f4e6-ced76876-6504da25-ed0dfe03".to_string(),
            index_in_series: Some(13),
            file_uuid: "d8c5eff3-986c-4fe4-b06e-7e52b2a4238e".to_string(),
            file_size: 139402,
            modified_from: Some("22c54cb6-28302a69-3ff454a3-676b98f4-b84cd80a".to_string()),
            entity: EntityKind::Instance,
            anonymized_from: None,
        };

        assert_eq!(
            instance.id(),
            "29fa4d9d-51a69d1d-70e2b29a-fd824316-50850d0c"
        );
        assert_eq!(
            instance.parent_id(),
            Some("82081568-b6f8f4e6-ced76876-6504da25-ed0dfe03")
        );
        assert_eq!(
            instance.main_dicom_tag("SOPInstanceUID"),
            Some("1.2.3.4.5.6789")
        );
        let ch: &[String] = &[];
        assert_eq!(instance.children(), ch);
        assert_eq!(instance.children_len(), 0);
        assert_eq!(instance.index(), Some(13));
        assert_eq!(instance.size(), 139402);
        assert_eq!(instance.parent_kind(), Some(EntityKind::Series));
        assert_eq!(instance.parent_kind_name(), Some("Series".to_string()));
        assert_eq!(instance.children_kind_name(), None);
    }
}