pub struct TagPeeker {
pub next_tag: Tag,
}
Expand description
Used to peek next tag by using Deserializer::deserialize_identifier
.
Can be used to implement ASN.1 Choice.
§Examples
use serde::de;
use picky_asn1::{
wrapper::{IntegerAsn1, Utf8StringAsn1},
tag::{Tag, TagPeeker},
};
use std::fmt;
pub enum MyChoice {
Integer(u32),
Utf8String(String),
}
impl<'de> de::Deserialize<'de> for MyChoice {
fn deserialize<D>(deserializer: D) -> Result<Self, <D as de::Deserializer<'de>>::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = MyChoice;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid MyChoice")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
match seq.next_element::<TagPeeker>()?.unwrap().next_tag {
Tag::INTEGER => {
let value = seq.next_element::<u32>()?.unwrap();
Ok(MyChoice::Integer(value))
}
Tag::UTF8_STRING => {
let value = seq.next_element::<String>()?.unwrap();
Ok(MyChoice::Utf8String(value))
}
_ => Err(de::Error::invalid_value(
de::Unexpected::Other(
"[MyChoice] unsupported or unknown choice value",
),
&"a supported choice value",
))
}
}
}
deserializer.deserialize_enum("MyChoice", &["Integer", "Utf8String"], Visitor)
}
}
let buffer = b"\x0C\x06\xE8\x8B\x97\xE5\xAD\x97";
let my_choice: MyChoice = picky_asn1_der::from_bytes(buffer).unwrap();
match my_choice {
MyChoice::Integer(_) => panic!("wrong variant"),
MyChoice::Utf8String(string) => assert_eq!(string, "苗字"),
}
Fields§
§next_tag: Tag
Trait Implementations§
Source§impl<'de> Deserialize<'de> for TagPeeker
impl<'de> Deserialize<'de> for TagPeeker
Source§fn deserialize<D>(deserializer: D) -> Result<TagPeeker, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<TagPeeker, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Ord for TagPeeker
impl Ord for TagPeeker
Source§impl PartialOrd for TagPeeker
impl PartialOrd for TagPeeker
impl Eq for TagPeeker
impl StructuralPartialEq for TagPeeker
Auto Trait Implementations§
impl Freeze for TagPeeker
impl RefUnwindSafe for TagPeeker
impl Send for TagPeeker
impl Sync for TagPeeker
impl Unpin for TagPeeker
impl UnwindSafe for TagPeeker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more