1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{
asn1::AnyRef, ord::OrdIsValueOrd, ByteSlice, DecodeValue, EncodeValue, Error, FixedTag, Header,
Length, Reader, Result, StrSlice, Tag, Writer,
};
use core::{fmt, str};
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Ia5StringRef<'a> {
inner: StrSlice<'a>,
}
impl<'a> Ia5StringRef<'a> {
pub fn new<T>(input: &'a T) -> Result<Self>
where
T: AsRef<[u8]> + ?Sized,
{
let input = input.as_ref();
if input.iter().any(|&c| c > 0x7F) {
return Err(Self::TAG.value_error());
}
StrSlice::from_bytes(input)
.map(|inner| Self { inner })
.map_err(|_| Self::TAG.value_error())
}
pub fn as_str(&self) -> &'a str {
self.inner.as_str()
}
pub fn as_bytes(&self) -> &'a [u8] {
self.inner.as_bytes()
}
pub fn len(&self) -> Length {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}
impl AsRef<str> for Ia5StringRef<'_> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<[u8]> for Ia5StringRef<'_> {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
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())
}
}
impl EncodeValue for Ia5StringRef<'_> {
fn value_len(&self) -> Result<Length> {
self.inner.value_len()
}
fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> {
self.inner.encode_value(writer)
}
}
impl<'a> FixedTag for Ia5StringRef<'a> {
const TAG: Tag = Tag::Ia5String;
}
impl OrdIsValueOrd for Ia5StringRef<'_> {}
impl<'a> From<&Ia5StringRef<'a>> for Ia5StringRef<'a> {
fn from(value: &Ia5StringRef<'a>) -> Ia5StringRef<'a> {
*value
}
}
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 AnyRef<'a> {
fn from(printable_string: Ia5StringRef<'a>) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Ia5String, printable_string.inner.into())
}
}
impl<'a> From<Ia5StringRef<'a>> for &'a [u8] {
fn from(printable_string: Ia5StringRef<'a>) -> &'a [u8] {
printable_string.as_bytes()
}
}
impl<'a> fmt::Display for Ia5StringRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl<'a> fmt::Debug for Ia5StringRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ia5String({:?})", self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::Ia5StringRef;
use crate::Decode;
use hex_literal::hex;
#[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");
}
}