Skip to content

Commit

Permalink
use 'IntoFutre' trait
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Mar 19, 2022
1 parent 018d8ef commit ab2823a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
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::days(args.days))
.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 @@ -149,7 +149,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 @@ -212,7 +211,6 @@ where
/// .transactions(account_id)
/// .since(Utc::now() - Duration::days(10))
/// .limit(10)
/// .send()
/// .await?;
/// #
/// # Ok(())
Expand All @@ -239,7 +237,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
14 changes: 13 additions & 1 deletion 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, client::handle_request, endpoints::Endpoint, Result};
Expand Down Expand Up @@ -95,7 +97,7 @@ pub(crate) mod basic {
}

/// Consume and send the [`Request`].
pub async fn send(self) -> Result<()> {
async fn send(self) -> Result<()> {
handle_request(self.client, &self).await
}
}
Expand All @@ -114,6 +116,16 @@ pub(crate) mod basic {
}
}

impl<'a> IntoFuture for Request<'a> {
type Output = Result<()>;

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

fn into_future(self) -> Self::IntoFuture {
self.send()
}
}

#[derive(Debug, Serialize)]
struct Params<'a> {
#[serde(rename = "params[title]")]
Expand Down
14 changes: 13 additions & 1 deletion 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::{self, handle_request},
Expand Down Expand Up @@ -52,7 +54,17 @@ impl<'a> Request<'a> {
}

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

impl<'a> IntoFuture for Request<'a> {
type Output = Result<Transaction>;

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

fn into_future(self) -> Self::IntoFuture {
self.send()
}
}
14 changes: 13 additions & 1 deletion 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 @@ -77,7 +79,7 @@ impl<'a> Request<'a> {
}

/// Consume the request and return the list of [`Transaction`]s
pub async fn send(self) -> Result<Vec<Transaction>> {
async fn send(self) -> Result<Vec<Transaction>> {
#[derive(Deserialize)]
struct Response {
transactions: Vec<Transaction>,
Expand All @@ -89,6 +91,16 @@ impl<'a> Request<'a> {
}
}

impl<'a> IntoFuture for Request<'a> {
type Output = Result<Vec<Transaction>>;

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

fn into_future(self) -> Self::IntoFuture {
self.send()
}
}

#[derive(Serialize, Debug)]
struct Query<'a> {
account_id: &'a str,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
#![feature(into_future)]
#![feature(type_alias_impl_trait)]

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

0 comments on commit ab2823a

Please sign in to comment.