Skip to main content

der/asn1/
videotex_string.rs

1//! ASN.1 `VideotexString` support.
2
3use crate::{FixedTag, Result, StringRef, Tag, asn1::AnyRef};
4use core::{fmt, ops::Deref};
5
6/// ASN.1 `VideotexString` type.
7///
8/// Supports a subset the ASCII character set (described below).
9///
10/// For UTF-8, use [`Utf8StringRef`][`crate::asn1::Utf8StringRef`] instead.
11/// For the full ASCII character set, use
12/// [`Ia5StringRef`][`crate::asn1::Ia5StringRef`].
13///
14/// This is a zero-copy reference type which borrows from the input data.
15///
16/// # Supported characters
17///
18/// For the practical purposes `VideotexString` is treated as `IA5string`, disallowing non-ASCII chars.
19///
20#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
21pub struct VideotexStringRef<'a> {
22    /// Inner value
23    inner: &'a StringRef,
24}
25
26impl<'a> VideotexStringRef<'a> {
27    /// Create a new ASN.1 `VideotexString`.
28    ///
29    /// # Errors
30    /// If input contains out-of-range characters.
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        // Validate all characters are within VideotexString's allowed set
38        // FIXME: treat as if it were IA5String
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
49impl_string_type!(VideotexStringRef<'a>, 'a);
50
51impl<'a> Deref for VideotexStringRef<'a> {
52    type Target = StringRef;
53
54    fn deref(&self) -> &Self::Target {
55        self.inner
56    }
57}
58
59impl FixedTag for VideotexStringRef<'_> {
60    const TAG: Tag = Tag::VideotexString;
61}
62
63impl<'a> From<&VideotexStringRef<'a>> for VideotexStringRef<'a> {
64    fn from(value: &VideotexStringRef<'a>) -> VideotexStringRef<'a> {
65        *value
66    }
67}
68
69impl<'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
75impl<'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
81impl 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)]
89mod 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}