Skip to content

Commit

Permalink
Implement Display for Dgc (#36)
Browse files Browse the repository at this point in the history
* Impl display for DgcName

* Implement display

* Add a test. Remove extra blank lines

* Update display

Co-authored-by: Gianmarco Garrisi <gianmarcogarrisi@tutanota.com>
  • Loading branch information
garro95 and Gianmarco Garrisi authored Jan 18, 2022
1 parent c453cd6 commit 926a814
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/dgc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::fmt;

use crate::{Recovery, Test, Vaccination};
use serde::{Deserialize, Deserializer, Serialize};
Expand All @@ -20,6 +21,17 @@ pub struct DgcName {
pub surname_standard: Cow<'static, str>,
}

impl fmt::Display for DgcName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self.forename.as_ref(), self.surname.as_ref()) {
(Some(forename), Some(surname)) => write!(f, "{} {}", forename, surname),
(Some(forename), None) => write!(f, "{}", forename),
(None, Some(surname)) => write!(f, "{}", surname),
(None, None) => write!(f, "{}", self.surname_standard),
}
}
}

fn empty_if_null<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -67,6 +79,24 @@ pub struct Dgc {
pub recoveries: Vec<Recovery>,
}

impl fmt::Display for Dgc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{} ({})", self.name, self.date_of_birth)?;
for test in &self.tests {
writeln!(f, "{}", test)?;
}

for vaccine in &self.vaccines {
writeln!(f, "{}", vaccine)?;
}

for recovery in &self.recoveries {
writeln!(f, "{}", recovery)?;
}
Ok(())
}
}

impl Dgc {
/// Updates all the ids in all the entries with their descriptive counterparts using
/// the official valueset.
Expand Down Expand Up @@ -231,4 +261,38 @@ mod tests {
assert_eq!(cert.tests[0].issuer, "Italy");
assert_eq!(cert.tests[0].id, "01IT053059F7676042D9BEE9F874C4901F9B#3");
}

#[test]
fn test_json_deserialization_and_display() {
let json_data = r#"{
"ver": "1.0.0",
"nam": {
"fn": "Di Caprio",
"fnt": "DI<CAPRIO",
"gn": "Marilù Teresa",
"gnt": "MARILU<TERESA"
},
"dob": "1977-06-16",
"t": [
{
"tg": "840539006",
"tt": "LP6464-4",
"nm": "Roche LightCycler qPCR",
"ma": "1232",
"sc": "2021-05-03T10:27:15Z",
"dr": "2021-05-11T12:27:15Z",
"tr": "260415000",
"tc": "Policlinico Umberto I",
"co": "IT",
"is": "IT",
"ci": "01IT053059F7676042D9BEE9F874C4901F9B#3"
}
]
}
"#;
let mut cert: Dgc = serde_json::from_str(json_data).unwrap();
cert.expand_values();
let display = format!("{}", cert);
assert_eq!(display, "Marilù Teresa Di Caprio (1977-06-16)\nTEST: COVID-19 Not detected on 2021-05-03T10:27:15Z. Issued by Italy\n");
}
}
11 changes: 11 additions & 0 deletions src/recovery.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::lookup_value;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt;

/// A recovery entry.
///
Expand Down Expand Up @@ -38,3 +39,13 @@ impl Recovery {
lookup_value(&mut self.country);
}
}

impl fmt::Display for Recovery {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Recovered from {} on {}. Issued by {}",
self.targeted_disease, self.result_date, self.issuer
)
}
}
11 changes: 11 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::lookup_value;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt;

/// A test entry.
///
Expand Down Expand Up @@ -57,3 +58,13 @@ impl Test {
lookup_value(&mut self.issuer);
}
}

impl fmt::Display for Test {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"TEST: {} {} on {}. Issued by {}",
self.targeted_disease, self.result, self.date_of_collection, self.issuer
)
}
}
11 changes: 11 additions & 0 deletions src/vaccination.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::lookup_value;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt;

/// A vaccination entry.
///
Expand Down Expand Up @@ -50,3 +51,13 @@ impl Vaccination {
lookup_value(&mut self.country);
}
}

impl fmt::Display for Vaccination {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Vaccinated against {} with {} of {} doses on {}. Issued by {}",
self.targeted_disease, self.dose_number, self.total_doses, self.date, self.issuer
)
}
}

0 comments on commit 926a814

Please sign in to comment.