use thiserror::Error;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum BMessageError {
#[error("missing required field: {0}")]
MissingField(&'static str),
#[error("unrecognised STATUS: {0}")]
UnknownStatus(String),
#[error("unrecognised TYPE: {0}")]
UnknownType(String),
#[error("unterminated section: {0}")]
UnterminatedSection(&'static str),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessageStatus {
Read,
Unread,
}
impl MessageStatus {
pub(super) const fn as_str(&self) -> &'static str {
match self {
Self::Read => "READ",
Self::Unread => "UNREAD",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessageType {
SmsGsm,
}
impl MessageType {
pub(super) const fn as_str() -> &'static str {
"SMS_GSM"
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BVCard {
pub name: String,
pub tel: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BBody {
pub encoding: String,
pub charset: String,
pub language: String,
pub text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BEnvelope {
pub recipients: Vec<BVCard>,
pub body: BBody,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BMessage {
status: MessageStatus,
type_: MessageType,
folder: String,
originator: Option<BVCard>,
envelope: BEnvelope,
}
impl BMessage {
#[must_use]
#[inline]
pub const fn status(&self) -> &MessageStatus {
&self.status
}
#[must_use]
#[inline]
pub fn folder(&self) -> &str {
&self.folder
}
#[must_use]
#[inline]
pub const fn originator(&self) -> Option<&BVCard> {
self.originator.as_ref()
}
#[must_use]
#[inline]
pub const fn envelope(&self) -> &BEnvelope {
&self.envelope
}
#[must_use]
pub fn outbound_sms(phone: &str, text: &str) -> Self {
Self {
status: MessageStatus::Unread,
type_: MessageType::SmsGsm,
folder: "telecom/msg/outbox".to_owned(),
originator: Some(BVCard::default()),
envelope: BEnvelope {
recipients: vec![BVCard { name: String::new(), tel: phone.to_owned() }],
body: BBody {
encoding: "8BIT".to_owned(),
charset: "UTF-8".to_owned(),
language: "UNKNOWN".to_owned(),
text: text.to_owned(),
},
},
}
}
pub(super) const fn from_parts(
status: MessageStatus,
type_: MessageType,
folder: String,
originator: Option<BVCard>,
envelope: BEnvelope,
) -> Self {
Self { status, type_, folder, originator, envelope }
}
}