use std::fmt;
use std::str::FromStr;
use crate::error::{DecodeError, ParseError};
use crate::location::UserLocation;
use crate::plmn::Mcc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SliceServiceType(u8);
impl SliceServiceType {
pub const EMBB: Self = Self(1);
pub const URLLC: Self = Self(2);
pub const MIOT: Self = Self(3);
pub const V2X: Self = Self(4);
#[must_use]
pub const fn new(value: u8) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u8 {
self.0
}
#[must_use]
pub const fn is_standardised(self) -> bool {
self.0 >= 1 && self.0 <= 4
}
#[must_use]
pub const fn name(self) -> Option<&'static str> {
match self.0 {
1 => Some("eMBB"),
2 => Some("URLLC"),
3 => Some("MIoT"),
4 => Some("V2X"),
_ => None,
}
}
}
impl fmt::Display for SliceServiceType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Snssai {
sst: SliceServiceType,
sd: Option<u32>,
}
impl Snssai {
pub const NO_SD: u32 = 0x00FF_FFFF;
#[must_use]
pub const fn new(sst: SliceServiceType) -> Self {
Self { sst, sd: None }
}
#[must_use]
pub const fn with_sd(sst: SliceServiceType, sd: u32) -> Option<Self> {
if sd > 0x00FF_FFFF || sd == Self::NO_SD {
return None;
}
Some(Self { sst, sd: Some(sd) })
}
#[must_use]
pub const fn sst(self) -> SliceServiceType {
self.sst
}
#[must_use]
pub const fn sd(self) -> Option<u32> {
self.sd
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
match bytes.len() {
1 => Ok(Self::new(SliceServiceType::new(bytes[0]))),
4 => {
let sd =
(u32::from(bytes[1]) << 16) | (u32::from(bytes[2]) << 8) | u32::from(bytes[3]);
let sst = SliceServiceType::new(bytes[0]);
Ok(if sd == Self::NO_SD {
Self::new(sst)
} else {
Self { sst, sd: Some(sd) }
})
}
_ => Err(DecodeError::new(0, "1 octet (SST) or 4 (SST and SD)")),
}
}
pub fn to_bytes(self, out: &mut [u8]) -> Result<usize, DecodeError> {
match self.sd {
None => {
if out.is_empty() {
return Err(DecodeError::new(0, "a buffer of at least 1 octet"));
}
out[0] = self.sst.get();
Ok(1)
}
Some(sd) => {
if out.len() < 4 {
return Err(DecodeError::new(0, "a buffer of at least 4 octets"));
}
out[0] = self.sst.get();
out[1] = ((sd >> 16) & 0xFF) as u8;
out[2] = ((sd >> 8) & 0xFF) as u8;
out[3] = (sd & 0xFF) as u8;
Ok(4)
}
}
}
}
impl fmt::Display for Snssai {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.sd {
None => write!(f, "{}", self.sst),
Some(sd) => write!(f, "{}-{sd:06x}", self.sst),
}
}
}
const SNSSAI_FORMAT: &str =
"a slice type, optionally with a six-hex-digit differentiator, such as 1 or 1-0000a1";
impl FromStr for Snssai {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let err = || ParseError::format("Snssai", s, SNSSAI_FORMAT);
let t = s.trim();
let (sst, sd) = match t.split_once('-') {
None => (t, None),
Some((sst, sd)) => (sst, Some(sd)),
};
let canonical_sst = !sst.is_empty()
&& sst.len() <= 3
&& sst.bytes().all(|b| b.is_ascii_digit())
&& (sst == "0" || !sst.starts_with('0'));
if !canonical_sst {
return Err(err());
}
let sst = SliceServiceType::new(sst.parse().map_err(|_| err())?);
match sd {
None => Ok(Self::new(sst)),
Some(sd) if sd.len() == 6 && sd.bytes().all(|b| b.is_ascii_hexdigit()) => {
let sd = u32::from_str_radix(sd, 16).map_err(|_| err())?;
Self::with_sd(sst, sd).ok_or_else(err)
}
Some(_) => Err(err()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Dnn(String);
const DNN_FORMAT: &str =
"up to 100 characters of dot-separated labels, such as internet or ims.mnc002.mcc262.gprs";
impl Dnn {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn labels(&self) -> std::str::Split<'_, char> {
self.0.split('.')
}
#[must_use]
pub fn network_identifier(&self) -> &str {
let name = &self.0[..];
let operator = |rest: &str| {
let three = |label: &str, tag: &str| {
label
.strip_prefix(tag)
.is_some_and(|d| d.len() == 3 && d.bytes().all(|b| b.is_ascii_digit()))
};
let mut labels = rest.split('.');
matches!(
(labels.next(), labels.next(), labels.next(), labels.next()),
(Some(mnc), Some(mcc), Some("gprs"), None) if three(mnc, "mnc") && three(mcc, "mcc")
)
};
name.match_indices('.')
.map(|(i, _)| i)
.find(|&i| operator(&name[i + 1..]))
.map_or(name, |i| &name[..i])
}
}
impl fmt::Display for Dnn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for Dnn {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let err = || ParseError::format("Dnn", s, DNN_FORMAT);
let t = s.trim();
let ok = !t.is_empty()
&& t.len() <= 100
&& !t.starts_with('.')
&& !t.ends_with('.')
&& t.split('.')
.all(|label| !label.is_empty() && label.len() <= 63)
&& t.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'.');
if ok {
Ok(Self(t.to_ascii_lowercase()))
} else {
Err(err())
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RatType {
Nr,
Eutran,
Utran,
Geran,
Wlan,
Wireline,
NonTerrestrial,
}
impl RatType {
pub const ALL: &'static [Self] = &[
Self::Nr,
Self::Eutran,
Self::Utran,
Self::Geran,
Self::Wlan,
Self::Wireline,
Self::NonTerrestrial,
];
pub const CODES: &'static [&'static str] = &[
"NR",
"EUTRAN",
"UTRAN",
"GERAN",
"WLAN",
"WIRELINE",
"NON_TERRESTRIAL",
];
#[must_use]
pub const fn jurisdiction_is_ambiguous(self) -> bool {
matches!(self, Self::NonTerrestrial)
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Nr => "NR",
Self::Eutran => "EUTRAN",
Self::Utran => "UTRAN",
Self::Geran => "GERAN",
Self::Wlan => "WLAN",
Self::Wireline => "WIRELINE",
Self::NonTerrestrial => "NON_TERRESTRIAL",
}
}
}
impl fmt::Display for RatType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for RatType {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let t = s.trim().to_ascii_uppercase();
Self::ALL
.iter()
.find(|r| r.as_str() == t)
.copied()
.ok_or_else(|| ParseError::one_of("RatType", s, Self::CODES))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ChargingCharacteristics(u16);
const CHARGING_CHARACTERISTICS_FORMAT: &str = "exactly four hexadecimal digits, such as 0800";
impl ChargingCharacteristics {
pub const OCTETS: usize = 2;
pub const NONE: Self = Self(0);
#[must_use]
pub const fn from_bits(bits: u16) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u16 {
self.0
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
match bytes {
[high, low] => Ok(Self(u16::from_be_bytes([*high, *low]))),
_ => Err(DecodeError::new(0, "exactly 2 octets")),
}
}
#[must_use]
pub const fn to_bytes(self) -> [u8; Self::OCTETS] {
self.0.to_be_bytes()
}
#[must_use]
pub const fn behaviour(self, index: u8) -> bool {
index < 16 && self.0 & (1u16 << index) != 0
}
pub fn set_behaviours(self) -> impl Iterator<Item = u8> {
(0u8..16).filter(move |index| self.behaviour(*index))
}
#[must_use]
pub const fn count(self) -> u32 {
self.0.count_ones()
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
}
impl fmt::Display for ChargingCharacteristics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04X}", self.0)
}
}
impl FromStr for ChargingCharacteristics {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let t = s.trim();
if t.len() != 4 || !t.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err(ParseError::format(
"ChargingCharacteristics",
s,
CHARGING_CHARACTERISTICS_FORMAT,
));
}
u16::from_str_radix(t, 16).map(Self).map_err(|_| {
ParseError::format(
"ChargingCharacteristics",
s,
CHARGING_CHARACTERISTICS_FORMAT,
)
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SessionContext {
pub dnn: Option<Dnn>,
pub snssai: Option<Snssai>,
pub rat: Option<RatType>,
pub location: Option<UserLocation>,
pub charging_characteristics: Option<ChargingCharacteristics>,
}
impl SessionContext {
#[must_use]
pub const fn none() -> Self {
Self {
dnn: None,
snssai: None,
rat: None,
location: None,
charging_characteristics: None,
}
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.dnn.is_none()
&& self.snssai.is_none()
&& self.rat.is_none()
&& self.location.is_none()
&& self.charging_characteristics.is_none()
}
#[must_use]
pub fn with_dnn(mut self, dnn: Dnn) -> Self {
self.dnn = Some(dnn);
self
}
#[must_use]
pub const fn with_snssai(mut self, snssai: Snssai) -> Self {
self.snssai = Some(snssai);
self
}
#[must_use]
pub const fn with_charging_characteristics(mut self, cc: ChargingCharacteristics) -> Self {
self.charging_characteristics = Some(cc);
self
}
#[must_use]
pub const fn with_rat(mut self, rat: RatType) -> Self {
self.rat = Some(rat);
self
}
#[must_use]
pub const fn with_location(mut self, location: UserLocation) -> Self {
self.location = Some(location);
self
}
#[must_use]
pub const fn serving_country(&self) -> Option<Mcc> {
match self.location {
Some(location) => Some(location.mcc()),
None => None,
}
}
#[must_use]
pub fn jurisdiction_is_ambiguous(&self) -> bool {
self.rat.is_some_and(RatType::jurisdiction_is_ambiguous)
}
}
impl fmt::Display for SessionContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut wrote = false;
for part in [
self.dnn.as_ref().map(ToString::to_string),
self.snssai.map(|s| format!("slice {s}")),
self.rat.map(|r| r.to_string()),
self.location.map(|l| l.to_string()),
self.charging_characteristics
.map(|c| format!("charging characteristics {c}")),
]
.into_iter()
.flatten()
{
if wrote {
f.write_str(" / ")?;
}
f.write_str(&part)?;
wrote = true;
}
if !wrote {
f.write_str("no context")?;
}
Ok(())
}
}
#[cfg(feature = "serde")]
mod serde_impl {
use super::{
ChargingCharacteristics, Dnn, RatType, SessionContext, SliceServiceType, Snssai,
UserLocation,
};
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
macro_rules! string_serde {
($ty:ty, $expecting:literal) => {
impl Serialize for $ty {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for $ty {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct V;
impl Visitor<'_> for V {
type Value = $ty;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str($expecting)
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<$ty, E> {
v.parse().map_err(de::Error::custom)
}
}
deserializer.deserialize_str(V)
}
}
};
}
string_serde!(Dnn, "a data network name such as \"internet\"");
string_serde!(RatType, "a radio access type code such as \"NR\"");
string_serde!(Snssai, "a slice identity such as \"1-0000a1\"");
string_serde!(
ChargingCharacteristics,
"four hexadecimal digits such as \"0800\""
);
impl Serialize for SliceServiceType {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u8(self.get())
}
}
impl<'de> Deserialize<'de> for SliceServiceType {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
u8::deserialize(deserializer).map(SliceServiceType::new)
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename = "SessionContext")]
struct Repr {
#[serde(default, skip_serializing_if = "Option::is_none")]
dnn: Option<Dnn>,
#[serde(default, skip_serializing_if = "Option::is_none")]
snssai: Option<Snssai>,
#[serde(default, skip_serializing_if = "Option::is_none")]
rat: Option<RatType>,
#[serde(default, skip_serializing_if = "Option::is_none")]
location: Option<UserLocation>,
#[serde(default, skip_serializing_if = "Option::is_none")]
charging_characteristics: Option<ChargingCharacteristics>,
}
impl Serialize for SessionContext {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Repr {
dnn: self.dnn.clone(),
snssai: self.snssai,
rat: self.rat,
location: self.location,
charging_characteristics: self.charging_characteristics,
}
.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for SessionContext {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let r = Repr::deserialize(deserializer)?;
Ok(SessionContext {
dnn: r.dnn,
snssai: r.snssai,
rat: r.rat,
location: r.location,
charging_characteristics: r.charging_characteristics,
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_absent_differentiator_is_not_a_zero_one() {
let bare = Snssai::from_bytes(&[0x01]).unwrap();
let reserved = Snssai::from_bytes(&[0x01, 0xFF, 0xFF, 0xFF]).unwrap();
let zero = Snssai::from_bytes(&[0x01, 0x00, 0x00, 0x00]).unwrap();
assert_eq!(bare, reserved, "the reserved value means no SD");
assert_eq!(bare.sd(), None);
assert_ne!(zero, bare);
assert_eq!(zero.sd(), Some(0));
}
#[test]
fn a_slice_with_no_differentiator_encodes_short() {
let bare = Snssai::new(SliceServiceType::EMBB);
let mut buffer = [0u8; 4];
assert_eq!(bare.to_bytes(&mut buffer).unwrap(), 1);
assert_eq!(buffer[0], 0x01);
assert_eq!(Snssai::with_sd(SliceServiceType::EMBB, Snssai::NO_SD), None);
}
#[test]
fn slice_identities_round_trip_over_the_wire() {
let cases = [
(Snssai::new(SliceServiceType::EMBB), 1usize),
(
Snssai::with_sd(SliceServiceType::URLLC, 0x00_00A1).unwrap(),
4,
),
(Snssai::with_sd(SliceServiceType::new(255), 0).unwrap(), 4),
(
Snssai::with_sd(SliceServiceType::MIOT, 0x00FF_FFFE).unwrap(),
4,
),
];
for (slice, expected_len) in cases {
let mut buffer = [0u8; 4];
let written = slice.to_bytes(&mut buffer).unwrap();
assert_eq!(written, expected_len, "{slice}");
assert_eq!(Snssai::from_bytes(&buffer[..written]).unwrap(), slice);
}
}
#[test]
fn slice_identities_round_trip_as_strings() {
for text in ["1", "2-0000a1", "255-000000", "3-fffffe"] {
let slice: Snssai = text.parse().unwrap();
assert_eq!(slice.to_string(), text);
}
for bad in ["", "1-", "1-abc", "1-0000A1G", "256", "1-ffffff"] {
assert!(bad.parse::<Snssai>().is_err(), "{bad:?} must not parse");
}
}
#[test]
fn only_the_two_defined_lengths_decode() {
assert!(Snssai::from_bytes(&[]).is_err());
assert!(Snssai::from_bytes(&[0x01, 0x00]).is_err());
assert!(Snssai::from_bytes(&[0x01, 0x00, 0x00]).is_err());
assert!(Snssai::from_bytes(&[0x01, 0x00, 0x00, 0x00, 0x00]).is_err());
}
#[test]
fn standardised_slice_types_are_named() {
assert_eq!(SliceServiceType::EMBB.name(), Some("eMBB"));
assert_eq!(SliceServiceType::V2X.name(), Some("V2X"));
assert_eq!(SliceServiceType::new(200).name(), None);
assert!(!SliceServiceType::new(200).is_standardised());
assert!(!SliceServiceType::new(0).is_standardised());
}
#[test]
fn a_data_network_name_has_one_spelling() {
let lower: Dnn = "internet".parse().unwrap();
for spelling in ["INTERNET", "Internet", " internet "] {
assert_eq!(spelling.parse::<Dnn>().unwrap(), lower);
}
assert_eq!(lower.to_string(), "internet");
}
#[test]
fn a_data_network_name_splits_into_labels() {
let dnn: Dnn = "ims.mnc002.mcc262.gprs".parse().unwrap();
assert_eq!(dnn.network_identifier(), "ims");
assert_eq!(dnn.labels().count(), 4);
let bare: Dnn = "internet".parse().unwrap();
assert_eq!(bare.network_identifier(), "internet");
}
#[test]
fn malformed_data_network_names_are_rejected() {
for bad in [
"",
".internet",
"internet.",
"a..b",
"has space",
"under_score",
] {
assert!(bad.parse::<Dnn>().is_err(), "{bad:?} must not parse");
}
assert!("x".repeat(101).parse::<Dnn>().is_err());
assert!("x".repeat(64).parse::<Dnn>().is_err());
let longest = format!("{}.{}", "x".repeat(63), "y".repeat(36));
assert!(longest.parse::<Dnn>().is_ok());
}
#[test]
fn only_non_terrestrial_access_makes_jurisdiction_ambiguous() {
for rat in RatType::ALL {
let expected = *rat == RatType::NonTerrestrial;
assert_eq!(rat.jurisdiction_is_ambiguous(), expected, "{rat}");
}
let orbit = SessionContext::none().with_rat(RatType::NonTerrestrial);
assert!(orbit.jurisdiction_is_ambiguous());
assert!(!SessionContext::none().jurisdiction_is_ambiguous());
}
#[test]
fn a_session_context_renders_what_it_has() {
assert_eq!(SessionContext::none().to_string(), "no context");
let full = SessionContext::none()
.with_dnn("internet".parse().unwrap())
.with_snssai(Snssai::with_sd(SliceServiceType::EMBB, 0xA1).unwrap())
.with_rat(RatType::Nr);
assert_eq!(full.to_string(), "internet / slice 1-0000a1 / NR");
assert!(!full.is_empty());
}
#[test]
fn a_location_yields_the_serving_country_and_nothing_finer() {
use crate::location::{Ncgi, Nci, UserLocation};
let cell = UserLocation::Ncgi(Ncgi::new(
"262-02".parse().unwrap(),
Nci::new(0x0_1234_5678).unwrap(),
));
let context = SessionContext::none()
.with_rat(RatType::Nr)
.with_location(cell);
assert_eq!(context.serving_country(), Some("262".parse().unwrap()));
assert!(context.to_string().contains("ncgi:262-02-"));
assert_eq!(SessionContext::none().serving_country(), None);
assert!(!SessionContext::none().with_location(cell).is_empty());
}
#[test]
fn rat_codes_round_trip() {
assert_eq!(RatType::ALL.len(), RatType::CODES.len());
for (rat, code) in RatType::ALL.iter().zip(RatType::CODES) {
assert_eq!(rat.as_str(), *code);
assert_eq!(code.parse::<RatType>().unwrap(), *rat);
}
assert!("SATELLITE".parse::<RatType>().is_err());
}
}