Skip to content

Commit

Permalink
Preliminary fix of dotenvy attribute to work with async_std
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenfarley committed Sep 4, 2024
1 parent fa19b77 commit 2396aa9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dotenvy-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub fn load(attr: TokenStream, item: TokenStream) -> TokenStream {
// this works with `tokio::main`` but not `async_std::main``
quote! {
// non-async wrapper function
#vis fn #fn_name() #output {
#vis async fn #fn_name() #output {
#load_env
#new_fn_name()
#new_fn_name().await
}

// orig async function, but renamed
Expand Down
Empty file added examples/async-std-dotenvy/.env
Empty file.
8 changes: 8 additions & 0 deletions examples/async-std-dotenvy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "async-std-dotenvy"
version = "0.1.0"
edition = "2021"

[dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
dotenvy = { path = "../../dotenvy" }
45 changes: 45 additions & 0 deletions examples/async-std-dotenvy/out.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
fn main() {
async fn main() {
use dotenvy::{EnvLoader, EnvSequence};
use std::{
error::Error, io::{self, ErrorKind},
process,
};
let seq = if false { EnvSequence::InputOnly } else { EnvSequence::InputThenEnv };
let mut loader = EnvLoader::from_path(".env").sequence(seq);
if let Err(e) = unsafe { loader.load_and_modify() } {
if let Some(io_err) = e
.source()
.and_then(|src| src.downcast_ref::<io::Error>())
{
if io_err.kind() == io::ErrorKind::NotFound && !true {}
}
{
::std::io::_eprint(
format_args!(
"Failed to load env file from path \'{0}\': {1}\n",
".env",
e,
),
);
};
process::exit(1);
}
main_inner()
}
async fn main_inner() {
{
{
{
::std::io::_print(format_args!("Hello, world!\n"));
};
}
}
}
async_std::task::block_on(async { main().await })
}
5 changes: 5 additions & 0 deletions examples/async-std-dotenvy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[async_std::main]
#[dotenvy::load]
async fn main() {
println!("Hello, world!");
}
Empty file added examples/tokio-dotenvy/.env
Empty file.
8 changes: 8 additions & 0 deletions examples/tokio-dotenvy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tokio-dotenvy"
version = "0.1.0"
edition = "2021"

[dependencies]
dotenvy = { path = "../../dotenvy" }
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"]}
20 changes: 20 additions & 0 deletions examples/tokio-dotenvy/out.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
fn main() {
let body = async {
{
::std::io::_print(format_args!("hello world\n"));
}
};
#[allow(clippy::expect_used, clippy::diverging_sub_expression)]
{
return tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(body);
}
}
5 changes: 5 additions & 0 deletions examples/tokio-dotenvy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[tokio::main]
#[dotenvy::load]
async fn main() {
println!("hello world")
}
1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toolchain.channel = "nightly"

0 comments on commit 2396aa9

Please sign in to comment.