pub struct Id { /* private fields */ }
Expand description
Implementations§
source§impl Id
impl Id
sourcepub fn new(class: ClassTag, pc: PCTag, number: TagNumber) -> Self
pub fn new(class: ClassTag, pc: PCTag, number: TagNumber) -> Self
Creates a new instance from class
, pc
, and number
.
§Warnings
ASN.1 reserves some universal identifier numbers and they should not be used, however, this function ignores the rule. For example, number 15 (0x0f) is reserved for now, but this function accepts such a number without any error.
§Examples
use bsn1::{Id, IdRef};
let idref = IdRef::integer();
let id = Id::new(idref.class(), idref.pc(), idref.number().unwrap());
assert_eq!(idref, &id);
sourcepub fn parse<R>(readable: &mut R) -> Result<Self, Error>
pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
Parses readable
starting with identifier and tries to build a new instance.
This function ignores extra octet(s) at the end if any.
§Performance
This function is not so efficient compared with IdRef::parse
.
If you have a slice of serialized bytes, use IdRef::parse
and then call ToOwned::to_owned
instead.
§Warnings
ASN.1 reserves some universal identifier numbers and they should not be used, however,
this function ignores the rule.
For example, number 15 (0x0f) is reserved for now,
but this functions returns Ok
.
§Examples
use bsn1::{Id, IdRef};
// Serialize an 'identifier' representing 'utf8-string'
let id = IdRef::utf8_string();
let mut serialized = Vec::from(id.as_ref());
// Deserialize it.
let deserialized = Id::parse(&mut &serialized[..]).unwrap();
assert_eq!(id, &deserialized);
// Extra octets at the end does not affect the result.
serialized.push(0);
serialized.push(1);
let deserialized = Id::parse(&mut &serialized[..]).unwrap();
assert_eq!(id, &deserialized);
// We can use `IdRef::parse` instead. It is more efficient.
let deserialized: Id = IdRef::parse(&mut &serialized[..]).map(ToOwned::to_owned).unwrap();
assert_eq!(id, &deserialized);
sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
Provides a reference from bytes
without any check.
bytes
must not include any extra octet.
If it is not sure whether bytes
is valid octets as an identifer, use parse
instead.
§Safety
The behaviour is undefined if the format of bytes
is bad as ‘identifier octets’.
§Examples
use bsn1::{Id, IdRef};
let id = IdRef::eoc();
let serialized = id.as_ref();
let deserialized = unsafe { Id::from_bytes_unchecked(serialized) };
assert_eq!(deserialized, id);
sourcepub fn set_number(&mut self, num: TagNumber)
pub fn set_number(&mut self, num: TagNumber)
Update the number of self
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
let mut id = Id::new(ClassTag::Private, PCTag::Primitive, 13_u8.into());
assert_eq!(id.number().unwrap(), 13_u8.into());
id.set_number(34_u8.into());
assert_eq!(id.number().unwrap(), 34_u8.into());
Methods from Deref<Target = IdRef>§
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the byte count of the inner bytes.
§Examples
use bsn1::IdRef;
// Id of Universal Integer is [0x02]. It is 1 byte long.
let id = IdRef::integer();
assert_eq!(id.len(), 1);
sourcepub fn class(&self) -> ClassTag
pub fn class(&self) -> ClassTag
Returns the ClassTag
of self
.
§Examples
use bsn1::{ClassTag, IdRef};
// 'EOC' is defined as Universal class.
let eoc = IdRef::eoc();
assert_eq!(ClassTag::Universal, eoc.class());
sourcepub fn is_universal(&self) -> bool
pub fn is_universal(&self) -> bool
Returns true
if self
is ‘Universal class’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_universal());
sourcepub fn is_application(&self) -> bool
pub fn is_application(&self) -> bool
Returns true
if self
is ‘Application class’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Application, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_application());
sourcepub fn is_context_specific(&self) -> bool
pub fn is_context_specific(&self) -> bool
Returns true
if self
is ‘Context Specific class’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::ContextSpecific, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_context_specific());
sourcepub fn is_private(&self) -> bool
pub fn is_private(&self) -> bool
Returns true
if self
is ‘Private class’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Private, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_private());
sourcepub fn pc(&self) -> PCTag
pub fn pc(&self) -> PCTag
Returns the Primitive/Constructed flag of self
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(PCTag::Primitive, id.pc());
let id = Id::new(ClassTag::Application, PCTag::Constructed, 1_u8.into());
assert_eq!(PCTag::Constructed, id.pc());
sourcepub fn is_primitive(&self) -> bool
pub fn is_primitive(&self) -> bool
Returns true
if self
is flagged as ‘Primitive’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Primitive, 0_u8.into());
assert_eq!(true, id.is_primitive());
sourcepub fn is_constructed(&self) -> bool
pub fn is_constructed(&self) -> bool
Returns true
if self
is flagged as ‘Constructed’, or false
.
§Examples
use bsn1::{ClassTag, Id, PCTag};
// 'Id' implements 'Deref<Target = IdRef>'.
let id = Id::new(ClassTag::Universal, PCTag::Constructed, 0_u8.into());
assert_eq!(true, id.is_constructed());
sourcepub fn number(&self) -> Result<TagNumber, Error>
pub fn number(&self) -> Result<TagNumber, Error>
Returns the number of self
unless overflow.
§Examples
use bsn1::{ClassTag, Id, IdRef, PCTag};
use std::ops::Deref;
let id = Id::new(ClassTag::Application, PCTag::Primitive, 49_u8.into());
let idref: &IdRef = id.deref();
assert_eq!(49, idref.number().unwrap().get());
sourcepub fn set_class(&mut self, cls: ClassTag)
pub fn set_class(&mut self, cls: ClassTag)
Update the class of self
.
§Examples
use bsn1::{ClassTag, IdRef};
// Creates a '&mut IdRef' representing 'Universal Integer'.
let mut bytes = Vec::from(IdRef::integer().as_ref());
let idref = IdRef::parse_mut(&mut &mut bytes[..]).unwrap();
assert_eq!(ClassTag::Universal, idref.class());
idref.set_class(ClassTag::Application);
assert_eq!(ClassTag::Application, idref.class());
sourcepub fn set_pc(&mut self, pc: PCTag)
pub fn set_pc(&mut self, pc: PCTag)
Update the Primitive/Constructed flag of self
.
§Examples
use bsn1::{PCTag, IdRef};
let id = IdRef::integer();
let mut serialized = Vec::from(id.as_ref());
let deserialized = IdRef::parse_mut(&mut &mut serialized[..]).unwrap();
// 'Integer' is primitive.
assert_eq!(PCTag::Primitive, deserialized.pc());
// Force to change into constructed.
deserialized.set_pc(PCTag::Constructed);
assert_eq!(PCTag::Constructed, deserialized.pc());
Trait Implementations§
source§impl Ord for Id
impl Ord for Id
source§impl<T> PartialOrd<T> for Id
impl<T> PartialOrd<T> for Id
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more