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, Default)]
22pub enum BridgeType {
23    #[default]
24    Fcm,
25    Adm,
26    Apns,
27}
28
29impl Display for BridgeType {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(
32            f,
33            "{}",
34            match self {
35                BridgeType::Adm => "adm",
36                BridgeType::Apns => "apns",
37                BridgeType::Fcm => "fcm",
38            }
39        )
40    }
41}
42#[derive(Clone, Debug)]
43pub struct PushConfiguration {
44    /// host name:port
45    pub server_host: String,
46
47    /// http protocol (for mobile, bridged connections "https")
48    pub http_protocol: Protocol,
49
50    /// bridge protocol ("fcm")
51    pub bridge_type: BridgeType,
52
53    /// Sender/Application ID value
54    pub sender_id: String,
55
56    /// OS Path to the database
57    pub database_path: String,
58
59    /// Number of seconds between to rate limit
60    /// the verify connection call
61    /// defaults to 24 hours
62    pub verify_connection_rate_limiter: Option<u64>,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
66pub enum Protocol {
67    #[default]
68    Https,
69    Http,
70}
71
72impl Display for Protocol {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        write!(
75            f,
76            "{}",
77            match self {
78                Protocol::Http => "http",
79                Protocol::Https => "https",
80            }
81        )
82    }
83}
84
85impl FromStr for Protocol {
86    type Err = PushError;
87
88    fn from_str(s: &str) -> Result<Self, Self::Err> {
89        Ok(match s {
90            "http" => Protocol::Http,
91            "https" => Protocol::Https,
92            _ => return Err(PushError::GeneralError("Invalid protocol".to_string())),
93        })
94    }
95}
96
97#[cfg(test)]
98impl Default for PushConfiguration {
99    fn default() -> PushConfiguration {
100        PushConfiguration {
101            server_host: String::from("push.services.mozilla.com"),
102            http_protocol: Protocol::Https,
103            bridge_type: Default::default(),
104            sender_id: String::from(""),
105            database_path: String::from(""),
106            verify_connection_rate_limiter: Some(DEFAULT_VERIFY_CONNECTION_LIMITER_INTERVAL),
107        }
108    }
109}