Skip to content

Commit

Permalink
move the ugliness out of the way.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 18, 2024
1 parent 5463f8f commit 62f8d15
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
56 changes: 11 additions & 45 deletions copper_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
extern crate proc_macro;

use copper::config::CopperConfig;
use format::{highlight_rust_code, rustfmt_generated_code};
use proc_macro::TokenStream;
use std::path::PathBuf;

use quote::quote;
use syn::{Field, ItemStruct, LitStr, parse_macro_input, parse_quote};
use syn::Fields::{Named, Unit, Unnamed};
use syn::meta::parser;
use walkdir::WalkDir;

use copper::config::CopperConfig;
use format::{highlight_rust_code, rustfmt_generated_code};
use syn::Fields::{Named, Unit, Unnamed};
use syn::{parse_macro_input, parse_quote, Field, ItemStruct, LitStr};

mod format;
mod utils;

// Parses the CopperRuntime attribute like #[copper_runtime(config = "path")]
#[proc_macro_attribute]
Expand All @@ -30,10 +27,13 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
});

parse_macro_input!(args with attribute_config_parser);
let config_file = config_file.expect("Expected config file attribute like #[CopperRuntime(config = \"path\")]").value();
let mut config_full_path = caller_crate_root();
let config_file = config_file
.expect("Expected config file attribute like #[CopperRuntime(config = \"path\")]")
.value();
let mut config_full_path = utils::caller_crate_root();
config_full_path.push(config_file);
let config_content = std::fs::read_to_string(&config_full_path).unwrap_or_else(|_| panic!("Failed to read configuration file: {:?}", &config_full_path));
let config_content = std::fs::read_to_string(&config_full_path)
.unwrap_or_else(|_| panic!("Failed to read configuration file: {:?}", &config_full_path));
println!("Config content:\n {}", config_content);
let deserialized = CopperConfig::deserialize(&config_content);

Expand Down Expand Up @@ -76,37 +76,3 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {

tokens
}

// Lifted this HORROR but it works.
fn caller_crate_root() -> PathBuf {
let crate_name =
std::env::var("CARGO_PKG_NAME").expect("failed to read ENV var `CARGO_PKG_NAME`!");
let current_dir = std::env::current_dir().expect("failed to unwrap env::current_dir()!");
let search_entry = format!("name=\"{crate_name}\"");
for entry in WalkDir::new(&current_dir)
.into_iter()
.filter_entry(|e| !e.file_name().eq_ignore_ascii_case("target"))
{
let Ok(entry) = entry else { continue; };
if !entry.file_type().is_file() {
continue;
}
let Some(file_name) = entry.path().file_name() else { continue; };
if !file_name.eq_ignore_ascii_case("Cargo.toml") {
continue;
}
let Ok(cargo_toml) = std::fs::read_to_string(entry.path()) else {
continue;
};
if cargo_toml
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.contains(search_entry.as_str())
{
return entry.path().parent().unwrap().to_path_buf();
}
}
current_dir
}

39 changes: 39 additions & 0 deletions copper_derive/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::path::PathBuf;
use walkdir::WalkDir;

// Lifted this HORROR but it works.
pub fn caller_crate_root() -> PathBuf {
let crate_name =
std::env::var("CARGO_PKG_NAME").expect("failed to read ENV var `CARGO_PKG_NAME`!");
let current_dir = std::env::current_dir().expect("failed to unwrap env::current_dir()!");
let search_entry = format!("name=\"{crate_name}\"");
for entry in WalkDir::new(&current_dir)
.into_iter()
.filter_entry(|e| !e.file_name().eq_ignore_ascii_case("target"))
{
let Ok(entry) = entry else {
continue;
};
if !entry.file_type().is_file() {
continue;
}
let Some(file_name) = entry.path().file_name() else {
continue;
};
if !file_name.eq_ignore_ascii_case("Cargo.toml") {
continue;
}
let Ok(cargo_toml) = std::fs::read_to_string(entry.path()) else {
continue;
};
if cargo_toml
.chars()
.filter(|&c| !c.is_whitespace())
.collect::<String>()
.contains(search_entry.as_str())
{
return entry.path().parent().unwrap().to_path_buf();
}
}
current_dir
}

0 comments on commit 62f8d15

Please sign in to comment.