use crate::{
BitString, ByteSlice, Choice, Decodable, Decoder, Encodable, Encoder, Error, ErrorKind,
GeneralizedTime, Header, Ia5String, Length, Null, OctetString, PrintableString, Result,
Sequence, Tag, UtcTime, Utf8String,
};
use core::convert::{TryFrom, TryInto};
#[cfg(feature = "oid")]
use crate::ObjectIdentifier;
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Any<'a> {
pub(crate) tag: Tag,
pub(crate) value: ByteSlice<'a>,
}
impl<'a> Any<'a> {
pub fn new(tag: Tag, value: &'a [u8]) -> Result<Self> {
Ok(Self {
tag,
value: ByteSlice::new(value).map_err(|_| ErrorKind::Length { tag })?,
})
}
pub fn tag(self) -> Tag {
self.tag
}
pub fn len(self) -> Length {
self.value.len()
}
pub fn is_empty(self) -> bool {
self.value.is_empty()
}
pub fn is_null(self) -> bool {
Null::try_from(self).is_ok()
}
pub fn as_bytes(self) -> &'a [u8] {
self.value.as_bytes()
}
pub fn bit_string(self) -> Result<BitString<'a>> {
self.try_into()
}
pub fn generalized_time(self) -> Result<GeneralizedTime> {
self.try_into()
}
pub fn ia5_string(self) -> Result<Ia5String<'a>> {
self.try_into()
}
pub fn octet_string(self) -> Result<OctetString<'a>> {
self.try_into()
}
#[cfg(feature = "oid")]
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
pub fn oid(self) -> Result<ObjectIdentifier> {
self.try_into()
}
pub fn printable_string(self) -> Result<PrintableString<'a>> {
self.try_into()
}
pub fn sequence<F, T>(self, f: F) -> Result<T>
where
F: FnOnce(&mut Decoder<'a>) -> Result<T>,
{
Sequence::try_from(self)?.decode_nested(f)
}
pub fn utc_time(self) -> Result<UtcTime> {
self.try_into()
}
pub fn utf8_string(self) -> Result<Utf8String<'a>> {
self.try_into()
}
}
impl<'a> Choice<'a> for Any<'a> {
fn can_decode(_: Tag) -> bool {
true
}
}
impl<'a> Decodable<'a> for Any<'a> {
fn decode(decoder: &mut Decoder<'a>) -> Result<Any<'a>> {
let header = Header::decode(decoder)?;
let tag = header.tag;
let value = decoder
.bytes(header.length)
.map_err(|_| ErrorKind::Length { tag })?;
Self::new(tag, value)
}
}
impl<'a> Encodable for Any<'a> {
fn encoded_len(&self) -> Result<Length> {
self.len().for_tlv()
}
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()> {
Header::new(self.tag, self.len())?.encode(encoder)?;
encoder.bytes(self.as_bytes())
}
}
impl<'a> TryFrom<&'a [u8]> for Any<'a> {
type Error = Error;
fn try_from(bytes: &'a [u8]) -> Result<Any<'a>> {
Any::from_der(bytes)
}
}