diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/der/src/asn1/ia5_string.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/der/src/asn1/ia5_string.rs')
-rw-r--r-- | vendor/der/src/asn1/ia5_string.rs | 154 |
1 files changed, 102 insertions, 52 deletions
diff --git a/vendor/der/src/asn1/ia5_string.rs b/vendor/der/src/asn1/ia5_string.rs index 3971270a8..c3f24f01c 100644 --- a/vendor/der/src/asn1/ia5_string.rs +++ b/vendor/der/src/asn1/ia5_string.rs @@ -1,10 +1,26 @@ //! ASN.1 `IA5String` support. -use crate::{ - asn1::AnyRef, ord::OrdIsValueOrd, ByteSlice, DecodeValue, EncodeValue, Error, FixedTag, Header, - Length, Reader, Result, StrSlice, Tag, Writer, -}; -use core::{fmt, ops::Deref, str}; +use crate::{asn1::AnyRef, FixedTag, Result, StrRef, Tag}; +use core::{fmt, ops::Deref}; + +macro_rules! impl_ia5_string { + ($type: ty) => { + impl_ia5_string!($type,); + }; + ($type: ty, $($li: lifetime)?) => { + impl_string_type!($type, $($li),*); + + impl<$($li),*> FixedTag for $type { + const TAG: Tag = Tag::Ia5String; + } + + impl<$($li),*> fmt::Debug for $type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Ia5String({:?})", self.as_str()) + } + } + }; +} /// ASN.1 `IA5String` type. /// @@ -21,7 +37,7 @@ use core::{fmt, ops::Deref, str}; #[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)] pub struct Ia5StringRef<'a> { /// Inner value - inner: StrSlice<'a>, + inner: StrRef<'a>, } impl<'a> Ia5StringRef<'a> { @@ -37,83 +53,117 @@ impl<'a> Ia5StringRef<'a> { return Err(Self::TAG.value_error()); } - StrSlice::from_bytes(input) + StrRef::from_bytes(input) .map(|inner| Self { inner }) .map_err(|_| Self::TAG.value_error()) } } +impl_ia5_string!(Ia5StringRef<'a>, 'a); + impl<'a> Deref for Ia5StringRef<'a> { - type Target = StrSlice<'a>; + type Target = StrRef<'a>; fn deref(&self) -> &Self::Target { &self.inner } } -impl AsRef<str> for Ia5StringRef<'_> { - fn as_ref(&self) -> &str { - self.as_str() +impl<'a> From<&Ia5StringRef<'a>> for Ia5StringRef<'a> { + fn from(value: &Ia5StringRef<'a>) -> Ia5StringRef<'a> { + *value } } -impl AsRef<[u8]> for Ia5StringRef<'_> { - fn as_ref(&self) -> &[u8] { - self.as_bytes() +impl<'a> From<Ia5StringRef<'a>> for AnyRef<'a> { + fn from(internationalized_string: Ia5StringRef<'a>) -> AnyRef<'a> { + AnyRef::from_tag_and_value(Tag::Ia5String, internationalized_string.inner.into()) } } -impl<'a> DecodeValue<'a> for Ia5StringRef<'a> { - fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { - Self::new(ByteSlice::decode_value(reader, header)?.as_slice()) - } -} +#[cfg(feature = "alloc")] +pub use self::allocation::Ia5String; -impl EncodeValue for Ia5StringRef<'_> { - fn value_len(&self) -> Result<Length> { - self.inner.value_len() +#[cfg(feature = "alloc")] +mod allocation { + use super::Ia5StringRef; + use crate::{ + asn1::AnyRef, + referenced::{OwnedToRef, RefToOwned}, + FixedTag, Result, StrOwned, Tag, + }; + use core::{fmt, ops::Deref}; + + /// ASN.1 `IA5String` type. + /// + /// Supports the [International Alphabet No. 5 (IA5)] character encoding, i.e. + /// the lower 128 characters of the ASCII alphabet. (Note: IA5 is now + /// technically known as the International Reference Alphabet or IRA as + /// specified in the ITU-T's T.50 recommendation). + /// + /// For UTF-8, use [`String`][`alloc::string::String`]. + /// + /// [International Alphabet No. 5 (IA5)]: https://en.wikipedia.org/wiki/T.50_%28standard%29 + #[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] + pub struct Ia5String { + /// Inner value + inner: StrOwned, } - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { - self.inner.encode_value(writer) + impl Ia5String { + /// Create a new `IA5String`. + pub fn new<T>(input: &T) -> Result<Self> + where + T: AsRef<[u8]> + ?Sized, + { + let input = input.as_ref(); + Ia5StringRef::new(input)?; + + StrOwned::from_bytes(input) + .map(|inner| Self { inner }) + .map_err(|_| Self::TAG.value_error()) + } } -} -impl<'a> FixedTag for Ia5StringRef<'a> { - const TAG: Tag = Tag::Ia5String; -} + impl_ia5_string!(Ia5String); -impl OrdIsValueOrd for Ia5StringRef<'_> {} + impl Deref for Ia5String { + type Target = StrOwned; -impl<'a> From<&Ia5StringRef<'a>> for Ia5StringRef<'a> { - fn from(value: &Ia5StringRef<'a>) -> Ia5StringRef<'a> { - *value + fn deref(&self) -> &Self::Target { + &self.inner + } } -} - -impl<'a> TryFrom<AnyRef<'a>> for Ia5StringRef<'a> { - type Error = Error; - fn try_from(any: AnyRef<'a>) -> Result<Ia5StringRef<'a>> { - any.decode_into() + impl<'a> From<Ia5StringRef<'a>> for Ia5String { + fn from(international_string: Ia5StringRef<'a>) -> Ia5String { + let inner = international_string.inner.into(); + Self { inner } + } } -} -impl<'a> From<Ia5StringRef<'a>> for AnyRef<'a> { - fn from(printable_string: Ia5StringRef<'a>) -> AnyRef<'a> { - AnyRef::from_tag_and_value(Tag::Ia5String, printable_string.inner.into()) + 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()) + } } -} -impl<'a> fmt::Display for Ia5StringRef<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.as_str()) + impl<'a> RefToOwned<'a> for Ia5StringRef<'a> { + type Owned = Ia5String; + fn ref_to_owned(&self) -> Self::Owned { + Ia5String { + inner: self.inner.ref_to_owned(), + } + } } -} -impl<'a> fmt::Debug for Ia5StringRef<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Ia5String({:?})", self.as_str()) + impl OwnedToRef for Ia5String { + type Borrowed<'a> = Ia5StringRef<'a>; + fn owned_to_ref(&self) -> Self::Borrowed<'_> { + Ia5StringRef { + inner: self.inner.owned_to_ref(), + } + } } } @@ -126,7 +176,7 @@ mod tests { #[test] fn parse_bytes() { let example_bytes = hex!("16 0d 74 65 73 74 31 40 72 73 61 2e 63 6f 6d"); - let printable_string = Ia5StringRef::from_der(&example_bytes).unwrap(); - assert_eq!(printable_string.as_str(), "test1@rsa.com"); + let internationalized_string = Ia5StringRef::from_der(&example_bytes).unwrap(); + assert_eq!(internationalized_string.as_str(), "test1@rsa.com"); } } |