rhine_schema_derive/
path_solver.rs

1use syn::{Attribute, ItemFn};
2
3pub fn get_module_path(item: &ItemFn) -> syn::Result<String> {
4    // 尝试从自定义属性中获取路径
5    let path = find_module_path_attr(&item.attrs).unwrap();
6    Ok(path)
7
8    // // 从环境变量中获取 crate 路径并推断模块路径
9    // let crate_root = std::env::var("CARGO_MANIFEST_DIR")
10    //     .map_err(|_| syn::Error::new(Span::call_site(), "无法获取CARGO_MANIFEST_DIR"))?;
11    //
12    // // 假设你传入了一个文件路径,这里直接传递
13    // let file_path = std::path::Path::new(&crate_root).join("src").join("your_module.rs");
14    //
15    // infer_module_path(&file_path)
16}
17
18fn find_module_path_attr(attrs: &[Attribute]) -> Option<String> {
19    attrs.iter().find_map(|attr| {
20        if attr.path().is_ident("module_path") {
21            attr.parse_args::<syn::LitStr>().ok().map(|lit| lit.value())
22        } else {
23            None
24        }
25    })
26}
27
28// fn infer_module_path(file_path: &std::path::Path) -> syn::Result<String> {
29//     let crate_root = std::env::var("CARGO_MANIFEST_DIR")
30//         .map_err(|_| syn::Error::new(Span::call_site(), "无法获取CARGO_MANIFEST_DIR"))?;
31//
32//     let relative_path = file_path.strip_prefix(crate_root)
33//         .map_err(|_| syn::Error::new(Span::call_site(), "路径推断失败"))?;
34//
35//     let module_path = relative_path.with_extension("")
36//         .to_string_lossy()
37//         .replace(std::path::MAIN_SEPARATOR, "::");
38//
39//     Ok(format!("crate::{}", module_path))
40// }