use super::{Error, Fault};
macro_rules! type_of_identity {
(
$(#[$doc:meta])*
$name:ident, $member:literal { $($(#[$variant_doc:meta])* $variant:ident = $bits:literal,)* }
) => {
$(#[$doc])*
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum $name {
$($(#[$variant_doc])* $variant = $bits,)*
}
impl $name {
pub fn bits(self) -> u8 {
self as u8
}
pub fn of(first_octet: u8) -> Result<Self, Error> {
Self::try_from(first_octet & 0x07)
}
}
impl TryFrom<u8> for $name {
type Error = Error;
fn try_from(bits: u8) -> Result<Self, Error> {
match bits {
$($bits => Ok(Self::$variant),)*
given => {
let expected = concat!("a type of identity of ", $member);
let given = given.into();
Err(Error::new("type-of-identity", Fault::Value { given, expected }))
}
}
}
}
impl From<$name> for u8 {
fn from(kind: $name) -> u8 {
kind as u8
}
}
};
}
type_of_identity! {
MobileIdentityType5gs, "the 5GS mobile identity" {
NoIdentity = 0,
Suci = 1,
Guti = 2,
Imei = 3,
STmsi = 4,
Imeisv = 5,
MacAddress = 6,
Eui64 = 7,
}
}
type_of_identity! {
MobileIdentityTypeEps, "the EPS mobile identity" {
Imsi = 1,
Imei = 3,
Guti = 6,
}
}
type_of_identity! {
IdentityType, "the mobile identity of TS 24.008" {
NoIdentity = 0,
Imsi = 1,
Imei = 2,
Imeisv = 3,
Tmsi = 4,
Tmgi = 5,
}
}