der/asn1/
videotex_string.rs1use crate::{
4 asn1::AnyRef, ord::OrdIsValueOrd, ByteSlice, DecodeValue, EncodeValue, Error, FixedTag, Header,
5 Length, Reader, Result, StrSlice, Tag, Writer,
6};
7use core::{fmt, ops::Deref, str};
8
9#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
24pub struct VideotexStringRef<'a> {
25 inner: StrSlice<'a>,
27}
28
29impl<'a> VideotexStringRef<'a> {
30 pub fn new<T>(input: &'a T) -> Result<Self>
32 where
33 T: AsRef<[u8]> + ?Sized,
34 {
35 let input = input.as_ref();
36
37 if input.iter().any(|&c| c > 0x7F) {
40 return Err(Self::TAG.value_error());
41 }
42
43 StrSlice::from_bytes(input)
44 .map(|inner| Self { inner })
45 .map_err(|_| Self::TAG.value_error())
46 }
47}
48
49impl<'a> Deref for VideotexStringRef<'a> {
50 type Target = StrSlice<'a>;
51
52 fn deref(&self) -> &Self::Target {
53 &self.inner
54 }
55}
56
57impl AsRef<str> for VideotexStringRef<'_> {
58 fn as_ref(&self) -> &str {
59 self.as_str()
60 }
61}
62
63impl AsRef<[u8]> for VideotexStringRef<'_> {
64 fn as_ref(&self) -> &[u8] {
65 self.as_bytes()
66 }
67}
68
69impl<'a> DecodeValue<'a> for VideotexStringRef<'a> {
70 fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
71 Self::new(ByteSlice::decode_value(reader, header)?.as_slice())
72 }
73}
74
75impl<'a> EncodeValue for VideotexStringRef<'a> {
76 fn value_len(&self) -> Result<Length> {
77 self.inner.value_len()
78 }
79
80 fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> {
81 self.inner.encode_value(writer)
82 }
83}
84
85impl FixedTag for VideotexStringRef<'_> {
86 const TAG: Tag = Tag::VideotexString;
87}
88
89impl OrdIsValueOrd for VideotexStringRef<'_> {}
90
91impl<'a> From<&VideotexStringRef<'a>> for VideotexStringRef<'a> {
92 fn from(value: &VideotexStringRef<'a>) -> VideotexStringRef<'a> {
93 *value
94 }
95}
96
97impl<'a> TryFrom<AnyRef<'a>> for VideotexStringRef<'a> {
98 type Error = Error;
99
100 fn try_from(any: AnyRef<'a>) -> Result<VideotexStringRef<'a>> {
101 any.decode_into()
102 }
103}
104
105impl<'a> From<VideotexStringRef<'a>> for AnyRef<'a> {
106 fn from(printable_string: VideotexStringRef<'a>) -> AnyRef<'a> {
107 AnyRef::from_tag_and_value(Tag::VideotexString, printable_string.inner.into())
108 }
109}
110
111impl<'a> From<VideotexStringRef<'a>> for &'a [u8] {
112 fn from(printable_string: VideotexStringRef<'a>) -> &'a [u8] {
113 printable_string.as_bytes()
114 }
115}
116
117impl<'a> fmt::Display for VideotexStringRef<'a> {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 f.write_str(self.as_str())
120 }
121}
122
123impl<'a> fmt::Debug for VideotexStringRef<'a> {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 write!(f, "VideotexString({:?})", self.as_str())
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::VideotexStringRef;
132 use crate::Decode;
133
134 #[test]
135 fn parse_bytes() {
136 let example_bytes = &[
137 0x15, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x65, 0x72, 0x20, 0x31,
138 ];
139
140 let printable_string = VideotexStringRef::from_der(example_bytes).unwrap();
141 assert_eq!(printable_string.as_str(), "Test User 1");
142 }
143}