Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

der_derive: use TryFrom conversions for asn1(type = ...) #1562

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions der/src/asn1/ia5_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,21 @@ mod allocation {
}

impl<'a> From<Ia5StringRef<'a>> for Ia5String {
fn from(international_string: Ia5StringRef<'a>) -> Ia5String {
let inner = international_string.inner.into();
fn from(ia5_string: Ia5StringRef<'a>) -> Ia5String {
let inner = ia5_string.inner.into();
Self { inner }
}
}

impl<'a> From<&'a Ia5String> for AnyRef<'a> {
fn from(international_string: &'a Ia5String) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Ia5String, (&international_string.inner).into())
fn from(ia5_string: &'a Ia5String) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Ia5String, (&ia5_string.inner).into())
}
}

impl<'a> From<&'a Ia5String> for Ia5StringRef<'a> {
fn from(ia5_string: &'a Ia5String) -> Ia5StringRef<'a> {
ia5_string.owned_to_ref()
}
}

Expand Down
8 changes: 8 additions & 0 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ impl<'a> From<OctetStringRef<'a>> for &'a [u8] {
}
}

impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
type Error = Error;

fn try_from(byte_slice: &'a [u8]) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

#[cfg(feature = "alloc")]
pub use self::allocating::OctetString;

Expand Down
6 changes: 6 additions & 0 deletions der/src/asn1/printable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ mod allocation {
}
}

impl<'a> From<&'a PrintableString> for PrintableStringRef<'a> {
fn from(printable_string: &'a PrintableString) -> PrintableStringRef<'a> {
printable_string.owned_to_ref()
}
}

impl<'a> RefToOwned<'a> for PrintableStringRef<'a> {
type Owned = PrintableString;
fn ref_to_owned(&self) -> Self::Owned {
Expand Down
6 changes: 6 additions & 0 deletions der/src/asn1/teletex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ mod allocation {
}
}

impl<'a> From<&'a TeletexString> for TeletexStringRef<'a> {
fn from(teletex_string: &'a TeletexString) -> TeletexStringRef<'a> {
teletex_string.owned_to_ref()
}
}

impl<'a> RefToOwned<'a> for TeletexStringRef<'a> {
type Owned = TeletexString;
fn ref_to_owned(&self) -> Self::Owned {
Expand Down
17 changes: 17 additions & 0 deletions der/src/asn1/utf8_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ impl<'a> From<Utf8StringRef<'a>> for AnyRef<'a> {
}
}

impl<'a> TryFrom<&'a str> for Utf8StringRef<'a> {
type Error = Error;

fn try_from(s: &'a str) -> Result<Self> {
Self::new(s)
}
}

impl<'a> fmt::Debug for Utf8StringRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Utf8String({:?})", self.as_str())
Expand Down Expand Up @@ -114,6 +122,15 @@ impl<'a> TryFrom<AnyRef<'a>> for String {
}
}

#[cfg(feature = "alloc")]
impl<'a> TryFrom<&'a String> for Utf8StringRef<'a> {
type Error = Error;

fn try_from(s: &'a String) -> Result<Self> {
Self::new(s.as_str())
}
}

#[cfg(feature = "alloc")]
impl<'a> DecodeValue<'a> for String {
type Error = Error;
Expand Down
2 changes: 1 addition & 1 deletion der_derive/src/asn1_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Asn1Type {
| Asn1Type::PrintableString
| Asn1Type::TeletexString
| Asn1Type::VideotexString
| Asn1Type::Utf8String => quote!(#type_path::new(#binding)?),
| Asn1Type::Utf8String => quote!(#type_path::try_from(#binding)?),
_ => quote!(#type_path::try_from(#binding)?),
}
}
Expand Down
2 changes: 1 addition & 1 deletion der_derive/src/choice/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
assert_eq!(
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ExampleVariant(variant) => ::der::asn1::Utf8StringRef::new(variant)?.encode_value(encoder),
Self::ExampleVariant(variant) => ::der::asn1::Utf8StringRef::try_from(variant)?.encode_value(encoder),
}
.to_string()
);
Expand Down
Loading