Skip to content

Commit

Permalink
fix docs tests and add default impl to all
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanjpg committed Jan 22, 2024
1 parent a4dc115 commit 59c8a7a
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 84 deletions.
22 changes: 16 additions & 6 deletions examples/simple_sender.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// cargo run --example simple_sender -- -t <device_token>

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]
Expand All @@ -27,17 +29,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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?;
Expand Down
2 changes: 1 addition & 1 deletion src/android/android_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) struct AndroidConfigInternal {
direct_boot_ok: Option<bool>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct AndroidConfig {
pub collapse_key: Option<String>,
pub priority: Option<AndroidMessagePriority>,
Expand Down
2 changes: 1 addition & 1 deletion src/android/android_fcm_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) struct AndroidFcmOptionsInternal {
analytics_label: String,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct AndroidFcmOptions {
pub analytics_label: String,
}
Expand Down
2 changes: 1 addition & 1 deletion src/android/android_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(crate) struct AndroidNotificationInternal {
image: Option<String>,
}

#[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.
Expand Down
2 changes: 1 addition & 1 deletion src/android/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/android/light_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/apns/apns_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) struct ApnsConfigInternal {
fcm_options: Option<ApnsFcmOptionsInternal>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct ApnsConfig {
pub headers: Option<Value>,
pub payload: Option<Value>,
Expand Down
2 changes: 1 addition & 1 deletion src/apns/apns_fcm_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) struct ApnsFcmOptionsInternal {
image: Option<String>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct ApnsFcmOptions {
// Label associated with the message's analytics data.
pub analytics_label: Option<String>,
Expand Down
71 changes: 27 additions & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error + Send + Sync>> {
//! use fcm::Target;
//! let client = fcm::Client::new();
//!
//! let mut map = HashMap::new();
//! map.insert("message", "Howdy!");
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! 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<dyn std::error::Error + Send + Sync>> {
//! 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;
Expand Down
2 changes: 1 addition & 1 deletion src/message/fcm_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) struct FcmOptionsInternal {
analytics_label: String,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct FcmOptions {
pub analytics_label: String,
}
Expand Down
13 changes: 1 addition & 12 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>,
Expand Down
13 changes: 1 addition & 12 deletions src/notification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub body: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/web/webpush_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct WebpushConfigInternal {
fcm_options: Option<WebpushFcmOptionsInternal>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct WebpushConfig {
pub headers: Option<Value>,
pub data: Option<Value>,
Expand Down
2 changes: 1 addition & 1 deletion src/web/webpush_fcm_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 59c8a7a

Please sign in to comment.