1+ use std:: { borrow:: Cow , string:: ToString } ;
2+
13use alloc:: string:: String ;
24use der:: {
35 Choice , FixedTag , Header , Reader , ValueOrd ,
4- asn1:: { Any , PrintableString , TeletexString } ,
6+ asn1:: { Any , BmpString , PrintableString , TeletexString } ,
57} ;
68
79/// DirectoryString as defined in [RFC 5280 Section 4.2.1.4].
@@ -52,6 +54,9 @@ pub enum DirectoryString {
5254
5355 #[ asn1( type = "UTF8String" ) ]
5456 Utf8String ( String ) ,
57+
58+ #[ asn1( type = "BMPString" ) ]
59+ BmpString ( BmpString ) ,
5560}
5661
5762impl < ' a > TryFrom < & ' a Any > for DirectoryString {
@@ -73,6 +78,7 @@ impl<'a> der::DecodeValue<'a> for DirectoryString {
7378 TeletexString :: decode_value ( reader, header) . map ( Self :: TeletexString )
7479 }
7580 String :: TAG => String :: decode_value ( reader, header) . map ( Self :: Utf8String ) ,
81+ BmpString :: TAG => BmpString :: decode_value ( reader, header) . map ( Self :: BmpString ) ,
7682 actual => Err ( der:: ErrorKind :: TagUnexpected {
7783 expected : None ,
7884 actual,
@@ -81,13 +87,28 @@ impl<'a> der::DecodeValue<'a> for DirectoryString {
8187 }
8288 }
8389}
90+ impl DirectoryString {
91+ /// Returns `Borrowed` variant for UTF-8 compatible strings
92+ /// and `Owned` variant otherwise.
93+ pub fn value < ' s > ( & ' s self ) -> Cow < ' s , str > {
94+ match self {
95+ Self :: PrintableString ( s) => Cow :: Borrowed ( s. as_ref ( ) ) ,
96+ Self :: TeletexString ( s) => Cow :: Borrowed ( s. as_ref ( ) ) ,
97+ Self :: Utf8String ( s) => Cow :: Borrowed ( s. as_ref ( ) ) ,
98+ Self :: BmpString ( s) => Cow :: Owned ( s. to_string ( ) ) ,
99+ }
100+ }
101+ }
84102
85103impl AsRef < str > for DirectoryString {
104+ // #[deprecated(since = "0.3.0-pre.0", note = "use `DirectoryString::value` instead")]
86105 fn as_ref ( & self ) -> & str {
87106 match self {
88107 Self :: PrintableString ( s) => s. as_ref ( ) ,
89108 Self :: TeletexString ( s) => s. as_ref ( ) ,
90109 Self :: Utf8String ( s) => s. as_ref ( ) ,
110+ // TODO(dishmaker): BMPString as ref
111+ Self :: BmpString ( _s) => "" ,
91112 }
92113 }
93114}
0 commit comments