use std::fmt;
use std::str::FromStr;
use crate::error::ParseError;
use crate::plmn::{Mcc, PlmnId};
const fn is_reserved_area(value: u32, bits: u8) -> bool {
value == 0 || value == (1u32 << bits) - 2
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Lac(u16);
impl Lac {
#[must_use]
pub const fn is_reserved(value: u16) -> bool {
is_reserved_area(value as u32, 16)
}
#[must_use]
pub const fn new(value: u16) -> Option<Self> {
if Self::is_reserved(value) {
None
} else {
Some(Self(value))
}
}
#[must_use]
pub const fn get(self) -> u16 {
self.0
}
}
impl fmt::Display for Lac {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04X}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CellId(u16);
impl CellId {
#[must_use]
pub const fn new(value: u16) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u16 {
self.0
}
}
impl fmt::Display for CellId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04X}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServiceAreaCode(u16);
impl ServiceAreaCode {
#[must_use]
pub const fn new(value: u16) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u16 {
self.0
}
}
impl fmt::Display for ServiceAreaCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04X}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Cgi {
plmn: PlmnId,
lac: Lac,
cell: CellId,
}
impl Cgi {
#[must_use]
pub const fn new(plmn: PlmnId, lac: Lac, cell: CellId) -> Self {
Self { plmn, lac, cell }
}
#[must_use]
pub const fn plmn(self) -> PlmnId {
self.plmn
}
#[must_use]
pub const fn lac(self) -> Lac {
self.lac
}
#[must_use]
pub const fn cell(self) -> CellId {
self.cell
}
}
impl fmt::Display for Cgi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}-{}", self.plmn, self.lac, self.cell)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sai {
plmn: PlmnId,
lac: Lac,
sac: ServiceAreaCode,
}
impl Sai {
#[must_use]
pub const fn new(plmn: PlmnId, lac: Lac, sac: ServiceAreaCode) -> Self {
Self { plmn, lac, sac }
}
#[must_use]
pub const fn plmn(self) -> PlmnId {
self.plmn
}
#[must_use]
pub const fn lac(self) -> Lac {
self.lac
}
#[must_use]
pub const fn sac(self) -> ServiceAreaCode {
self.sac
}
}
impl fmt::Display for Sai {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}-{}", self.plmn, self.lac, self.sac)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EnbKind {
Macro,
Home,
}
impl EnbKind {
pub const ALL: &'static [Self] = &[Self::Macro, Self::Home];
pub const CODES: &'static [&'static str] = &["MACRO", "HOME"];
#[must_use]
pub const fn id_bits(self) -> u8 {
match self {
Self::Macro => 20,
Self::Home => 28,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Macro => "MACRO",
Self::Home => "HOME",
}
}
}
impl fmt::Display for EnbKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for EnbKind {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::ALL
.iter()
.find(|k| k.as_str().eq_ignore_ascii_case(s.trim()))
.copied()
.ok_or_else(|| ParseError::format("EnbKind", s, "MACRO or HOME"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Eci(u32);
impl Eci {
pub const BITS: u32 = 28;
const LIMIT: u32 = 1 << Self::BITS;
#[must_use]
pub const fn new(value: u32) -> Option<Self> {
if value < Self::LIMIT {
Some(Self(value))
} else {
None
}
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
#[must_use]
pub const fn enb_id(self, kind: EnbKind) -> u32 {
match kind {
EnbKind::Macro => self.0 >> 8,
EnbKind::Home => self.0,
}
}
#[must_use]
pub const fn cell_id(self, kind: EnbKind) -> Option<u8> {
match kind {
EnbKind::Macro => Some((self.0 & 0xFF) as u8),
EnbKind::Home => None,
}
}
}
impl fmt::Display for Eci {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:07X}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ecgi {
plmn: PlmnId,
eci: Eci,
}
impl Ecgi {
#[must_use]
pub const fn new(plmn: PlmnId, eci: Eci) -> Self {
Self { plmn, eci }
}
#[must_use]
pub const fn plmn(self) -> PlmnId {
self.plmn
}
#[must_use]
pub const fn eci(self) -> Eci {
self.eci
}
}
impl fmt::Display for Ecgi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.plmn, self.eci)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GnbIdLength(u8);
impl GnbIdLength {
pub const MIN: u8 = 22;
pub const MAX: u8 = 32;
#[must_use]
pub const fn new(bits: u8) -> Option<Self> {
if bits >= Self::MIN && bits <= Self::MAX {
Some(Self(bits))
} else {
None
}
}
#[must_use]
pub const fn get(self) -> u8 {
self.0
}
#[must_use]
pub const fn cell_bits(self) -> u8 {
Nci::BITS as u8 - self.0
}
}
impl fmt::Display for GnbIdLength {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for GnbIdLength {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let err = || ParseError::format("GnbIdLength", s, "a bit count from 22 to 32");
Self::new(s.trim().parse::<u8>().map_err(|_| err())?).ok_or_else(err)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Nci(u64);
impl Nci {
pub const BITS: u32 = 36;
const LIMIT: u64 = 1 << Self::BITS;
#[must_use]
pub const fn new(value: u64) -> Option<Self> {
if value < Self::LIMIT {
Some(Self(value))
} else {
None
}
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
#[must_use]
pub const fn gnb_id(self, length: GnbIdLength) -> u64 {
self.0 >> length.cell_bits()
}
#[must_use]
pub const fn cell_id(self, length: GnbIdLength) -> u64 {
self.0 & ((1u64 << length.cell_bits()) - 1)
}
}
impl fmt::Display for Nci {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:09X}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ncgi {
plmn: PlmnId,
nci: Nci,
}
impl Ncgi {
#[must_use]
pub const fn new(plmn: PlmnId, nci: Nci) -> Self {
Self { plmn, nci }
}
#[must_use]
pub const fn plmn(self) -> PlmnId {
self.plmn
}
#[must_use]
pub const fn nci(self) -> Nci {
self.nci
}
}
impl fmt::Display for Ncgi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.plmn, self.nci)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TacWidth {
Eps,
FiveGs,
}
impl TacWidth {
pub const ALL: &'static [Self] = &[Self::Eps, Self::FiveGs];
pub const CODES: &'static [&'static str] = &["EPS", "5GS"];
#[must_use]
pub const fn bits(self) -> u8 {
match self {
Self::Eps => 16,
Self::FiveGs => 24,
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Eps => "EPS",
Self::FiveGs => "5GS",
}
}
}
impl fmt::Display for TacWidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for TacWidth {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::ALL
.iter()
.find(|w| w.as_str().eq_ignore_ascii_case(s.trim()))
.copied()
.ok_or_else(|| ParseError::format("TacWidth", s, "EPS or 5GS"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TrackingAreaCode {
value: u32,
width: TacWidth,
}
impl TrackingAreaCode {
#[must_use]
pub const fn eps(value: u16) -> Option<Self> {
if is_reserved_area(value as u32, TacWidth::Eps.bits()) {
None
} else {
Some(Self {
value: value as u32,
width: TacWidth::Eps,
})
}
}
#[must_use]
pub const fn five_gs(value: u32) -> Option<Self> {
if value > 0x00FF_FFFF || is_reserved_area(value, TacWidth::FiveGs.bits()) {
None
} else {
Some(Self {
value,
width: TacWidth::FiveGs,
})
}
}
#[must_use]
pub const fn get(self) -> u32 {
self.value
}
#[must_use]
pub const fn width(self) -> TacWidth {
self.width
}
#[must_use]
pub const fn widened(self) -> Self {
Self {
value: self.value,
width: TacWidth::FiveGs,
}
}
#[must_use]
pub const fn is_same_area(self, other: Self) -> bool {
self.value == other.value
}
}
impl fmt::Display for TrackingAreaCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.width {
TacWidth::Eps => write!(f, "{:04X}", self.value),
TacWidth::FiveGs => write!(f, "{:06X}", self.value),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tai {
plmn: PlmnId,
tac: TrackingAreaCode,
}
impl Tai {
#[must_use]
pub const fn new(plmn: PlmnId, tac: TrackingAreaCode) -> Self {
Self { plmn, tac }
}
#[must_use]
pub const fn plmn(self) -> PlmnId {
self.plmn
}
#[must_use]
pub const fn tac(self) -> TrackingAreaCode {
self.tac
}
}
impl fmt::Display for Tai {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.plmn, self.tac)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UserLocation {
Cgi(Cgi),
Sai(Sai),
Ecgi(Ecgi),
Ncgi(Ncgi),
Tai(Tai),
}
impl UserLocation {
#[must_use]
pub const fn plmn(self) -> PlmnId {
match self {
Self::Cgi(v) => v.plmn(),
Self::Sai(v) => v.plmn(),
Self::Ecgi(v) => v.plmn(),
Self::Ncgi(v) => v.plmn(),
Self::Tai(v) => v.plmn(),
}
}
#[must_use]
pub const fn mcc(self) -> Mcc {
self.plmn().mcc()
}
#[must_use]
pub const fn is_cell(self) -> bool {
!matches!(self, Self::Tai(_))
}
#[must_use]
pub const fn kind(self) -> &'static str {
match self {
Self::Cgi(_) => "CGI",
Self::Sai(_) => "SAI",
Self::Ecgi(_) => "ECGI",
Self::Ncgi(_) => "NCGI",
Self::Tai(_) => "TAI",
}
}
}
impl fmt::Display for UserLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cgi(v) => write!(f, "cgi:{v}"),
Self::Sai(v) => write!(f, "sai:{v}"),
Self::Ecgi(v) => write!(f, "ecgi:{v}"),
Self::Ncgi(v) => write!(f, "ncgi:{v}"),
Self::Tai(v) => write!(f, "tai:{v}"),
}
}
}
#[cfg(feature = "serde")]
mod serde_impl {
use super::{
CellId, Cgi, Ecgi, Eci, EnbKind, GnbIdLength, Lac, Ncgi, Nci, Sai, ServiceAreaCode,
TacWidth, Tai, TrackingAreaCode, UserLocation,
};
use crate::plmn::PlmnId;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
macro_rules! string_enum {
($t:ty, $expecting:literal) => {
impl Serialize for $t {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for $t {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
struct V;
impl Visitor<'_> for V {
type Value = $t;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str($expecting)
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<$t, E> {
v.parse().map_err(de::Error::custom)
}
}
d.deserialize_str(V)
}
}
};
}
string_enum!(EnbKind, "MACRO or HOME");
string_enum!(TacWidth, "EPS or 5GS");
macro_rules! checked_number {
($t:ty, $raw:ty, $ctor:expr, $expecting:literal) => {
impl Serialize for $t {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.get().serialize(s)
}
}
impl<'de> Deserialize<'de> for $t {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let raw = <$raw>::deserialize(d)?;
#[allow(clippy::redundant_closure_call)]
($ctor)(raw)
.ok_or_else(|| de::Error::custom(format!("{raw} is not {}", $expecting)))
}
}
};
}
checked_number!(
Lac,
u16,
Lac::new,
"a location area code (0000 and FFFE are reserved)"
);
checked_number!(Eci, u32, Eci::new, "a 28-bit E-UTRAN cell identity");
checked_number!(Nci, u64, Nci::new, "a 36-bit NR cell identity");
checked_number!(
GnbIdLength,
u8,
GnbIdLength::new,
"a gNB-ID length of 22 to 32 bits"
);
macro_rules! plain_number {
($t:ty, $raw:ty, $ctor:expr) => {
impl Serialize for $t {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.get().serialize(s)
}
}
impl<'de> Deserialize<'de> for $t {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
#[allow(clippy::redundant_closure_call)]
Ok(($ctor)(<$raw>::deserialize(d)?))
}
}
};
}
plain_number!(CellId, u16, CellId::new);
plain_number!(ServiceAreaCode, u16, ServiceAreaCode::new);
#[derive(Serialize, Deserialize)]
#[serde(rename = "TrackingAreaCode")]
struct TacRepr {
value: u32,
width: TacWidth,
}
impl Serialize for TrackingAreaCode {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
TacRepr {
value: self.get(),
width: self.width(),
}
.serialize(s)
}
}
impl<'de> Deserialize<'de> for TrackingAreaCode {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let r = TacRepr::deserialize(d)?;
let built = match r.width {
TacWidth::Eps => u16::try_from(r.value).ok().and_then(TrackingAreaCode::eps),
TacWidth::FiveGs => TrackingAreaCode::five_gs(r.value),
};
built.ok_or_else(|| {
de::Error::custom(format!(
"{} is not a valid {} tracking area code",
r.value, r.width
))
})
}
}
macro_rules! composite {
($t:ty, $name:literal, { $($field:ident : $ty:ty),+ $(,)? }) => {
#[derive(Serialize, Deserialize)]
#[serde(rename = $name)]
struct Repr {
$($field: $ty),+
}
impl Serialize for $t {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
Repr { $($field: self.$field()),+ }.serialize(s)
}
}
impl<'de> Deserialize<'de> for $t {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let r = Repr::deserialize(d)?;
Ok(<$t>::new($(r.$field),+))
}
}
};
}
mod cgi {
use super::*;
composite!(Cgi, "Cgi", { plmn: PlmnId, lac: Lac, cell: CellId });
}
mod sai {
use super::*;
composite!(Sai, "Sai", { plmn: PlmnId, lac: Lac, sac: ServiceAreaCode });
}
mod ecgi {
use super::*;
composite!(Ecgi, "Ecgi", { plmn: PlmnId, eci: Eci });
}
mod ncgi {
use super::*;
composite!(Ncgi, "Ncgi", { plmn: PlmnId, nci: Nci });
}
mod tai {
use super::*;
composite!(Tai, "Tai", { plmn: PlmnId, tac: TrackingAreaCode });
}
#[derive(Serialize, Deserialize)]
#[serde(tag = "kind", rename = "UserLocation")]
enum LocationRepr {
#[serde(rename = "CGI")]
Cgi(Cgi),
#[serde(rename = "SAI")]
Sai(Sai),
#[serde(rename = "ECGI")]
Ecgi(Ecgi),
#[serde(rename = "NCGI")]
Ncgi(Ncgi),
#[serde(rename = "TAI")]
Tai(Tai),
}
impl Serialize for UserLocation {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
match *self {
UserLocation::Cgi(v) => LocationRepr::Cgi(v),
UserLocation::Sai(v) => LocationRepr::Sai(v),
UserLocation::Ecgi(v) => LocationRepr::Ecgi(v),
UserLocation::Ncgi(v) => LocationRepr::Ncgi(v),
UserLocation::Tai(v) => LocationRepr::Tai(v),
}
.serialize(s)
}
}
impl<'de> Deserialize<'de> for UserLocation {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(match LocationRepr::deserialize(d)? {
LocationRepr::Cgi(v) => UserLocation::Cgi(v),
LocationRepr::Sai(v) => UserLocation::Sai(v),
LocationRepr::Ecgi(v) => UserLocation::Ecgi(v),
LocationRepr::Ncgi(v) => UserLocation::Ncgi(v),
LocationRepr::Tai(v) => UserLocation::Tai(v),
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn plmn() -> PlmnId {
"262-02".parse().expect("a valid network")
}
#[test]
fn the_two_reserved_location_areas_are_rejected() {
assert!(Lac::new(0x0000).is_none());
assert!(Lac::new(0xFFFE).is_none());
assert!(Lac::new(0x0001).is_some());
assert!(Lac::new(0xFFFF).is_some(), "only the two are reserved");
assert!(Lac::is_reserved(0xFFFE));
assert!(!Lac::is_reserved(0xFFFF));
assert_eq!(
(0u32..=0xFFFF)
.filter(|v| super::is_reserved_area(*v, 16))
.count(),
2
);
assert!(super::is_reserved_area(0x00FF_FFFE, 24));
assert!(!super::is_reserved_area(0x0000_FFFE, 24));
assert_eq!(Lac::new(0x00AB).unwrap().to_string(), "00AB");
}
#[test]
fn the_same_two_values_are_reserved_for_tracking_areas() {
assert!(TrackingAreaCode::eps(0x0000).is_none());
assert!(TrackingAreaCode::eps(0xFFFE).is_none());
assert!(TrackingAreaCode::five_gs(0x00_0000).is_none());
assert!(TrackingAreaCode::five_gs(0xFF_FFFE).is_none());
assert!(TrackingAreaCode::five_gs(0x01_0000).is_some());
assert!(
TrackingAreaCode::five_gs(0x0100_0000).is_none(),
"more than 24 bits"
);
}
#[test]
fn a_cell_identity_splits_differently_for_a_home_enodeb() {
let eci = Eci::new(0x00A1_B2C3).unwrap();
assert_eq!(eci.enb_id(EnbKind::Macro), 0x0000_A1B2);
assert_eq!(eci.cell_id(EnbKind::Macro), Some(0xC3));
assert_eq!(eci.enb_id(EnbKind::Home), 0x00A1_B2C3);
assert_eq!(
eci.cell_id(EnbKind::Home),
None,
"a 28-bit home identifier leaves no cell part"
);
assert_eq!(EnbKind::Macro.id_bits() + 8, EnbKind::Home.id_bits());
}
#[test]
fn an_nr_cell_identity_needs_the_configured_length() {
let nci = Nci::new(0x0_1234_5678).unwrap();
let short = GnbIdLength::new(22).unwrap();
let long = GnbIdLength::new(32).unwrap();
assert_eq!(short.cell_bits(), 14);
assert_eq!(long.cell_bits(), 4);
assert_ne!(nci.gnb_id(short), nci.gnb_id(long));
for bits in GnbIdLength::MIN..=GnbIdLength::MAX {
let length = GnbIdLength::new(bits).unwrap();
let rebuilt = (nci.gnb_id(length) << length.cell_bits()) | nci.cell_id(length);
assert_eq!(rebuilt, nci.get(), "at {bits} bits");
}
}
#[test]
fn identities_are_bounded_by_their_bit_widths() {
assert!(Eci::new((1 << 28) - 1).is_some());
assert!(Eci::new(1 << 28).is_none());
assert!(Nci::new((1 << 36) - 1).is_some());
assert!(Nci::new(1 << 36).is_none());
assert!(GnbIdLength::new(21).is_none());
assert!(GnbIdLength::new(33).is_none());
assert_eq!(GnbIdLength::new(22).unwrap().get(), 22);
}
#[test]
fn a_tracking_area_carries_the_width_it_was_written_at() {
let eps = TrackingAreaCode::eps(0x1234).unwrap();
let five_gs = TrackingAreaCode::five_gs(0x00_1234).unwrap();
assert_ne!(eps, five_gs, "different widths are different values");
assert_eq!(eps.widened(), five_gs, "and this is the way across");
assert!(eps.is_same_area(five_gs));
assert_eq!(eps.to_string(), "1234");
assert_eq!(five_gs.to_string(), "001234");
assert_eq!(eps.width(), TacWidth::Eps);
assert_eq!(TacWidth::Eps.bits() + 8, TacWidth::FiveGs.bits());
}
#[test]
fn every_location_form_yields_a_country() {
let lac = Lac::new(0x1234).unwrap();
let forms = [
UserLocation::Cgi(Cgi::new(plmn(), lac, CellId::new(0xABCD))),
UserLocation::Sai(Sai::new(plmn(), lac, ServiceAreaCode::new(0xABCD))),
UserLocation::Ecgi(Ecgi::new(plmn(), Eci::new(0x0012_3456).unwrap())),
UserLocation::Ncgi(Ncgi::new(plmn(), Nci::new(0x0_1234_5678).unwrap())),
UserLocation::Tai(Tai::new(
plmn(),
TrackingAreaCode::five_gs(0x00_1234).unwrap(),
)),
];
for form in forms {
assert_eq!(form.mcc(), plmn().mcc());
assert_eq!(form.plmn(), plmn());
}
assert!(forms[0].is_cell());
assert!(!forms[4].is_cell());
}
#[test]
fn the_forms_are_distinguishable_as_strings() {
let lac = Lac::new(0x1234).unwrap();
let cgi = UserLocation::Cgi(Cgi::new(plmn(), lac, CellId::new(0xABCD)));
let sai = UserLocation::Sai(Sai::new(plmn(), lac, ServiceAreaCode::new(0xABCD)));
assert_ne!(cgi.to_string(), sai.to_string());
assert_eq!(cgi.to_string(), "cgi:262-02-1234-ABCD");
assert_eq!(sai.to_string(), "sai:262-02-1234-ABCD");
assert_eq!(cgi.kind(), "CGI");
}
#[test]
fn identities_render_at_their_full_width() {
assert_eq!(Eci::new(0x1).unwrap().to_string(), "0000001");
assert_eq!(Nci::new(0x1).unwrap().to_string(), "000000001");
assert_eq!(CellId::new(0x1).to_string(), "0001");
assert_eq!(ServiceAreaCode::new(0x1).to_string(), "0001");
}
#[test]
fn vocabulary_codes_round_trip() {
for (kind, code) in EnbKind::ALL.iter().zip(EnbKind::CODES) {
assert_eq!(kind.as_str(), *code);
assert_eq!(code.parse::<EnbKind>().unwrap(), *kind);
}
for (width, code) in TacWidth::ALL.iter().zip(TacWidth::CODES) {
assert_eq!(width.as_str(), *code);
assert_eq!(code.parse::<TacWidth>().unwrap(), *width);
}
assert_eq!("24".parse::<GnbIdLength>().unwrap().get(), 24);
assert!("21".parse::<GnbIdLength>().is_err());
assert!("LTE".parse::<TacWidth>().is_err());
}
}