From c352ad86eb2769853e8162b5b124656939cc0be3 Mon Sep 17 00:00:00 2001 From: Tudyx <56633664+Tudyx@users.noreply.github.com> Date: Mon, 22 Jan 2024 08:22:19 +0100 Subject: [PATCH] Add some utility traits for ASN.1 string types --- rcgen/src/string_types.rs | 98 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/rcgen/src/string_types.rs b/rcgen/src/string_types.rs index a9570f42..1abcdef0 100644 --- a/rcgen/src/string_types.rs +++ b/rcgen/src/string_types.rs @@ -1,5 +1,5 @@ use crate::{Error, InvalidAsn1String}; -use std::str::FromStr; +use std::{fmt, str::FromStr}; /// ASN.1 `PrintableString` type. /// @@ -118,6 +118,36 @@ impl AsRef for PrintableString { } } +impl fmt::Display for PrintableString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.as_str(), f) + } +} + +impl PartialEq for PrintableString { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + +impl PartialEq for PrintableString { + fn eq(&self, other: &String) -> bool { + self.as_str() == other.as_str() + } +} + +impl PartialEq<&str> for PrintableString { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} + +impl PartialEq<&String> for PrintableString { + fn eq(&self, other: &&String) -> bool { + self.as_str() == other.as_str() + } +} + /// ASN.1 `IA5String` type. /// /// # Examples @@ -194,6 +224,36 @@ impl AsRef for Ia5String { } } +impl fmt::Display for Ia5String { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.as_str(), f) + } +} + +impl PartialEq for Ia5String { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + +impl PartialEq for Ia5String { + fn eq(&self, other: &String) -> bool { + self.as_str() == other.as_str() + } +} + +impl PartialEq<&str> for Ia5String { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} + +impl PartialEq<&String> for Ia5String { + fn eq(&self, other: &&String) -> bool { + self.as_str() == other.as_str() + } +} + /// ASN.1 `TeletexString` type. /// /// # Examples @@ -279,6 +339,36 @@ impl AsRef for TeletexString { } } +impl fmt::Display for TeletexString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.as_str(), f) + } +} + +impl PartialEq for TeletexString { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + +impl PartialEq for TeletexString { + fn eq(&self, other: &String) -> bool { + self.as_str() == other.as_str() + } +} + +impl PartialEq<&str> for TeletexString { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} + +impl PartialEq<&String> for TeletexString { + fn eq(&self, other: &&String) -> bool { + self.as_str() == other.as_str() + } +} + /// ASN.1 `BMPString` type. /// /// # Examples @@ -518,7 +608,7 @@ mod tests { fn printable_string() { const EXAMPLE_UTF8: &str = "CertificateTemplate"; let printable_string = PrintableString::try_from(EXAMPLE_UTF8).unwrap(); - assert_eq!(printable_string.as_str(), EXAMPLE_UTF8); + assert_eq!(printable_string, EXAMPLE_UTF8); assert!(PrintableString::try_from("@").is_err()); assert!(PrintableString::try_from("*").is_err()); } @@ -527,7 +617,7 @@ mod tests { fn ia5_string() { const EXAMPLE_UTF8: &str = "CertificateTemplate"; let ia5_string = Ia5String::try_from(EXAMPLE_UTF8).unwrap(); - assert_eq!(ia5_string.as_str(), EXAMPLE_UTF8); + assert_eq!(ia5_string, EXAMPLE_UTF8); // TODO use an invalid ascii character assert!(Ia5String::try_from(String::from('\u{7F}')).is_ok()); assert!(Ia5String::try_from(String::from('\u{8F}')).is_err()); @@ -537,7 +627,7 @@ mod tests { fn teletext_string() { const EXAMPLE_UTF8: &str = "CertificateTemplate"; let teletext_string = TeletexString::try_from(EXAMPLE_UTF8).unwrap(); - assert_eq!(teletext_string.as_str(), EXAMPLE_UTF8); + assert_eq!(teletext_string, EXAMPLE_UTF8); assert!(Ia5String::try_from(String::from('\u{7F}')).is_ok()); assert!(Ia5String::try_from(String::from('\u{8F}')).is_err()); }