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

fix: remove macro dependencies #45

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## Changed

- Minimum Supported Rust Version is now 1.56.1
- Removed internal `dotenv_codegen_impl` crate and `proc_macro_hack` dependency

## [0.15.6] - 2022-10-17

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
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"]
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ This library loads environment variables from a _.env_ file. This is convenient

1. [`dotenvy`](https://crates.io/crates/dotenvy) crate - A well-maintained fork of the `dotenv` crate.
2. [`dotenvy_macro`](https://crates.io/crates/dotenvy_codegen) crate - A macro for compile time dotenv inspection. This is a fork of `dotenv_codegen`.
3. [`dotenvy_codgen_impl`](https://crates.io/crates/dotenvy_codegen_impl) crate - Internal implementation for dotenvy_codegen.
4. `dotenvy` CLI tool for running a command using the environment from a _.env_ file (currently Unix only)
3. `dotenvy` CLI tool for running a command using the environment from a _.env_ file (currently Unix only)

## Usage

Expand Down
3 changes: 1 addition & 2 deletions dotenv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ This library loads environment variables from a _.env_ file. This is convenient

1. [`dotenvy`](https://crates.io/crates/dotenvy) crate - A well-maintained fork of the `dotenv` crate.
2. [`dotenvy_macro`](https://crates.io/crates/dotenvy_codegen) crate - A macro for compile time dotenv inspection. This is a fork of `dotenv_codegen`.
3. [`dotenvy_codgen_impl`](https://crates.io/crates/dotenvy_codegen_impl) crate - Internal implementation for dotenvy_codegen.
4. `dotenvy` CLI tool for running a command using the environment from a _.env_ file (currently Unix only)
3. `dotenvy` CLI tool for running a command using the environment from a _.env_ file (currently Unix only)

## Usage

Expand Down
10 changes: 7 additions & 3 deletions dotenv_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[package]
[lib]
proc-macro = true

[package]
name = "dotenvy_macro"
version = "0.15.1"
authors = [
Expand All @@ -20,5 +22,7 @@ edition = "2018"
rust-version = "1.56.1"

[dependencies]
dotenvy_codegen_impl = { version = "0.15", path = "../dotenv_codegen_impl" }
proc-macro-hack = "0.5"
proc-macro2 = "1"
quote = "1"
syn = "1"
dotenvy = { version = "0.15", path = "../dotenv" }
2 changes: 0 additions & 2 deletions dotenv_codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
A macro for compile time dotenv inspection.

This is a well-maintained fork of `dotenv_codegen`.

Warning: there is an outstanding issue with rust-analyzer ([rust-analyzer #9606](https://github.com/rust-analyzer/rust-analyzer/issues/9606)) related to the `dotenv!` macro
69 changes: 66 additions & 3 deletions dotenv_codegen/src/lib.rs
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(),
),
)),
}
}
30 changes: 0 additions & 30 deletions dotenv_codegen_impl/Cargo.toml

This file was deleted.

8 changes: 0 additions & 8 deletions dotenv_codegen_impl/README.md

This file was deleted.

67 changes: 0 additions & 67 deletions dotenv_codegen_impl/src/lib.rs

This file was deleted.