| 1 |
|
| 2 |
|
| 3 |
use crate::{FixedTag, Result, StringRef, Tag, asn1::AnyRef}; |
| 4 |
use core::{fmt, ops::Deref}; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)] |
| 21 |
pub struct VideotexStringRef<'a> { |
| 22 |
|
| 23 |
inner: &'a StringRef, |
| 24 |
} |
| 25 |
|
| 26 |
impl<'a> VideotexStringRef<'a> { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
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 |
|
| 38 |
|
| 39 |
if input.iter().any(|&c| c > 0x7F) { |
| 40 |
return Err(Self::TAG.value_error().into()); |
| 41 |
} |
| 42 |
|
| 43 |
StringRef::from_bytes(input) |
| 44 |
.map(|inner| Self { inner }) |
| 45 |
.map_err(|_| Self::TAG.value_error().into()) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl_string_type!(VideotexStringRef<'a>, 'a); |
| 50 |
|
| 51 |
impl<'a> Deref for VideotexStringRef<'a> { |
| 52 |
type Target = StringRef; |
| 53 |
|
| 54 |
fn deref(&self) -> &Self::Target { |
| 55 |
self.inner |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl FixedTag for VideotexStringRef<'_> { |
| 60 |
const TAG: Tag = Tag::VideotexString; |
| 61 |
} |
| 62 |
|
| 63 |
impl<'a> From<&VideotexStringRef<'a>> for VideotexStringRef<'a> { |
| 64 |
fn from(value: &VideotexStringRef<'a>) -> VideotexStringRef<'a> { |
| 65 |
*value |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl<'a> From<VideotexStringRef<'a>> for AnyRef<'a> { |
| 70 |
fn from(videotex_string: VideotexStringRef<'a>) -> AnyRef<'a> { |
| 71 |
AnyRef::from_tag_and_value(Tag::VideotexString, videotex_string.inner.as_ref()) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl<'a> From<VideotexStringRef<'a>> for &'a [u8] { |
| 76 |
fn from(videotex_string: VideotexStringRef<'a>) -> &'a [u8] { |
| 77 |
videotex_string.inner.as_bytes() |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl fmt::Debug for VideotexStringRef<'_> { |
| 82 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 83 |
write!(f, "VideotexString({:?})", self.as_str()) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[cfg(test)] |
| 88 |
#[allow(clippy::unwrap_used)] |
| 89 |
mod tests { |
| 90 |
use super::VideotexStringRef; |
| 91 |
use crate::Decode; |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn parse_bytes() { |
| 95 |
let example_bytes = &[ |
| 96 |
0x15, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x65, 0x72, 0x20, 0x31, |
| 97 |
]; |
| 98 |
|
| 99 |
let videotex_string = VideotexStringRef::from_der(example_bytes).unwrap(); |
| 100 |
assert_eq!(videotex_string.as_str(), "Test User 1"); |
| 101 |
} |
| 102 |
} |