-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove macro dependencies (#45)
- Remove proc_macro_hack - Remove dotenv_codegen_impl
- Loading branch information
Showing
10 changed files
with
77 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
|
||
members = ["dotenv", "dotenv_codegen", "dotenv_codegen_impl"] | ||
members = ["dotenv", "dotenv_codegen"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,69 @@ | ||
#![forbid(unsafe_code)] | ||
|
||
use proc_macro_hack::proc_macro_hack; | ||
use std::env::{self, VarError}; | ||
|
||
#[proc_macro_hack] | ||
pub use dotenvy_codegen_impl::dotenv; | ||
use quote::quote; | ||
use syn::parse::Parser; | ||
use syn::punctuated::Punctuated; | ||
use syn::spanned::Spanned; | ||
use syn::Token; | ||
|
||
#[proc_macro] | ||
pub fn dotenv(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
dotenv_inner(input.into()).into() | ||
} | ||
|
||
fn dotenv_inner(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream { | ||
if let Err(err) = dotenvy::dotenv() { | ||
let msg = format!("Error loading .env file: {}", err); | ||
return quote! { | ||
compile_error!(#msg); | ||
}; | ||
} | ||
|
||
match expand_env(input) { | ||
Ok(stream) => stream, | ||
Err(e) => e.to_compile_error(), | ||
} | ||
} | ||
|
||
fn expand_env(input_raw: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenStream> { | ||
let args = <Punctuated<syn::LitStr, Token![,]>>::parse_terminated | ||
.parse(input_raw.into()) | ||
.expect("expected macro to be called with a comma-separated list of string literals"); | ||
|
||
let mut iter = args.iter(); | ||
|
||
let var_name = iter | ||
.next() | ||
.ok_or_else(|| syn::Error::new(args.span(), "dotenv! takes 1 or 2 arguments"))? | ||
.value(); | ||
let err_msg = iter.next(); | ||
|
||
if iter.next().is_some() { | ||
return Err(syn::Error::new( | ||
args.span(), | ||
"dotenv! takes 1 or 2 arguments", | ||
)); | ||
} | ||
|
||
match env::var(&var_name) { | ||
Ok(val) => Ok(quote!(#val)), | ||
Err(e) => Err(syn::Error::new( | ||
var_name.span(), | ||
err_msg.map_or_else( | ||
|| match e { | ||
VarError::NotPresent => { | ||
format!("environment variable `{}` not defined", var_name) | ||
} | ||
|
||
VarError::NotUnicode(s) => format!( | ||
"environment variable `{}` was not valid unicode: {:?}", | ||
var_name, s | ||
), | ||
}, | ||
|lit| lit.value(), | ||
), | ||
)), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.