From 59c8a7a94021311030df745c54224c3dc5d408b6 Mon Sep 17 00:00:00 2001 From: adnanjpg Date: Mon, 22 Jan 2024 19:12:52 +0300 Subject: [PATCH] fix docs tests and add default impl to all --- examples/simple_sender.rs | 22 ++++++--- src/android/android_config.rs | 2 +- src/android/android_fcm_options.rs | 2 +- src/android/android_notification.rs | 2 +- src/android/color.rs | 2 +- src/android/light_settings.rs | 2 +- src/apns/apns_config.rs | 2 +- src/apns/apns_fcm_options.rs | 2 +- src/lib.rs | 71 +++++++++++------------------ src/message/fcm_options.rs | 2 +- src/message/mod.rs | 13 +----- src/notification/mod.rs | 13 +----- src/web/webpush_config.rs | 2 +- src/web/webpush_fcm_options.rs | 2 +- 14 files changed, 55 insertions(+), 84 deletions(-) diff --git a/examples/simple_sender.rs b/examples/simple_sender.rs index 934b24e99..e5fc85300 100644 --- a/examples/simple_sender.rs +++ b/examples/simple_sender.rs @@ -1,7 +1,9 @@ // cargo run --example simple_sender -- -t use argparse::{ArgumentParser, Store}; -use fcm::{Client, FcmOptions, Message, Notification, Target}; +use fcm::{ + AndroidConfig, AndroidNotification, ApnsConfig, Client, FcmOptions, Message, Notification, Target, WebpushConfig, +}; use serde_json::json; #[tokio::main] @@ -27,17 +29,25 @@ async fn main() -> Result<(), Box> { let builder = Message { data: Some(data), notification: Some(Notification { - title: Some("Hello".to_string()), + title: Some("I'm high".to_string()), body: Some(format!("it's {}", chrono::Utc::now())), - image: None, + ..Default::default() }), target: Target::Token(device_token), - android: None, - webpush: None, - apns: None, fcm_options: Some(FcmOptions { analytics_label: "analytics_label".to_string(), }), + android: Some(AndroidConfig { + priority: Some(fcm::AndroidMessagePriority::High), + notification: Some(AndroidNotification { + title: Some("I'm Android high".to_string()), + body: Some(format!("Hi Android, it's {}", chrono::Utc::now())), + ..Default::default() + }), + ..Default::default() + }), + apns: Some(ApnsConfig { ..Default::default() }), + webpush: Some(WebpushConfig { ..Default::default() }), }; let response = client.send(builder).await?; diff --git a/src/android/android_config.rs b/src/android/android_config.rs index de9c213ef..938cf9508 100644 --- a/src/android/android_config.rs +++ b/src/android/android_config.rs @@ -45,7 +45,7 @@ pub(crate) struct AndroidConfigInternal { direct_boot_ok: Option, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct AndroidConfig { pub collapse_key: Option, pub priority: Option, diff --git a/src/android/android_fcm_options.rs b/src/android/android_fcm_options.rs index 830e12b56..4864af376 100644 --- a/src/android/android_fcm_options.rs +++ b/src/android/android_fcm_options.rs @@ -7,7 +7,7 @@ pub(crate) struct AndroidFcmOptionsInternal { analytics_label: String, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct AndroidFcmOptions { pub analytics_label: String, } diff --git a/src/android/android_notification.rs b/src/android/android_notification.rs index 7bf4546be..6d4105349 100644 --- a/src/android/android_notification.rs +++ b/src/android/android_notification.rs @@ -116,7 +116,7 @@ pub(crate) struct AndroidNotificationInternal { image: Option, } -#[derive(Debug)] +#[derive(Debug, Default)] // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#androidnotification pub struct AndroidNotification { // The notification's title. diff --git a/src/android/color.rs b/src/android/color.rs index d9096cab3..61aeda3a6 100644 --- a/src/android/color.rs +++ b/src/android/color.rs @@ -16,7 +16,7 @@ pub(crate) struct ColorInternal { alpha: f32, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Color { // The amount of red in the color as a value in the interval [0, 1]. pub red: f32, diff --git a/src/android/light_settings.rs b/src/android/light_settings.rs index 0b991b312..1ec4e1a1b 100644 --- a/src/android/light_settings.rs +++ b/src/android/light_settings.rs @@ -17,7 +17,7 @@ pub(crate) struct LightSettingsInternal { light_off_duration: String, } -#[derive(Debug)] +#[derive(Debug, Default)] // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#LightSettings pub struct LightSettings { // Set color of the LED with google.type.Color. diff --git a/src/apns/apns_config.rs b/src/apns/apns_config.rs index 294ed670b..b5ccd1b74 100644 --- a/src/apns/apns_config.rs +++ b/src/apns/apns_config.rs @@ -19,7 +19,7 @@ pub(crate) struct ApnsConfigInternal { fcm_options: Option, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct ApnsConfig { pub headers: Option, pub payload: Option, diff --git a/src/apns/apns_fcm_options.rs b/src/apns/apns_fcm_options.rs index c680eaceb..1ac76e74b 100644 --- a/src/apns/apns_fcm_options.rs +++ b/src/apns/apns_fcm_options.rs @@ -10,7 +10,7 @@ pub(crate) struct ApnsFcmOptionsInternal { image: Option, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct ApnsFcmOptions { // Label associated with the message's analytics data. pub analytics_label: Option, diff --git a/src/lib.rs b/src/lib.rs index 5a6247968..259f34f97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,55 +9,38 @@ //! To send out a FCM Message with some custom data: //! //! ```no_run -//! # use std::collections::HashMap; -//! # #[tokio::main] -//! # async fn main() -> Result<(), Box> { -//! use fcm::Target; -//! let client = fcm::Client::new(); //! -//! let mut map = HashMap::new(); -//! map.insert("message", "Howdy!"); +//! #[tokio::main] +//! async fn main() -> Result<(), Box> { +//! use serde_json::json; +//! use fcm::{Target, FcmOptions, Notification, Message}; +//! let client = fcm::Client::new(); //! -//! let mut builder = fcm::MessageBuilder::new(Target::Token("token".to_string())); -//! builder.data(&map); +//! let data = json!({ +//! "message": "Howdy!" +//! }); //! -//! let response = client.send(builder.finalize()).await?; -//! println!("Sent: {:?}", response); -//! # Ok(()) -//! # } -//! ``` -//! -//! To send a message using FCM Notifications, we first build the notification: -//! -//! ```rust -//! # fn main() { -//! let mut builder = fcm::NotificationBuilder::new(); -//! builder.title("Hey!".to_string()); -//! builder.body("Do you want to catch up later?".to_string()); -//! let notification = builder.finalize(); -//! # } -//! ``` -//! -//! And then set it in the message, before sending it: -//! -//! ```no_run -//! # #[tokio::main] -//! # async fn main() -> Result<(), Box> { -//! use fcm::Target; -//! let client = fcm::Client::new(); -//! -//! let mut notification_builder = fcm::NotificationBuilder::new(); -//! notification_builder.title("Hey!".to_string()); -//! notification_builder.body("Do you want to catch up later?".to_string()); +//! let builder = Message { +//! data: Some(data), +//! notification: Some(Notification { +//! title: Some("Hello".to_string()), +//! body: Some(format!("it's {}", chrono::Utc::now())), +//! image: None, +//! }), +//! target: Target::Token("token".to_string()), +//! android: None, +//! webpush: None, +//! apns: None, +//! fcm_options: Some(FcmOptions { +//! analytics_label: "analytics_label".to_string(), +//! }), +//! }; //! -//! let notification = notification_builder.finalize(); -//! let mut message_builder = fcm::MessageBuilder::new(Target::Token("token".to_string())); -//! message_builder.notification(notification); +//! let response = client.send(builder).await?; +//! println!("Sent: {:?}", response); //! -//! let response = client.send(message_builder.finalize()).await?; -//! println!("Sent: {:?}", response); -//! # Ok(()) -//! # } +//! Ok(()) +//! } //! ``` mod message; diff --git a/src/message/fcm_options.rs b/src/message/fcm_options.rs index 0b26aeb47..3c2a50855 100644 --- a/src/message/fcm_options.rs +++ b/src/message/fcm_options.rs @@ -7,7 +7,7 @@ pub(crate) struct FcmOptionsInternal { analytics_label: String, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct FcmOptions { pub analytics_label: String, } diff --git a/src/message/mod.rs b/src/message/mod.rs index a3a2801fd..a84d7834b 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -67,18 +67,7 @@ pub(crate) struct MessageInternal { target: Target, } -/// -/// A builder to get a `Message` instance. -/// -/// # Examples -/// -/// ```rust -/// use fcm::{MessageBuilder, NotificationBuilder, Target}; -/// -/// let mut builder = MessageBuilder::new(Target::Token("token".to_string())); -/// builder.notification(NotificationBuilder::new().finalize()); -/// let message = builder.finalize(); -/// ``` +/// A `Message` instance is the main object to send to the FCM API. #[derive(Debug)] pub struct Message { pub data: Option, diff --git a/src/notification/mod.rs b/src/notification/mod.rs index e61441d6e..2105cdfda 100644 --- a/src/notification/mod.rs +++ b/src/notification/mod.rs @@ -23,18 +23,7 @@ pub(crate) struct NotificationInternal { } /// A builder to get a `Notification` instance. -/// -/// # Examples -/// -/// ```rust -/// use fcm::NotificationBuilder; -/// -/// let mut builder = NotificationBuilder::new(); -/// builder.title("Australia vs New Zealand".to_string()); -/// builder.body("3 runs to win in 1 ball".to_string()); -/// let notification = builder.finalize(); -/// ``` -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Notification { pub title: Option, pub body: Option, diff --git a/src/web/webpush_config.rs b/src/web/webpush_config.rs index 1486518d8..d467a1e55 100644 --- a/src/web/webpush_config.rs +++ b/src/web/webpush_config.rs @@ -24,7 +24,7 @@ pub(crate) struct WebpushConfigInternal { fcm_options: Option, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct WebpushConfig { pub headers: Option, pub data: Option, diff --git a/src/web/webpush_fcm_options.rs b/src/web/webpush_fcm_options.rs index b9b239549..b1caaaa95 100644 --- a/src/web/webpush_fcm_options.rs +++ b/src/web/webpush_fcm_options.rs @@ -10,7 +10,7 @@ pub(crate) struct WebpushFcmOptionsInternal { analytics_label: String, } -#[derive(Debug)] +#[derive(Debug, Default)] // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#webpushfcmoptions pub struct WebpushFcmOptions { // The link to open when the user clicks on the notification.