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

Support wasm32-unknown-unknown architecture #458

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions tests/test_with_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ pub mod test_pagination {
}

/// This test iterates a request of 10 items, with 5 requests of 2 items.
#[cfg_attr(all(feature = "__async", not(target_arch = "wasm32")), tokio::test)]
#[cfg_attr(all(feature = "__async", target_arch = "wasm32"), wasm_bindgen_test)]
#[cfg(feature = "__async")]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn test_pagination_async() {
use futures_util::StreamExt;

Expand Down
18 changes: 17 additions & 1 deletion tests/test_with_oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,26 @@ use maybe_async::maybe_async;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg(not(target_arch = "wasm32"))]
fn get_access_tokens() -> (Option<String>, Option<String>) {
use std::env;
(
env::var("RSPOTIFY_ACCESS_TOKEN").ok(),
env::var("RSPOTIFY_REFRESH_TOKEN").ok(),
)
}

#[cfg(target_arch = "wasm32")]
fn get_access_tokens() -> (Option<String>, Option<String>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with wasm32 architecture, does it mean wasm32 doesn't support env::var function, so we need another macro to obtain the environment variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, wasm32-unknown-unknown doesn't support accessing OS environment variables. As a work-around the tokens are imported at compile time. I extracted the code into a function to make it easier to manage the difference between architectures.

let access_token = option_env!("RSPOTIFY_ACCESS_TOKEN").map(|s| s.to_string());
let refresh_token = option_env!("RSPOTIFY_REFRESH_TOKEN").map(|s| s.to_string());
(access_token, refresh_token)
}

/// Generating a new OAuth client for the requests.
#[maybe_async]
pub async fn oauth_client() -> AuthCodeSpotify {
let (access_token, refresh_token) = util::get_access_tokens();
let (access_token, refresh_token) = get_access_tokens();

if let Some(access_token) = access_token {
let tok = Token {
Expand Down
16 changes: 0 additions & 16 deletions tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,3 @@ pub fn get_credentials() -> Credentials {
let secret = dotenvy_macro::dotenv!("RSPOTIFY_CLIENT_SECRET");
Credentials::new(&id, &secret)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn get_access_tokens() -> (Option<String>, Option<String>) {
use std::env;
(
env::var("RSPOTIFY_ACCESS_TOKEN").ok(),
env::var("RSPOTIFY_REFRESH_TOKEN").ok(),
)
}

#[cfg(target_arch = "wasm32")]
pub fn get_access_tokens() -> (Option<String>, Option<String>) {
let access_token = option_env!("RSPOTIFY_ACCESS_TOKEN").map(|s| s.to_string());
let refresh_token = option_env!("RSPOTIFY_REFRESH_TOKEN").map(|s| s.to_string());
(access_token, refresh_token)
}
Loading