continuwuity/src/macros/cargo.rs
Jason Volk ca82b59c6f upgrade to syn 2.x
Signed-off-by: Jason Volk <jason@zemos.net>
2024-07-27 08:26:42 +00:00

47 lines
1.1 KiB
Rust

use std::{fs::read_to_string, path::PathBuf};
use proc_macro::{Span, TokenStream};
use quote::quote;
use syn::{Error, ItemConst, Meta};
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();
let val = manifest.as_str();
let name = item.ident;
let ret = quote! {
const #name: &'static str = #val;
};
Ok(ret.into())
}
#[allow(clippy::option_env_unwrap)]
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();
// 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");
Ok(path)
}