Files
continuwuity/src/macros/cargo.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

48 lines
1.1 KiB
Rust
Raw Normal View History

2024-07-24 09:10:01 +00:00
use std::{fs::read_to_string, path::PathBuf};
2024-07-26 20:40:07 +00:00
use proc_macro::{Span, TokenStream};
2024-07-24 09:10:01 +00:00
use quote::quote;
2024-07-26 20:40:07 +00:00
use syn::{Error, ItemConst, Meta};
2024-07-24 09:10:01 +00:00
2024-07-26 20:40:07 +00:00
use crate::{utils, Result};
pub(super) fn manifest(item: ItemConst, args: &[Meta]) -> Result<TokenStream> {
let member = utils::get_named_string(args, "crate");
let path = manifest_path(member.as_deref())?;
let manifest = read_to_string(&path).unwrap_or_default();
2024-07-24 09:10:01 +00:00
let val = manifest.as_str();
2024-07-26 20:40:07 +00:00
let name = item.ident;
2024-07-24 09:10:01 +00:00
let ret = quote! {
const #name: &'static str = #val;
};
2024-07-26 20:40:07 +00:00
Ok(ret.into())
2024-07-24 09:10:01 +00:00
}
#[allow(clippy::option_env_unwrap)]
2024-07-26 20:40:07 +00:00
fn manifest_path(member: Option<&str>) -> Result<PathBuf> {
let Some(path) = option_env!("CARGO_MANIFEST_DIR") else {
return Err(Error::new(
Span::call_site().into(),
"missing CARGO_MANIFEST_DIR in environment",
));
};
let mut path: PathBuf = path.into();
2024-07-24 09:10:01 +00:00
// conduwuit/src/macros/ -> conduwuit/src/
path.pop();
if let Some(member) = member {
// conduwuit/$member/Cargo.toml
path.push(member);
} else {
// conduwuit/src/ -> conduwuit/
path.pop();
}
path.push("Cargo.toml");
2024-07-26 20:40:07 +00:00
Ok(path)
2024-07-24 09:10:01 +00:00
}