Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use 'IntoFuture' trait to await futures directly without calling 'send' #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/list_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async fn main() -> monzo::Result<()> {
.transactions(account_id)
.since(Utc::now() - Duration::try_days(args.days).unwrap())
.limit(args.limit)
.send()
.await?;

println!("account: {account_id}");
Expand Down
4 changes: 1 addition & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ where
/// client
/// .basic_feed_item(account_id, title, image_url)
/// .body("i figured out how to send messages to monzo from my computer...")
/// .send()
/// .await?;
/// #
/// # Ok(())
Expand Down Expand Up @@ -243,7 +242,6 @@ where
/// .transactions(account_id)
/// .since(Utc::now() - Duration::days(10))
/// .limit(10)
/// .send()
/// .await?;
/// #
/// # Ok(())
Expand All @@ -270,7 +268,7 @@ where
/// #
/// let transaction_id = "TRANSACTION_ID";
///
/// let transactions = client.transaction(transaction_id).send().await?;
/// let transactions = client.transaction(transaction_id).await?;
/// #
/// # Ok(())
/// # }
Expand Down
21 changes: 16 additions & 5 deletions src/endpoints/feed_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pub use basic::Request as Basic;

pub(crate) mod basic {
use std::future::{Future, IntoFuture};

use serde::Serialize;

use crate::{client, endpoints::Endpoint, Result};
Expand Down Expand Up @@ -99,11 +101,6 @@
self.payload.params.body = Some(body);
self
}

/// Consume and send the [`Request`].
pub async fn send(self) -> Result<()> {
self.client.handle_request(&self).await
}
}

impl<C> Endpoint for Request<'_, C>
Expand All @@ -121,6 +118,20 @@
}
}

impl<'a, C> IntoFuture for Request<'a, C>

Check failure on line 121 in src/endpoints/feed_items.rs

View workflow job for this annotation

GitHub Actions / lint

the following explicit lifetimes could be elided: 'a

error: the following explicit lifetimes could be elided: 'a --> src/endpoints/feed_items.rs:121:10 | 121 | impl<'a, C> IntoFuture for Request<'a, C> | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes note: the lint level is defined here --> src/lib.rs:2:5 | 2 | clippy::all, | ^^^^^^^^^^^ = note: `#[deny(clippy::needless_lifetimes)]` implied by `#[deny(clippy::all)]` help: elide the lifetimes | 121 - impl<'a, C> IntoFuture for Request<'a, C> 121 + impl<C> IntoFuture for Request<'_, C> |
where
C: client::Inner,
{
type Output = Result<()>;

type IntoFuture = impl Future<Output = Self::Output>;

/// Consume and send the [`Request`].
fn into_future(self) -> Self::IntoFuture {
async move { self.client.handle_request(&self).await }
}

Check warning on line 132 in src/endpoints/feed_items.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/feed_items.rs#L130-L132

Added lines #L130 - L132 were not covered by tests
}

#[derive(Debug, Serialize)]
struct Params<'a> {
#[serde(rename = "params[title]")]
Expand Down
15 changes: 13 additions & 2 deletions src/endpoints/transactions/get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future::{Future, IntoFuture};

use super::Transaction;
use crate::{client, endpoints::Endpoint, Result};

Expand Down Expand Up @@ -53,9 +55,18 @@
self.expand_merchant = true;
self
}
}

impl<'a, C> IntoFuture for Request<'a, C>

Check failure on line 60 in src/endpoints/transactions/get.rs

View workflow job for this annotation

GitHub Actions / lint

the following explicit lifetimes could be elided: 'a

error: the following explicit lifetimes could be elided: 'a --> src/endpoints/transactions/get.rs:60:6 | 60 | impl<'a, C> IntoFuture for Request<'a, C> | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 60 - impl<'a, C> IntoFuture for Request<'a, C> 60 + impl<C> IntoFuture for Request<'_, C> |
where
C: client::Inner,
{
type Output = Result<Transaction>;

type IntoFuture = impl Future<Output = Self::Output>;

/// Consume the request and return the [`Transaction`]
pub async fn send(self) -> Result<Transaction> {
self.client.handle_request(&self).await
fn into_future(self) -> Self::IntoFuture {
async move { self.client.handle_request(&self).await }

Check warning on line 70 in src/endpoints/transactions/get.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/transactions/get.rs#L69-L70

Added lines #L69 - L70 were not covered by tests
}
}
21 changes: 16 additions & 5 deletions src/endpoints/transactions/list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::future::{Future, IntoFuture};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -78,17 +80,26 @@
self.query.expand_merchant = Some("merchant");
self
}
}

impl<'a, C> IntoFuture for Request<'a, C>

Check failure on line 85 in src/endpoints/transactions/list.rs

View workflow job for this annotation

GitHub Actions / lint

the following explicit lifetimes could be elided: 'a

error: the following explicit lifetimes could be elided: 'a --> src/endpoints/transactions/list.rs:85:6 | 85 | impl<'a, C> IntoFuture for Request<'a, C> | ^^ ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes help: elide the lifetimes | 85 - impl<'a, C> IntoFuture for Request<'a, C> 85 + impl<C> IntoFuture for Request<'_, C> |
where
C: client::Inner,
{
type Output = Result<Vec<Transaction>>;

type IntoFuture = impl Future<Output = Self::Output>;

/// Consume the request and return the list of [`Transaction`]s
pub async fn send(self) -> Result<Vec<Transaction>> {
fn into_future(self) -> Self::IntoFuture {

Check warning on line 94 in src/endpoints/transactions/list.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/transactions/list.rs#L94

Added line #L94 was not covered by tests
#[derive(Deserialize)]
struct Response {
transactions: Vec<Transaction>,
}

let response: Response = self.client.handle_request(&self).await?;

Ok(response.transactions)
async move {
let response: Response = self.client.handle_request(&self).await?;
Ok(response.transactions)
}

Check warning on line 102 in src/endpoints/transactions/list.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/transactions/list.rs#L99-L102

Added lines #L99 - L102 were not covered by tests
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
#![feature(impl_trait_in_assoc_type)]

//! A Monzo client in pure rust.
//!
Expand Down
Loading