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/teletex_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/teletex_string.rs')
-rw-r--r-- | vendor/der/src/asn1/teletex_string.rs | 160 |
1 files changed, 110 insertions, 50 deletions
diff --git a/vendor/der/src/asn1/teletex_string.rs b/vendor/der/src/asn1/teletex_string.rs index 7d6621d2d..cedf727db 100644 --- a/vendor/der/src/asn1/teletex_string.rs +++ b/vendor/der/src/asn1/teletex_string.rs @@ -1,10 +1,26 @@ //! ASN.1 `TeletexString` support. +//! +use crate::{asn1::AnyRef, FixedTag, Result, StrRef, Tag}; +use core::{fmt, ops::Deref}; + +macro_rules! impl_teletex_string { + ($type: ty) => { + impl_teletex_string!($type,); + }; + ($type: ty, $($li: lifetime)?) => { + impl_string_type!($type, $($li),*); + + impl<$($li),*> FixedTag for $type { + const TAG: Tag = Tag::TeletexString; + } -use crate::{ - asn1::AnyRef, ord::OrdIsValueOrd, ByteSlice, DecodeValue, EncodeValue, Error, FixedTag, Header, - Length, Reader, Result, StrSlice, Tag, Writer, -}; -use core::{fmt, ops::Deref, str}; + impl<$($li),*> fmt::Debug for $type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "TeletexString({:?})", self.as_str()) + } + } + }; +} /// ASN.1 `TeletexString` type. /// @@ -25,7 +41,7 @@ use core::{fmt, ops::Deref, str}; #[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)] pub struct TeletexStringRef<'a> { /// Inner value - inner: StrSlice<'a>, + inner: StrRef<'a>, } impl<'a> TeletexStringRef<'a> { @@ -41,83 +57,127 @@ impl<'a> TeletexStringRef<'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_teletex_string!(TeletexStringRef<'a>, 'a); + impl<'a> Deref for TeletexStringRef<'a> { - type Target = StrSlice<'a>; + type Target = StrRef<'a>; fn deref(&self) -> &Self::Target { &self.inner } } -impl AsRef<str> for TeletexStringRef<'_> { - fn as_ref(&self) -> &str { - self.as_str() +impl<'a> From<&TeletexStringRef<'a>> for TeletexStringRef<'a> { + fn from(value: &TeletexStringRef<'a>) -> TeletexStringRef<'a> { + *value } } -impl AsRef<[u8]> for TeletexStringRef<'_> { - fn as_ref(&self) -> &[u8] { - self.as_bytes() +impl<'a> From<TeletexStringRef<'a>> for AnyRef<'a> { + fn from(teletex_string: TeletexStringRef<'a>) -> AnyRef<'a> { + AnyRef::from_tag_and_value(Tag::TeletexString, teletex_string.inner.into()) } } -impl<'a> DecodeValue<'a> for TeletexStringRef<'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::TeletexString; -impl<'a> EncodeValue for TeletexStringRef<'a> { - fn value_len(&self) -> Result<Length> { - self.inner.value_len() - } +#[cfg(feature = "alloc")] +mod allocation { + use super::TeletexStringRef; - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { - self.inner.encode_value(writer) + use crate::{ + asn1::AnyRef, + referenced::{OwnedToRef, RefToOwned}, + BytesRef, FixedTag, Result, StrOwned, Tag, + }; + use core::{fmt, ops::Deref}; + + /// ASN.1 `TeletexString` type. + /// + /// Supports a subset the ASCII character set (described below). + /// + /// For UTF-8, use [`Utf8StringRef`][`crate::asn1::Utf8StringRef`] instead. + /// For the full ASCII character set, use + /// [`Ia5StringRef`][`crate::asn1::Ia5StringRef`]. + /// + /// # Supported characters + /// + /// The standard defines a complex character set allowed in this type. However, quoting the ASN.1 + /// mailing list, "a sizable volume of software in the world treats TeletexString (T61String) as a + /// simple 8-bit string with mostly Windows Latin 1 (superset of iso-8859-1) encoding". + /// + #[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] + pub struct TeletexString { + /// Inner value + inner: StrOwned, } -} -impl FixedTag for TeletexStringRef<'_> { - const TAG: Tag = Tag::TeletexString; -} + impl TeletexString { + /// Create a new ASN.1 `TeletexString`. + pub fn new<T>(input: &T) -> Result<Self> + where + T: AsRef<[u8]> + ?Sized, + { + let input = input.as_ref(); -impl OrdIsValueOrd for TeletexStringRef<'_> {} + TeletexStringRef::new(input)?; -impl<'a> From<&TeletexStringRef<'a>> for TeletexStringRef<'a> { - fn from(value: &TeletexStringRef<'a>) -> TeletexStringRef<'a> { - *value + StrOwned::from_bytes(input) + .map(|inner| Self { inner }) + .map_err(|_| Self::TAG.value_error()) + } } -} -impl<'a> TryFrom<AnyRef<'a>> for TeletexStringRef<'a> { - type Error = Error; + impl_teletex_string!(TeletexString); - fn try_from(any: AnyRef<'a>) -> Result<TeletexStringRef<'a>> { - any.decode_into() + impl Deref for TeletexString { + type Target = StrOwned; + + fn deref(&self) -> &Self::Target { + &self.inner + } } -} -impl<'a> From<TeletexStringRef<'a>> for AnyRef<'a> { - fn from(teletex_string: TeletexStringRef<'a>) -> AnyRef<'a> { - AnyRef::from_tag_and_value(Tag::TeletexString, teletex_string.inner.into()) + impl<'a> From<TeletexStringRef<'a>> for TeletexString { + fn from(value: TeletexStringRef<'a>) -> TeletexString { + let inner = + StrOwned::from_bytes(value.inner.as_bytes()).expect("Invalid TeletexString"); + Self { inner } + } } -} -impl<'a> fmt::Display for TeletexStringRef<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.as_str()) + impl<'a> From<&'a TeletexString> for AnyRef<'a> { + fn from(teletex_string: &'a TeletexString) -> AnyRef<'a> { + AnyRef::from_tag_and_value( + Tag::TeletexString, + BytesRef::new(teletex_string.inner.as_bytes()).expect("Invalid TeletexString"), + ) + } } -} -impl<'a> fmt::Debug for TeletexStringRef<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "TeletexString({:?})", self.as_str()) + impl<'a> RefToOwned<'a> for TeletexStringRef<'a> { + type Owned = TeletexString; + fn ref_to_owned(&self) -> Self::Owned { + TeletexString { + inner: self.inner.ref_to_owned(), + } + } + } + + impl OwnedToRef for TeletexString { + type Borrowed<'a> = TeletexStringRef<'a>; + fn owned_to_ref(&self) -> Self::Borrowed<'_> { + TeletexStringRef { + inner: self.inner.owned_to_ref(), + } + } } } |