-
Notifications
You must be signed in to change notification settings - Fork 13
Allow user input environmental variables as base path #21
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,4 @@ proc-macro-hack = "0.5.9" | |
|
|
||
| [features] | ||
| stable = ["include-flate-codegen/stable"] | ||
| std = [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,21 @@ | ||
| #![cfg_attr(not(any(test, feature = "std")), no_std)] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| #[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)] | ||
| pub use include_flate_codegen::deflate_file; | ||
|
|
||
| #[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)] | ||
| pub use include_flate_codegen::deflate_utf8_file; | ||
|
|
||
| #[cfg(feature = "std")] | ||
| pub type String = ::std::string::String; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| pub type String = ::alloc::string::String; | ||
|
|
||
| #[cfg(feature = "std")] | ||
| pub type Vec<T> = ::std::vec::Vec<T>; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| pub type Vec<T> = ::alloc::vec::Vec<T>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,8 @@ use std::str::from_utf8; | |
| use libflate::deflate::Encoder; | ||
| use proc_macro::TokenStream; | ||
| use proc_macro2::Span; | ||
| use quote::quote; | ||
| use syn::{Error, LitByteStr, LitStr, Result}; | ||
| use quote::{quote, ToTokens}; | ||
| use syn::{Error, ExprTuple, LitByteStr, LitStr, Result}; | ||
|
|
||
| /// `deflate_file!("file")` is equivalent to `include_bytes!("file.gz")`. | ||
| /// | ||
|
|
@@ -67,20 +67,42 @@ pub fn deflate_utf8_file(ts: TokenStream) -> TokenStream { | |
| } | ||
|
|
||
| fn inner(ts: TokenStream, utf8: bool) -> Result<impl Into<TokenStream>> { | ||
| if let Ok(t) = syn::parse::<ExprTuple>(ts.clone()) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why tuple? The invocation will then become |
||
| if t.elems.len() != 2 { | ||
| panic!("expected a tuple of size 2 only"); | ||
| } | ||
| let (lit, base) = ( | ||
| t.elems.first().unwrap().into_token_stream(), | ||
| t.elems.last().unwrap().into_token_stream(), | ||
| ); | ||
| let (lit, base) = ( | ||
| syn::parse::<LitStr>(lit.into())?, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why parse again instead of just checking if |
||
| syn::parse::<LitStr>(base.into())?, | ||
| ); | ||
| let key = quote!(#base).to_string(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just get |
||
| compress_file_as_tokenstream(PathBuf::from(lit.value()), &key[1..key.len() - 1], utf8) | ||
| } else if let Ok(lit) = syn::parse::<LitStr>(ts) { | ||
| compress_file_as_tokenstream(PathBuf::from(lit.value()), "CARGO_MANIFEST_DIR", utf8) | ||
| } else { | ||
| panic!("invalid pattern") | ||
| } | ||
| } | ||
|
|
||
| fn compress_file_as_tokenstream( | ||
| path: PathBuf, | ||
| key: &str, | ||
| utf8: bool, | ||
| ) -> Result<impl Into<TokenStream>> { | ||
| fn emap<E: std::fmt::Display>(error: E) -> Error { | ||
| Error::new(Span::call_site(), error) | ||
| } | ||
|
|
||
| let dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
|
|
||
| let lit = syn::parse::<LitStr>(ts)?; | ||
| let path = PathBuf::from(lit.value()); | ||
|
|
||
| if path.is_absolute() { | ||
| Err(emap("absolute paths are not supported"))?; | ||
| } | ||
|
|
||
| let target = dir.join(path); | ||
| let dir = PathBuf::from(std::env::var(key).unwrap()); | ||
| let target: PathBuf = dir.join(path); | ||
|
|
||
| let mut file = File::open(target).map_err(emap)?; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about actually using a custom
Parseimplementation