1
+ use alloc:: borrow:: Cow ;
1
2
use alloc:: string:: String ;
3
+ use alloc:: string:: ToString ;
2
4
use der:: {
3
5
Choice , FixedTag , Header , Reader , ValueOrd ,
4
- asn1:: { Any , PrintableString , TeletexString } ,
6
+ asn1:: { Any , BmpString , PrintableString , TeletexString } ,
5
7
} ;
6
8
7
9
/// DirectoryString as defined in [RFC 5280 Section 4.2.1.4].
@@ -52,6 +54,9 @@ pub enum DirectoryString {
52
54
53
55
#[ asn1( type = "UTF8String" ) ]
54
56
Utf8String ( String ) ,
57
+
58
+ #[ asn1( type = "BMPString" ) ]
59
+ BmpString ( BmpString ) ,
55
60
}
56
61
57
62
impl < ' a > TryFrom < & ' a Any > for DirectoryString {
@@ -73,6 +78,7 @@ impl<'a> der::DecodeValue<'a> for DirectoryString {
73
78
TeletexString :: decode_value ( reader, header) . map ( Self :: TeletexString )
74
79
}
75
80
String :: TAG => String :: decode_value ( reader, header) . map ( Self :: Utf8String ) ,
81
+ BmpString :: TAG => BmpString :: decode_value ( reader, header) . map ( Self :: BmpString ) ,
76
82
actual => Err ( der:: ErrorKind :: TagUnexpected {
77
83
expected : None ,
78
84
actual,
@@ -81,13 +87,28 @@ impl<'a> der::DecodeValue<'a> for DirectoryString {
81
87
}
82
88
}
83
89
}
90
+ impl DirectoryString {
91
+ /// Returns `Borrowed` variant for UTF-8 compatible strings
92
+ /// and `Owned` variant otherwise.
93
+ pub fn value ( & self ) -> Cow < ' _ , 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
+ }
84
102
85
103
impl AsRef < str > for DirectoryString {
104
+ // #[deprecated(since = "0.3.0-pre.0", note = "use `DirectoryString::value` instead")]
86
105
fn as_ref ( & self ) -> & str {
87
106
match self {
88
107
Self :: PrintableString ( s) => s. as_ref ( ) ,
89
108
Self :: TeletexString ( s) => s. as_ref ( ) ,
90
109
Self :: Utf8String ( s) => s. as_ref ( ) ,
110
+ // TODO(dishmaker): BMPString as ref
111
+ Self :: BmpString ( _s) => "" ,
91
112
}
92
113
}
93
114
}
0 commit comments