Skip to content

Commit

Permalink
Remove async-trait from a few crates (#8077)
Browse files Browse the repository at this point in the history
### Description

- Removed `async-trait` dependency from `turborepo-analytics` and `turborepo-api-client`
- Refactored implementation of async functions in `turborepo-api-client` and `turborepo-telemetry`

This crate isn't needed on newer rust toolchains (as long as you are not using trait objects).

### Testing Instructions

If it compiles it's good enough.


Closes TURBO-2973
  • Loading branch information
arlyon committed May 7, 2024
1 parent 9de8fd5 commit 8472b0f
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 61 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/turborepo-analytics/Cargo.toml
Expand Up @@ -10,7 +10,6 @@ license = "MPL-2.0"
workspace = true

[dependencies]
async-trait = { workspace = true }
futures.workspace = true
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full", "time"] }
Expand Down
2 changes: 0 additions & 2 deletions crates/turborepo-analytics/src/lib.rs
Expand Up @@ -184,7 +184,6 @@ mod tests {
time::Duration,
};

use async_trait::async_trait;
use tokio::{
select,
sync::{mpsc, mpsc::UnboundedReceiver},
Expand All @@ -207,7 +206,6 @@ mod tests {
}
}

#[async_trait]
impl AnalyticsClient for DummyClient {
async fn record_analytics(
&self,
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-api-client/Cargo.toml
Expand Up @@ -20,7 +20,6 @@ workspace = true

[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true }
regex = { workspace = true }
Expand Down
9 changes: 4 additions & 5 deletions crates/turborepo-api-client/src/analytics.rs
@@ -1,19 +1,18 @@
use async_trait::async_trait;
use std::future::Future;

use reqwest::Method;
pub use turborepo_vercel_api::{AnalyticsEvent, CacheEvent, CacheSource};

use crate::{retry, APIAuth, APIClient, Error};

#[async_trait]
pub trait AnalyticsClient {
async fn record_analytics(
fn record_analytics(
&self,
api_auth: &APIAuth,
events: Vec<AnalyticsEvent>,
) -> Result<(), Error>;
) -> impl Future<Output = Result<(), Error>> + Send;
}

#[async_trait]
impl AnalyticsClient for APIClient {
#[tracing::instrument(skip_all)]
async fn record_analytics(
Expand Down
68 changes: 38 additions & 30 deletions crates/turborepo-api-client/src/lib.rs
Expand Up @@ -2,9 +2,8 @@
#![feature(error_generic_member_access)]
#![deny(clippy::all)]

use std::{backtrace::Backtrace, env};
use std::{backtrace::Backtrace, env, future::Future};

use async_trait::async_trait;
use lazy_static::lazy_static;
use regex::Regex;
pub use reqwest::Response;
Expand All @@ -31,37 +30,47 @@ lazy_static! {
Regex::new(r"(?i)(?:^|,) *authorization *(?:,|$)").unwrap();
}

#[async_trait]
pub trait Client {
async fn get_user(&self, token: &str) -> Result<UserResponse>;
async fn get_teams(&self, token: &str) -> Result<TeamsResponse>;
async fn get_team(&self, token: &str, team_id: &str) -> Result<Option<Team>>;
fn get_user(&self, token: &str) -> impl Future<Output = Result<UserResponse>> + Send;
fn get_teams(&self, token: &str) -> impl Future<Output = Result<TeamsResponse>> + Send;
fn get_team(
&self,
token: &str,
team_id: &str,
) -> impl Future<Output = Result<Option<Team>>> + Send;
fn add_ci_header(request_builder: RequestBuilder) -> RequestBuilder;
async fn get_spaces(&self, token: &str, team_id: Option<&str>) -> Result<SpacesResponse>;
async fn verify_sso_token(&self, token: &str, token_name: &str) -> Result<VerifiedSsoUser>;
async fn handle_403(response: Response) -> Error;
fn get_spaces(
&self,
token: &str,
team_id: Option<&str>,
) -> impl Future<Output = Result<SpacesResponse>> + Send;
fn verify_sso_token(
&self,
token: &str,
token_name: &str,
) -> impl Future<Output = Result<VerifiedSsoUser>> + Send;
fn handle_403(response: Response) -> impl Future<Output = Error> + Send;
fn make_url(&self, endpoint: &str) -> Result<Url>;
}

#[async_trait]
pub trait CacheClient {
async fn get_artifact(
fn get_artifact(
&self,
hash: &str,
token: &str,
team_id: Option<&str>,
team_slug: Option<&str>,
method: Method,
) -> Result<Option<Response>>;
async fn fetch_artifact(
) -> impl Future<Output = Result<Option<Response>>> + Send;
fn fetch_artifact(
&self,
hash: &str,
token: &str,
team_id: Option<&str>,
team_slug: Option<&str>,
) -> Result<Option<Response>>;
) -> impl Future<Output = Result<Option<Response>>> + Send;
#[allow(clippy::too_many_arguments)]
async fn put_artifact(
fn put_artifact(
&self,
hash: &str,
artifact_body: &[u8],
Expand All @@ -70,26 +79,28 @@ pub trait CacheClient {
token: &str,
team_id: Option<&str>,
team_slug: Option<&str>,
) -> Result<()>;
async fn artifact_exists(
) -> impl Future<Output = Result<()>> + Send;
fn artifact_exists(
&self,
hash: &str,
token: &str,
team_id: Option<&str>,
team_slug: Option<&str>,
) -> Result<Option<Response>>;
async fn get_caching_status(
) -> impl Future<Output = Result<Option<Response>>> + Send;
fn get_caching_status(
&self,
token: &str,
team_id: Option<&str>,
team_slug: Option<&str>,
) -> Result<CachingStatusResponse>;
) -> impl Future<Output = Result<CachingStatusResponse>> + Send;
}

#[async_trait]
pub trait TokenClient {
async fn get_metadata(&self, token: &str) -> Result<ResponseTokenMetadata>;
async fn delete_token(&self, token: &str) -> Result<()>;
fn get_metadata(
&self,
token: &str,
) -> impl Future<Output = Result<ResponseTokenMetadata>> + Send;
fn delete_token(&self, token: &str) -> impl Future<Output = Result<()>> + Send;
}

#[derive(Clone)]
Expand All @@ -113,7 +124,6 @@ pub fn is_linked(api_auth: &Option<APIAuth>) -> bool {
.map_or(false, |api_auth| api_auth.is_linked())
}

#[async_trait]
impl Client for APIClient {
async fn get_user(&self, token: &str) -> Result<UserResponse> {
let url = self.make_url("/v2/user")?;
Expand Down Expand Up @@ -262,7 +272,6 @@ impl Client for APIClient {
}
}

#[async_trait]
impl CacheClient for APIClient {
async fn get_artifact(
&self,
Expand Down Expand Up @@ -414,7 +423,6 @@ impl CacheClient for APIClient {
}
}

#[async_trait]
impl TokenClient for APIClient {
async fn get_metadata(&self, token: &str) -> Result<ResponseTokenMetadata> {
let endpoint = "/v5/user/tokens/current";
Expand Down Expand Up @@ -464,9 +472,9 @@ impl TokenClient for APIClient {
message: body.error.message,
});
}
return Err(Error::ForbiddenToken {
Err(Error::ForbiddenToken {
url: self.make_url(endpoint)?.to_string(),
});
})
}
_ => Err(response.error_for_status().unwrap_err().into()),
}
Expand Down Expand Up @@ -516,9 +524,9 @@ impl TokenClient for APIClient {
message: body.error.message,
});
}
return Err(Error::ForbiddenToken {
Err(Error::ForbiddenToken {
url: self.make_url(endpoint)?.to_string(),
});
})
}
_ => Err(response.error_for_status().unwrap_err().into()),
}
Expand Down
9 changes: 4 additions & 5 deletions crates/turborepo-api-client/src/telemetry.rs
@@ -1,22 +1,21 @@
use async_trait::async_trait;
use std::future::Future;

use reqwest::Method;
use turborepo_vercel_api::telemetry::TelemetryEvent;

use crate::{retry, AnonAPIClient, Error};

const TELEMETRY_ENDPOINT: &str = "/api/turborepo/v1/events";

#[async_trait]
pub trait TelemetryClient {
async fn record_telemetry(
fn record_telemetry(
&self,
events: Vec<TelemetryEvent>,
telemetry_id: &str,
session_id: &str,
) -> Result<(), Error>;
) -> impl Future<Output = Result<(), Error>> + Send;
}

#[async_trait]
impl TelemetryClient for AnonAPIClient {
async fn record_telemetry(
&self,
Expand Down
3 changes: 0 additions & 3 deletions crates/turborepo-auth/src/auth/login.rs
Expand Up @@ -200,7 +200,6 @@ mod tests {
}
}

#[async_trait]
impl Client for MockApiClient {
async fn get_user(&self, token: &str) -> turborepo_api_client::Result<UserResponse> {
if token.is_empty() {
Expand Down Expand Up @@ -269,7 +268,6 @@ mod tests {
}
}

#[async_trait]
impl TokenClient for MockApiClient {
async fn get_metadata(
&self,
Expand Down Expand Up @@ -301,7 +299,6 @@ mod tests {
}
}

#[async_trait]
impl CacheClient for MockApiClient {
async fn get_artifact(
&self,
Expand Down
3 changes: 0 additions & 3 deletions crates/turborepo-auth/src/auth/logout.rs
Expand Up @@ -82,7 +82,6 @@ impl<T: TokenClient> LogoutOptions<T> {
mod tests {
use std::backtrace::Backtrace;

use async_trait::async_trait;
use reqwest::{RequestBuilder, Response};
use tempfile::tempdir;
use turbopath::AbsoluteSystemPathBuf;
Expand All @@ -100,7 +99,6 @@ mod tests {
pub succeed_delete_request: bool,
}

#[async_trait]
impl Client for MockApiClient {
async fn get_user(&self, _token: &str) -> turborepo_api_client::Result<UserResponse> {
unimplemented!("get_user")
Expand Down Expand Up @@ -143,7 +141,6 @@ mod tests {
}
}

#[async_trait]
impl TokenClient for MockApiClient {
async fn delete_token(&self, _token: &str) -> turborepo_api_client::Result<()> {
if self.succeed_delete_request {
Expand Down
3 changes: 0 additions & 3 deletions crates/turborepo-auth/src/auth/sso.rs
Expand Up @@ -191,7 +191,6 @@ mod tests {
}
}

#[async_trait]
impl Client for MockApiClient {
async fn get_user(&self, token: &str) -> turborepo_api_client::Result<UserResponse> {
if token.is_empty() {
Expand Down Expand Up @@ -267,7 +266,6 @@ mod tests {
}
}

#[async_trait]
impl TokenClient for MockApiClient {
async fn get_metadata(
&self,
Expand Down Expand Up @@ -298,7 +296,6 @@ mod tests {
}
}

#[async_trait]
impl CacheClient for MockApiClient {
async fn get_artifact(
&self,
Expand Down
2 changes: 0 additions & 2 deletions crates/turborepo-auth/src/lib.rs
Expand Up @@ -266,7 +266,6 @@ fn is_token_active(metadata: &ResponseTokenMetadata, current_time: u128) -> bool
mod tests {
use std::backtrace::Backtrace;

use async_trait::async_trait;
use reqwest::{Method, Response};
use tempfile::tempdir;
use turbopath::AbsoluteSystemPathBuf;
Expand Down Expand Up @@ -396,7 +395,6 @@ mod tests {
pub response: MockCachingResponse,
}

#[async_trait]
impl CacheClient for MockCacheClient {
async fn get_artifact(
&self,
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-telemetry/Cargo.toml
Expand Up @@ -16,7 +16,6 @@ test-case = { workspace = true }
turborepo-vercel-api-mock = { workspace = true }

[dependencies]
async-trait = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
config = { version = "0.13.4", default-features = false, features = ["json"] }
futures = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions crates/turborepo-telemetry/src/lib.rs
Expand Up @@ -256,7 +256,6 @@ mod tests {
time::Duration,
};

use async_trait::async_trait;
use tokio::{
select,
sync::{mpsc, mpsc::UnboundedReceiver},
Expand All @@ -281,7 +280,6 @@ mod tests {
}
}

#[async_trait]
impl TelemetryClient for DummyClient {
async fn record_telemetry(
&self,
Expand Down

0 comments on commit 8472b0f

Please sign in to comment.