Skip to content

Commit

Permalink
Add some utility traits for ASN.1 string types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tudyx committed Jan 23, 2024
1 parent 540d7c8 commit b427983
Showing 1 changed file with 94 additions and 5 deletions.
99 changes: 94 additions & 5 deletions rcgen/src/string_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, InvalidAsn1String};
use std::str::FromStr;
use std::{fmt, str::FromStr};

/// ASN.1 `PrintableString` type.
///
Expand Down Expand Up @@ -118,6 +118,36 @@ impl AsRef<str> for PrintableString {
}

Check warning on line 118 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L116-L118

Added lines #L116 - L118 were not covered by tests
}

impl fmt::Display for PrintableString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}

Check warning on line 124 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L122-L124

Added lines #L122 - L124 were not covered by tests
}

impl PartialEq<str> for PrintableString {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}

Check warning on line 130 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L128-L130

Added lines #L128 - L130 were not covered by tests
}

impl PartialEq<String> for PrintableString {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}

Check warning on line 136 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L134-L136

Added lines #L134 - L136 were not covered by tests
}

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()
}

Check warning on line 148 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L146-L148

Added lines #L146 - L148 were not covered by tests
}

/// ASN.1 `IA5String` type.
///
/// # Examples
Expand Down Expand Up @@ -194,6 +224,36 @@ impl AsRef<str> for Ia5String {
}

Check warning on line 224 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L222-L224

Added lines #L222 - L224 were not covered by tests
}

impl fmt::Display for Ia5String {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}

Check warning on line 230 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L228-L230

Added lines #L228 - L230 were not covered by tests
}

impl PartialEq<str> for Ia5String {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}

Check warning on line 236 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L234-L236

Added lines #L234 - L236 were not covered by tests
}

impl PartialEq<String> for Ia5String {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}

Check warning on line 242 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L240-L242

Added lines #L240 - L242 were not covered by tests
}

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()
}

Check warning on line 254 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L252-L254

Added lines #L252 - L254 were not covered by tests
}

/// ASN.1 `TeletexString` type.
///
/// # Examples
Expand Down Expand Up @@ -279,6 +339,36 @@ impl AsRef<str> for TeletexString {
}

Check warning on line 339 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L337-L339

Added lines #L337 - L339 were not covered by tests
}

impl fmt::Display for TeletexString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}

Check warning on line 345 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L343-L345

Added lines #L343 - L345 were not covered by tests
}

impl PartialEq<str> for TeletexString {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}

Check warning on line 351 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L349-L351

Added lines #L349 - L351 were not covered by tests
}

impl PartialEq<String> for TeletexString {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}

Check warning on line 357 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L355-L357

Added lines #L355 - L357 were not covered by tests
}

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()
}

Check warning on line 369 in rcgen/src/string_types.rs

View check run for this annotation

Codecov / codecov/patch

rcgen/src/string_types.rs#L367-L369

Added lines #L367 - L369 were not covered by tests
}

/// ASN.1 `BMPString` type.
///
/// # Examples
Expand Down Expand Up @@ -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());
}
Expand All @@ -527,8 +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);
// TODO use an invalid ascii character
assert_eq!(ia5_string, EXAMPLE_UTF8);
assert!(Ia5String::try_from(String::from('\u{7F}')).is_ok());
assert!(Ia5String::try_from(String::from('\u{8F}')).is_err());
}
Expand All @@ -537,7 +626,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());
}
Expand Down

0 comments on commit b427983

Please sign in to comment.