Skip to content

Commit 94f0b86

Browse files
authored
der_derive: use TryFrom conversions for asn1(type = ...) (#1562)
Previously it was using `<asn1_type>::new`. Using `TryFrom` is more flexible in terms of the possible conversions that it permits. Also fills out the missing `From`/`TryFrom` conversions needed for the existing crates in this repo (most notably `x509-cert` to compile with the changes).
1 parent 24fc0eb commit 94f0b86

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

der/src/asn1/ia5_string.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,21 @@ mod allocation {
137137
}
138138

139139
impl<'a> From<Ia5StringRef<'a>> for Ia5String {
140-
fn from(international_string: Ia5StringRef<'a>) -> Ia5String {
141-
let inner = international_string.inner.into();
140+
fn from(ia5_string: Ia5StringRef<'a>) -> Ia5String {
141+
let inner = ia5_string.inner.into();
142142
Self { inner }
143143
}
144144
}
145145

146146
impl<'a> From<&'a Ia5String> for AnyRef<'a> {
147-
fn from(international_string: &'a Ia5String) -> AnyRef<'a> {
148-
AnyRef::from_tag_and_value(Tag::Ia5String, (&international_string.inner).into())
147+
fn from(ia5_string: &'a Ia5String) -> AnyRef<'a> {
148+
AnyRef::from_tag_and_value(Tag::Ia5String, (&ia5_string.inner).into())
149+
}
150+
}
151+
152+
impl<'a> From<&'a Ia5String> for Ia5StringRef<'a> {
153+
fn from(ia5_string: &'a Ia5String) -> Ia5StringRef<'a> {
154+
ia5_string.owned_to_ref()
149155
}
150156
}
151157

der/src/asn1/octet_string.rs

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ impl<'a> From<OctetStringRef<'a>> for &'a [u8] {
9696
}
9797
}
9898

99+
impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
100+
type Error = Error;
101+
102+
fn try_from(byte_slice: &'a [u8]) -> Result<Self, Error> {
103+
OctetStringRef::new(byte_slice)
104+
}
105+
}
106+
99107
#[cfg(feature = "alloc")]
100108
pub use self::allocating::OctetString;
101109

der/src/asn1/printable_string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ mod allocation {
204204
}
205205
}
206206

207+
impl<'a> From<&'a PrintableString> for PrintableStringRef<'a> {
208+
fn from(printable_string: &'a PrintableString) -> PrintableStringRef<'a> {
209+
printable_string.owned_to_ref()
210+
}
211+
}
212+
207213
impl<'a> RefToOwned<'a> for PrintableStringRef<'a> {
208214
type Owned = PrintableString;
209215
fn ref_to_owned(&self) -> Self::Owned {

der/src/asn1/teletex_string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ mod allocation {
166166
}
167167
}
168168

169+
impl<'a> From<&'a TeletexString> for TeletexStringRef<'a> {
170+
fn from(teletex_string: &'a TeletexString) -> TeletexStringRef<'a> {
171+
teletex_string.owned_to_ref()
172+
}
173+
}
174+
169175
impl<'a> RefToOwned<'a> for TeletexStringRef<'a> {
170176
type Owned = TeletexString;
171177
fn ref_to_owned(&self) -> Self::Owned {

der/src/asn1/utf8_string.rs

+17
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ impl<'a> From<Utf8StringRef<'a>> for AnyRef<'a> {
6868
}
6969
}
7070

71+
impl<'a> TryFrom<&'a str> for Utf8StringRef<'a> {
72+
type Error = Error;
73+
74+
fn try_from(s: &'a str) -> Result<Self> {
75+
Self::new(s)
76+
}
77+
}
78+
7179
impl<'a> fmt::Debug for Utf8StringRef<'a> {
7280
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7381
write!(f, "Utf8String({:?})", self.as_str())
@@ -114,6 +122,15 @@ impl<'a> TryFrom<AnyRef<'a>> for String {
114122
}
115123
}
116124

125+
#[cfg(feature = "alloc")]
126+
impl<'a> TryFrom<&'a String> for Utf8StringRef<'a> {
127+
type Error = Error;
128+
129+
fn try_from(s: &'a String) -> Result<Self> {
130+
Self::new(s.as_str())
131+
}
132+
}
133+
117134
#[cfg(feature = "alloc")]
118135
impl<'a> DecodeValue<'a> for String {
119136
type Error = Error;

der_derive/src/asn1_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Asn1Type {
7777
| Asn1Type::PrintableString
7878
| Asn1Type::TeletexString
7979
| Asn1Type::VideotexString
80-
| Asn1Type::Utf8String => quote!(#type_path::new(#binding)?),
80+
| Asn1Type::Utf8String => quote!(#type_path::try_from(#binding)?),
8181
_ => quote!(#type_path::try_from(#binding)?),
8282
}
8383
}

der_derive/src/choice/variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ mod tests {
225225
assert_eq!(
226226
variant.to_encode_value_tokens().to_string(),
227227
quote! {
228-
Self::ExampleVariant(variant) => ::der::asn1::Utf8StringRef::new(variant)?.encode_value(encoder),
228+
Self::ExampleVariant(variant) => ::der::asn1::Utf8StringRef::try_from(variant)?.encode_value(encoder),
229229
}
230230
.to_string()
231231
);

0 commit comments

Comments
 (0)