Skip to content

Commit

Permalink
use real TOML parsing to avoid issues like the one oliver reported
Browse files Browse the repository at this point in the history
* fixes #5
  • Loading branch information
sam0x17 committed Aug 4, 2023
1 parent 5582030 commit e3c4b4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ walkdir = "2"
common-path = "1"
termcolor = "1"
once_cell = "1"
toml = "0.7"
13 changes: 6 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
fs::{self, OpenOptions},
io::Write,
path::{Path, PathBuf},
str::FromStr,
};
use syn::{
parse2,
Expand All @@ -21,6 +22,7 @@ use syn::{
AttrStyle, Attribute, Error, File, Ident, Item, LitStr, Meta, Result, Token,
};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use toml::{Table, Value};
use walkdir::WalkDir;

fn line_start_position<S: AsRef<str>>(source: S, pos: usize) -> usize {
Expand Down Expand Up @@ -75,7 +77,6 @@ fn caller_crate_root() -> Option<PathBuf> {
let current_dir = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR").expect("failed to read ENV var `CARGO_MANIFEST_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"))
Expand All @@ -91,12 +92,10 @@ fn caller_crate_root() -> Option<PathBuf> {
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())
{
let Ok(table) = Table::from_str(cargo_toml.as_str()) else { continue };
let Some(package) = table.get("package") else { continue };
let Some(Value::String(package_name)) = package.get("name") else { continue };
if package_name.eq_ignore_ascii_case(&crate_name) {
return Some(entry.path().parent().unwrap().to_path_buf());
}
}
Expand Down

0 comments on commit e3c4b4f

Please sign in to comment.