Skip to content

Commit

Permalink
Add standard traits to public structs (#32)
Browse files Browse the repository at this point in the history
* Add common traits to pub structs.
This adds traits such as `Clone`, `Default`, `serde`, and `Hash` to public facing structs where possible and logical. Also optimize imports.

* Bump to v0.9.1
  • Loading branch information
andyblarblar authored Sep 15, 2021
1 parent 4bef960 commit 475fc40
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "web-push"
description = "Web push notification client with support for http-ece encryption and VAPID authentication."
version = "0.9.0"
version = "0.9.1"
authors = ["Julius de Bruijn <[email protected]>", "Andrew Ealovega <[email protected]>"]
license = "Apache-2.0"
homepage = "https://github.com/pimeys/rust-web-push"
Expand Down
3 changes: 2 additions & 1 deletion src/clients/hyper_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::convert::Infallible;

use http::header::{CONTENT_LENGTH, RETRY_AFTER};
use hyper::{body::HttpBody, client::HttpConnector, Body, Client, Request as HttpRequest};
use hyper_tls::HttpsConnector;

use crate::clients::request_builder;
use crate::error::{RetryAfter, WebPushError};
use crate::message::WebPushMessage;
use std::convert::Infallible;

/// An async client for sending the notification payload.
///
Expand Down
2 changes: 1 addition & 1 deletion src/clients/isahc_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use futures_lite::AsyncReadExt;
use http::header::{CONTENT_LENGTH, RETRY_AFTER};
use isahc::HttpClient;

use crate::clients::request_builder;
use crate::error::{RetryAfter, WebPushError};
use crate::message::WebPushMessage;
use futures_lite::AsyncReadExt;

/// An async client for sending the notification payload. This client is expensive to create, and
/// should be reused.
Expand Down
6 changes: 4 additions & 2 deletions src/clients/request_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Functions used to send and consume push http messages.
//! This module can be used to build custom clients.
use crate::{error::WebPushError, message::WebPushMessage};
use http::header::{CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE};
use http::{Request, StatusCode};

use crate::{error::WebPushError, message::WebPushMessage};

#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct ErrorInfo {
code: u16,
Expand Down Expand Up @@ -87,11 +88,12 @@ pub fn parse_response(response_status: StatusCode, body: Vec<u8>) -> Result<(),

#[cfg(test)]
mod tests {
use http::Uri;

use crate::clients::request_builder::*;
use crate::error::WebPushError;
use crate::http_ece::ContentEncoding;
use crate::message::WebPushMessageBuilder;
use http::Uri;

#[test]
fn builds_a_correct_request_with_empty_payload() {
Expand Down
9 changes: 5 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use base64::DecodeError;
use http::uri::InvalidUri;
use serde_json::error::Error as JsonError;
use std::string::FromUtf8Error;
use std::time::{Duration, SystemTime};
use std::{convert::From, error::Error, fmt, io::Error as IoError};

#[derive(PartialEq, Debug)]
use base64::DecodeError;
use http::uri::InvalidUri;
use serde_json::error::Error as JsonError;

#[derive(PartialEq, Debug, Clone, Ord, PartialOrd, Eq, Deserialize, Serialize, Hash)]
pub enum WebPushError {
/// An unknown error happened encrypting the message,
Unspecified,
Expand Down
8 changes: 5 additions & 3 deletions src/http_ece.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ece::encrypt;

use crate::error::WebPushError;
use crate::message::WebPushPayload;
use crate::vapid::VapidSignature;
use ece::encrypt;

/// Content encoding profiles.
pub enum ContentEncoding {
Expand Down Expand Up @@ -78,12 +79,13 @@ impl<'a> HttpEce<'a> {

#[cfg(test)]
mod tests {
use base64::{self, URL_SAFE};
use regex::Regex;

use crate::error::WebPushError;
use crate::http_ece::{ContentEncoding, HttpEce};
use crate::VapidSignature;
use crate::WebPushPayload;
use base64::{self, URL_SAFE};
use regex::Regex;

#[test]
fn test_payload_too_big() {
Expand Down
23 changes: 9 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,24 @@
//! # }
//! ```
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;

mod clients;
mod error;
mod http_ece;
mod message;
mod vapid;
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "hyper-client")]
pub use crate::clients::hyper_client::WebPushClient;

#[cfg(not(feature = "hyper-client"))]
pub use crate::clients::isahc_client::WebPushClient;

pub use crate::clients::request_builder;
pub use crate::error::WebPushError;

pub use crate::message::{SubscriptionInfo, SubscriptionKeys, WebPushMessage, WebPushMessageBuilder, WebPushPayload};

pub use crate::http_ece::ContentEncoding;
pub use crate::message::{SubscriptionInfo, SubscriptionKeys, WebPushMessage, WebPushMessageBuilder, WebPushPayload};
pub use crate::vapid::builder::PartialVapidSignatureBuilder;
pub use crate::vapid::{VapidSignature, VapidSignatureBuilder};

pub use crate::clients::request_builder;
mod clients;
mod error;
mod http_ece;
mod message;
mod vapid;
7 changes: 4 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use http::uri::Uri;

use crate::error::WebPushError;
use crate::http_ece::{ContentEncoding, HttpEce};
use crate::vapid::VapidSignature;
use http::uri::Uri;

/// Encryption keys from the client.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
pub struct SubscriptionKeys {
/// The public key. Base64 encoded.
pub p256dh: String,
Expand All @@ -16,7 +17,7 @@ pub struct SubscriptionKeys {
/// subscription info JSON data (AKA pushSubscription object).
///
/// Client pushSubscription objects can be directly deserialized into this struct using serde.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
pub struct SubscriptionInfo {
/// The endpoint URI for sending the notification.
pub endpoint: String,
Expand Down
18 changes: 11 additions & 7 deletions src/vapid/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::collections::BTreeMap;
use std::io::Read;

use http::uri::Uri;
use jwt_simple::prelude::*;
use serde_json::Value;

use crate::error::WebPushError;
use crate::message::SubscriptionInfo;
use crate::vapid::signer::Claims;
use crate::vapid::{VapidKey, VapidSignature, VapidSigner};
use http::uri::Uri;
use jwt_simple::prelude::*;
use serde_json::Value;
use std::collections::BTreeMap;
use std::io::Read;

/// A VAPID signature builder for generating an optional signature to the
/// request. This encryption is required for payloads in all current and future browsers.
Expand Down Expand Up @@ -241,10 +243,12 @@ impl<'a> PartialVapidSignatureBuilder {

#[cfg(test)]
mod tests {
use std::fs::File;

use ::lazy_static::lazy_static;

use crate::message::SubscriptionInfo;
use crate::vapid::VapidSignatureBuilder;
use ::lazy_static::lazy_static;
use std::fs::File;

lazy_static! {
static ref PRIVATE_PEM: File = File::open("resources/vapid_test_key.pem").unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/vapid/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ impl VapidKey {

#[cfg(test)]
mod tests {
use crate::vapid::key::VapidKey;
use std::fs::File;

use crate::vapid::key::VapidKey;

#[test]
/// Tests that VapidKey derives the correct public key.
fn test_public_key_derivation() {
Expand Down
8 changes: 4 additions & 4 deletions src/vapid/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Contains tooling for signing with VAPID.
pub mod builder;
mod key;
mod signer;

pub use self::builder::VapidSignatureBuilder;
use self::key::VapidKey;
pub use self::signer::Claims;
pub use self::signer::VapidSignature;
use self::signer::VapidSigner;

pub mod builder;
mod key;
mod signer;
8 changes: 5 additions & 3 deletions src/vapid/signer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{error::WebPushError, vapid::VapidKey};
use std::collections::BTreeMap;

use http::uri::Uri;
use jwt_simple::prelude::*;
use serde_json::Value;
use std::collections::BTreeMap;

use crate::{error::WebPushError, vapid::VapidKey};

/// A struct representing a VAPID signature. Should be generated using the
/// [VapidSignatureBuilder](struct.VapidSignatureBuilder.html).
#[derive(Debug)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VapidSignature {
/// The signed JWT, base64 encoded
pub auth_t: String,
Expand Down

0 comments on commit 475fc40

Please sign in to comment.