#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LocationIndicator([u8; 2]);
impl LocationIndicator {
pub fn new(s: &str) -> Result<Self, Error> {
if s.len() != 2 || !s.is_ascii() {
return Err(Error::UnknownLocationIndicator(s.to_string()));
}
let bytes = s.as_bytes();
Ok(LocationIndicator([bytes[0], bytes[1]]))
}
pub fn as_str(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(&self.0) }
}
}
impl std::fmt::Display for LocationIndicator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl TryFrom<&str> for LocationIndicator {
type Error = Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::new(s)
}
}