Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

support mashup use of env!("varname") #12

Merged
merged 2 commits into from
Oct 5, 2018
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
52 changes: 49 additions & 3 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern crate proc_macro_hack;

extern crate proc_macro2;
use proc_macro2::{Delimiter, Ident, TokenStream, TokenTree};
use proc_macro2::{Delimiter, Ident, Literal, TokenStream, TokenTree};

use std::collections::BTreeMap as Map;
use std::str::FromStr;
Expand Down Expand Up @@ -36,12 +36,27 @@ struct Concat {

impl Concat {
fn mashup(&self) -> String {
self.pieces.iter().map(ToString::to_string).collect()
self.pieces
.iter()
.map(|tt| match tt {
TokenTree::Literal(lit) => identify(lit),
_ => tt.to_string(),
}).collect()
}
}

/// Returns a `to_string()` impl for `Literals`
/// which is safe for use in idents
fn identify(lit: &Literal) -> String {
let stringified = lit.to_string();
match stringified.chars().next() {
Some(ch) if ch == '"' || ch == '\'' => stringified.trim_matches(ch).into(),
_ => stringified,
}
}

fn parse(tts: TokenStream) -> Input {
let mut tts = tts.into_iter();
let mut tts = tts.into_iter().peekable();
let mut map = Map::new();
let mut attrs = Vec::new();

Expand Down Expand Up @@ -79,6 +94,37 @@ fn parse(tts: TokenStream) -> Input {
if fragment.starts_with("r#") {
ident = Ident::new(&fragment[2..], ident.span());
}
if fragment == "env" {
let resolve_env = match tts.peek() {
Some(TokenTree::Punct(ref p)) => p.as_char() == '!',
_ => false,
};
if resolve_env {
match tts.next().and_then(|_| tts.next()) {
Some(TokenTree::Group(ref grp))
if grp.delimiter() == Delimiter::Parenthesis =>
{
match grp.stream().into_iter().next() {
Some(TokenTree::Literal(ref varname)) => {
let resolved = std::env::var(identify(varname))
.unwrap_or_else(|_| {
panic!(
"unresolvable mashup env var {}",
varname
)
});
pieces.push(TokenTree::Literal(
Literal::string(&resolved),
));
continue;
}
_ => panic!("unexpected mashup input"),
}
}
_ => panic!("unexpected mashup input"),
}
}
}
pieces.push(TokenTree::Ident(ident));
}
TokenTree::Literal(_) => {
Expand Down
39 changes: 39 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,45 @@ fn test_keyword() {
let _ = Fmove;
}

#[test]
fn test_literal_str() {
mashup! {
m["x"] = Foo "Bar";
}

m! {
struct "x";
}

let _ = FooBar;
}

#[test]
fn test_env_literal() {
mashup! {
m["x"] = Lib env bar;
}

m! {
struct "x";
}

let _ = Libenvbar;
}

#[test]
fn test_env_present() {
mashup! {
m["x"] = Lib env!("CARGO_PKG_NAME");
}

m! {
struct "x";
}

let _ = Libmashup;
}

macro_rules! conditionally_ignore {
{
#[cfg(not($cfg:ident))]
Expand Down