push/internal/
config.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! Provides configuration for the [PushManager](`crate::PushManager`)
6//!
7
8use std::{fmt::Display, str::FromStr};
9
10pub const DEFAULT_VERIFY_CONNECTION_LIMITER_INTERVAL: u64 = 24 * 60 * 60; // 24 hours.
11
12use crate::PushError;
13/// The types of supported native bridges.
14///
15/// FCM = Google Android Firebase Cloud Messaging
16/// ADM = Amazon Device Messaging for FireTV
17/// APNS = Apple Push Notification System for iOS
18///
19/// Please contact services back-end for any additional bridge protocols.
20///
21#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
22pub enum BridgeType {
23    Fcm,
24    Adm,
25    Apns,
26}
27
28#[cfg(test)]
29// To avoid a future footgun, the default implementation is only for tests
30impl Default for BridgeType {
31    fn default() -> Self {
32        Self::Fcm
33    }
34}
35
36impl Display for BridgeType {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(
39            f,
40            "{}",
41            match self {
42                BridgeType::Adm => "adm",
43                BridgeType::Apns => "apns",
44                BridgeType::Fcm => "fcm",
45            }
46        )
47    }
48}
49#[derive(Clone, Debug)]
50pub struct PushConfiguration {
51    /// host name:port
52    pub server_host: String,
53
54    /// http protocol (for mobile, bridged connections "https")
55    pub http_protocol: Protocol,
56
57    /// bridge protocol ("fcm")
58    pub bridge_type: BridgeType,
59
60    /// Sender/Application ID value
61    pub sender_id: String,
62
63    /// OS Path to the database
64    pub database_path: String,
65
66    /// Number of seconds between to rate limit
67    /// the verify connection call
68    /// defaults to 24 hours
69    pub verify_connection_rate_limiter: Option<u64>,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
73pub enum Protocol {
74    #[default]
75    Https,
76    Http,
77}
78
79impl Display for Protocol {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(
82            f,
83            "{}",
84            match self {
85                Protocol::Http => "http",
86                Protocol::Https => "https",
87            }
88        )
89    }
90}
91
92impl FromStr for Protocol {
93    type Err = PushError;
94
95    fn from_str(s: &str) -> Result<Self, Self::Err> {
96        Ok(match s {
97            "http" => Protocol::Http,
98            "https" => Protocol::Https,
99            _ => return Err(PushError::GeneralError("Invalid protocol".to_string())),
100        })
101    }
102}
103
104#[cfg(test)]
105impl Default for PushConfiguration {
106    fn default() -> PushConfiguration {
107        PushConfiguration {
108            server_host: String::from("push.services.mozilla.com"),
109            http_protocol: Protocol::Https,
110            bridge_type: Default::default(),
111            sender_id: String::from(""),
112            database_path: String::from(""),
113            verify_connection_rate_limiter: Some(DEFAULT_VERIFY_CONNECTION_LIMITER_INTERVAL),
114        }
115    }
116}