bevy/crates/bevy_derive/src/modules.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

use find_crate::Manifest;
2020-05-04 00:54:16 +00:00
use proc_macro::TokenStream;
use syn::{Attribute, Path};
2020-05-04 00:54:16 +00:00
#[derive(Debug)]
2020-05-04 00:54:16 +00:00
pub struct Modules {
pub bevy_render: String,
pub bevy_asset: String,
pub bevy_core: String,
pub bevy_app: String,
}
impl Modules {
pub fn meta(name: &str) -> Modules {
2020-05-04 00:54:16 +00:00
Modules {
bevy_asset: format!("{}::asset", name),
bevy_render: format!("{}::render", name),
bevy_core: format!("{}::core", name),
bevy_app: format!("{}::app", name),
2020-05-04 00:54:16 +00:00
}
}
pub fn external() -> Modules {
Modules {
bevy_asset: "bevy_asset".to_string(),
bevy_render: "bevy_render".to_string(),
bevy_core: "bevy_core".to_string(),
bevy_app: "bevy_app".to_string(),
}
}
}
fn get_meta() -> Option<Modules> {
let manifest = Manifest::new().unwrap();
if let Some(package) = manifest.find(|name| name == "bevy") {
Some(Modules::meta(&package.name))
} else if let Some(package) = manifest.find(|name| name == "bevy_internal") {
Some(Modules::meta(&package.name))
} else {
None
}
2020-05-04 00:54:16 +00:00
}
const AS_CRATE_ATTRIBUTE_NAME: &str = "as_crate";
pub fn get_modules(attributes: &[Attribute]) -> Modules {
let mut modules = get_meta().unwrap_or_else(Modules::external);
for attribute in attributes.iter() {
if *attribute.path.get_ident().as_ref().unwrap() == AS_CRATE_ATTRIBUTE_NAME {
let value = attribute.tokens.to_string();
if value[1..value.len() - 1] == modules.bevy_render {
2020-07-17 00:23:50 +00:00
modules.bevy_render = "crate".to_string();
}
}
}
modules
2020-05-04 00:54:16 +00:00
}
pub fn get_path(path_str: &str) -> Path {
syn::parse(path_str.parse::<TokenStream>().unwrap()).unwrap()
}