Skip to content

Commit

Permalink
Improve ergonomics of the new trait
Browse files Browse the repository at this point in the history
  • Loading branch information
andyblarblar committed Aug 21, 2023
1 parent a49cdc6 commit d3b13f2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sec1_decode = "^0.1.0"
base64 = "^0.13"
chrono = "^0.4"
log = "^0.4"
async-trait = "0.1.73"
async-trait = "^0.1"

[dev-dependencies]
argparse = "^0.2"
Expand Down
18 changes: 8 additions & 10 deletions src/clients/hyper_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_trait::async_trait;
use std::convert::Infallible;

use http::header::{CONTENT_LENGTH, RETRY_AFTER};
use hyper::{body::HttpBody, client::HttpConnector, Body, Client, Request as HttpRequest};
Expand All @@ -22,22 +21,21 @@ pub struct HyperWebPushClient {

impl Default for HyperWebPushClient {
fn default() -> Self {
Self::new().unwrap()
Self::new()
}
}

#[async_trait]
impl WebPushClient for HyperWebPushClient {
type CreationError = Infallible;

impl HyperWebPushClient {
/// Creates a new client.
fn new() -> Result<Self, Self::CreationError> {
//This method can never fail, but returns error to match API of the isahc client.
Ok(Self {
pub fn new() -> Self {
Self {
client: Client::builder().build(HttpsConnector::new()),
})
}
}
}

#[async_trait]
impl WebPushClient for HyperWebPushClient {
/// Sends a notification. Never times out.
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
trace!("Message: {:?}", message);
Expand Down
10 changes: 5 additions & 5 deletions src/clients/isahc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ impl Default for IsahcWebPushClient {
}
}

#[async_trait]
impl WebPushClient for IsahcWebPushClient {
type CreationError = WebPushError;

impl IsahcWebPushClient {
/// Creates a new client. Can fail under resource depletion.
fn new() -> Result<Self, Self::CreationError> {
pub fn new() -> Result<Self, WebPushError> {
Ok(Self {
client: HttpClient::new()?,
})
}
}

#[async_trait]
impl WebPushClient for IsahcWebPushClient {
/// Sends a notification. Never times out.
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
trace!("Message: {:?}", message);
Expand Down
11 changes: 1 addition & 10 deletions src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ pub mod isahc_client;
/// An async client for sending the notification payload.
/// Other features, such as thread safety, may vary by implementation.
#[async_trait]
pub trait WebPushClient
where
Self: Sized,
{
/// Errors that can occur when creating a client.
type CreationError;

/// Creates a new client.
fn new() -> Result<Self, Self::CreationError>;

pub trait WebPushClient {
/// Sends a notification. Never times out.
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError>;
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl From<hyper::Error> for WebPushError {
}
}

#[cfg(not(feature = "hyper-client"))]
#[cfg(feature = "isahc-client")]
impl From<isahc::Error> for WebPushError {
fn from(_: isahc::Error) -> Self {
Self::Unspecified
Expand Down

0 comments on commit d3b13f2

Please sign in to comment.