Skip to main content

imessage_database/tables/messages/models/
service.rs

1/*!
2 Service a message was sent over.
3*/
4
5use std::fmt::{Display, Formatter, Result};
6
7/// Defines different types of [services](https://support.apple.com/en-us/104972) we can receive messages from.
8#[derive(Debug, PartialEq, Eq)]
9pub enum Service<'a> {
10    /// iMessage.
11    #[allow(non_camel_case_types)]
12    iMessage,
13    /// SMS.
14    SMS,
15    /// RCS.
16    RCS,
17    /// A message sent via [satellite](https://support.apple.com/en-us/120930) (literally: `iMessageLite` in the database).
18    Satellite,
19    /// Unrecognized service name.
20    Other(&'a str),
21    /// Missing service field.
22    Unknown,
23}
24
25impl<'a> Service<'a> {
26    /// Map the database service name to a [`Service`] variant.
27    #[must_use]
28    pub fn from_name(service: Option<&'a str>) -> Self {
29        if let Some(service_name) = service {
30            return match service_name.trim() {
31                "iMessage" => Service::iMessage,
32                "iMessageLite" => Service::Satellite,
33                "SMS" => Service::SMS,
34                "rcs" | "RCS" => Service::RCS,
35                service_name => Service::Other(service_name),
36            };
37        }
38        Service::Unknown
39    }
40}
41
42impl Display for Service<'_> {
43    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
44        match self {
45            Service::iMessage => write!(fmt, "iMessage"),
46            Service::SMS => write!(fmt, "SMS"),
47            Service::RCS => write!(fmt, "RCS"),
48            Service::Satellite => write!(fmt, "Satellite"),
49            Service::Other(other) => write!(fmt, "{other}"),
50            Service::Unknown => write!(fmt, "Unknown"),
51        }
52    }
53}