Implement Display for CardholderRelatedData

This commit is contained in:
Heiko Schaefer 2022-05-01 17:16:22 +02:00
parent db9c6e120f
commit 02b42081b9
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
3 changed files with 26 additions and 0 deletions

View file

@ -737,6 +737,23 @@ pub struct CardholderRelatedData {
sex: Option<Sex>,
}
impl Display for CardholderRelatedData {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(name) = &self.name {
writeln!(f, "Name: {}", Self::latin1_to_string(name))?;
}
if let Some(sex) = self.sex {
writeln!(f, "Sex: {}", sex)?;
}
if let Some(lang) = &self.lang {
for (n, l) in lang.iter().enumerate() {
writeln!(f, "Lang {}: {}", n + 1, l)?;
}
}
Ok(())
}
}
/// 4.4.3.5 Sex
/// Encoded in accordance with https://en.wikipedia.org/wiki/ISO/IEC_5218
#[derive(Debug, PartialEq, Clone, Copy)]

View file

@ -13,6 +13,14 @@ impl CardholderRelatedData {
self.name.as_deref()
}
/// The name field is defined as latin1 encoded,
/// with ´<´ and ´<<´ filler characters to separate elements and name-parts.
///
/// (The filler/separation characters are not processed by this fn)
pub(crate) fn latin1_to_string(s: &[u8]) -> String {
s.iter().map(|&c| c as char).collect()
}
pub fn lang(&self) -> Option<&[Lang]> {
self.lang.as_deref()
}

View file

@ -419,6 +419,7 @@ fn print_status(ident: Option<String>, verbose: bool) -> Result<()> {
let crd = open.cardholder_related_data()?;
if let Some(name) = crd.name() {
// FIXME: decoding as utf8 is wrong (the spec defines this field as latin1 encoded)
let name = String::from_utf8_lossy(name).to_string();
print!("Cardholder: ");