rust-italia / dgc

A parser and validator for the EU Digital Green Certificate (dgc) a.k.a. greenpass
https://github.com/rust-italia/dgc
MIT License
26 stars 11 forks source link

Implement a method to extract the current test/vaccine/recovery data from a `DgcContainer` #10

Open lmammino opened 2 years ago

lmammino commented 2 years ago

First of all, it would be convenient to have a way to distinguish between test, recovery and vaccine data through an enum. This might look like the following code:

#[derive(Debug)]
pub enum CertData<'a> {
    RecoveryData(&'a Recovery),
    TestData(&'a Test),
    VaccinationData(&'a Vaccination),
}

Once we have this, DgcContainer could implement a method like get_first_cert() which might work as follows (untested):

impl DgcContainer {
    pub fn get_first_cert(&self) -> Option<CertData<'_>> {
        for (_, certs) in self.certs.iter() {
            if let Some(v) = certs.v.first() {
                return Some(CertData::VaccinationData(v));
            }

            if let Some(t) = certs.t.first() {
                return Some(CertData::TestData(t));
            }

            if let Some(r) = certs.r.first() {
                return Some(CertData::RecoveryData(r));
            }
        }

        None
    }
}

A structurally valid Dgc should contain only one entry, so we can use this method to extract that easily.

lu-zero commented 2 years ago

A get_best_cert() and get_last_cert() might be of use?