Skip to content

Commit b427983

Browse files
committed
Add some utility traits for ASN.1 string types
1 parent 540d7c8 commit b427983

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

rcgen/src/string_types.rs

+94-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Error, InvalidAsn1String};
2-
use std::str::FromStr;
2+
use std::{fmt, str::FromStr};
33

44
/// ASN.1 `PrintableString` type.
55
///
@@ -118,6 +118,36 @@ impl AsRef<str> for PrintableString {
118118
}
119119
}
120120

121+
impl fmt::Display for PrintableString {
122+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123+
fmt::Display::fmt(self.as_str(), f)
124+
}
125+
}
126+
127+
impl PartialEq<str> for PrintableString {
128+
fn eq(&self, other: &str) -> bool {
129+
self.as_str() == other
130+
}
131+
}
132+
133+
impl PartialEq<String> for PrintableString {
134+
fn eq(&self, other: &String) -> bool {
135+
self.as_str() == other.as_str()
136+
}
137+
}
138+
139+
impl PartialEq<&str> for PrintableString {
140+
fn eq(&self, other: &&str) -> bool {
141+
self.as_str() == *other
142+
}
143+
}
144+
145+
impl PartialEq<&String> for PrintableString {
146+
fn eq(&self, other: &&String) -> bool {
147+
self.as_str() == other.as_str()
148+
}
149+
}
150+
121151
/// ASN.1 `IA5String` type.
122152
///
123153
/// # Examples
@@ -194,6 +224,36 @@ impl AsRef<str> for Ia5String {
194224
}
195225
}
196226

227+
impl fmt::Display for Ia5String {
228+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229+
fmt::Display::fmt(self.as_str(), f)
230+
}
231+
}
232+
233+
impl PartialEq<str> for Ia5String {
234+
fn eq(&self, other: &str) -> bool {
235+
self.as_str() == other
236+
}
237+
}
238+
239+
impl PartialEq<String> for Ia5String {
240+
fn eq(&self, other: &String) -> bool {
241+
self.as_str() == other.as_str()
242+
}
243+
}
244+
245+
impl PartialEq<&str> for Ia5String {
246+
fn eq(&self, other: &&str) -> bool {
247+
self.as_str() == *other
248+
}
249+
}
250+
251+
impl PartialEq<&String> for Ia5String {
252+
fn eq(&self, other: &&String) -> bool {
253+
self.as_str() == other.as_str()
254+
}
255+
}
256+
197257
/// ASN.1 `TeletexString` type.
198258
///
199259
/// # Examples
@@ -279,6 +339,36 @@ impl AsRef<str> for TeletexString {
279339
}
280340
}
281341

342+
impl fmt::Display for TeletexString {
343+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
344+
fmt::Display::fmt(self.as_str(), f)
345+
}
346+
}
347+
348+
impl PartialEq<str> for TeletexString {
349+
fn eq(&self, other: &str) -> bool {
350+
self.as_str() == other
351+
}
352+
}
353+
354+
impl PartialEq<String> for TeletexString {
355+
fn eq(&self, other: &String) -> bool {
356+
self.as_str() == other.as_str()
357+
}
358+
}
359+
360+
impl PartialEq<&str> for TeletexString {
361+
fn eq(&self, other: &&str) -> bool {
362+
self.as_str() == *other
363+
}
364+
}
365+
366+
impl PartialEq<&String> for TeletexString {
367+
fn eq(&self, other: &&String) -> bool {
368+
self.as_str() == other.as_str()
369+
}
370+
}
371+
282372
/// ASN.1 `BMPString` type.
283373
///
284374
/// # Examples
@@ -518,7 +608,7 @@ mod tests {
518608
fn printable_string() {
519609
const EXAMPLE_UTF8: &str = "CertificateTemplate";
520610
let printable_string = PrintableString::try_from(EXAMPLE_UTF8).unwrap();
521-
assert_eq!(printable_string.as_str(), EXAMPLE_UTF8);
611+
assert_eq!(printable_string, EXAMPLE_UTF8);
522612
assert!(PrintableString::try_from("@").is_err());
523613
assert!(PrintableString::try_from("*").is_err());
524614
}
@@ -527,8 +617,7 @@ mod tests {
527617
fn ia5_string() {
528618
const EXAMPLE_UTF8: &str = "CertificateTemplate";
529619
let ia5_string = Ia5String::try_from(EXAMPLE_UTF8).unwrap();
530-
assert_eq!(ia5_string.as_str(), EXAMPLE_UTF8);
531-
// TODO use an invalid ascii character
620+
assert_eq!(ia5_string, EXAMPLE_UTF8);
532621
assert!(Ia5String::try_from(String::from('\u{7F}')).is_ok());
533622
assert!(Ia5String::try_from(String::from('\u{8F}')).is_err());
534623
}
@@ -537,7 +626,7 @@ mod tests {
537626
fn teletext_string() {
538627
const EXAMPLE_UTF8: &str = "CertificateTemplate";
539628
let teletext_string = TeletexString::try_from(EXAMPLE_UTF8).unwrap();
540-
assert_eq!(teletext_string.as_str(), EXAMPLE_UTF8);
629+
assert_eq!(teletext_string, EXAMPLE_UTF8);
541630
assert!(Ia5String::try_from(String::from('\u{7F}')).is_ok());
542631
assert!(Ia5String::try_from(String::from('\u{8F}')).is_err());
543632
}

0 commit comments

Comments
 (0)