Skip to content

Commit 2c846ac

Browse files
mockersfItsDoot
authored andcommitted
add helper for macro to get either bevy::x or bevy_x depending on how it was imported (bevyengine#7164)
# Objective - It can be useful for third party crates to work independently on how bevy is imported ## Solution - Expose an helper to get a subcrate path for macros
1 parent 43d8dcb commit 2c846ac

File tree

2 files changed

+16
-15
lines changed
  • crates

2 files changed

+16
-15
lines changed

crates/bevy_encase_derive/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
use bevy_macro_utils::BevyManifest;
22
use encase_derive_impl::{implement, syn};
33

4-
const BEVY: &str = "bevy";
5-
const BEVY_RENDER: &str = "bevy_render";
64
const ENCASE: &str = "encase";
75

86
fn bevy_encase_path() -> syn::Path {
97
let bevy_manifest = BevyManifest::default();
108
bevy_manifest
11-
.maybe_get_path(BEVY)
12-
.map(|bevy_path| {
13-
let mut segments = bevy_path.segments;
14-
segments.push(BevyManifest::parse_str("render"));
15-
syn::Path {
16-
leading_colon: None,
17-
segments,
18-
}
19-
})
20-
.or_else(|| bevy_manifest.maybe_get_path(BEVY_RENDER))
9+
.get_subcrate("render")
2110
.map(|bevy_render_path| {
2211
let mut segments = bevy_render_path.segments;
2312
segments.push(BevyManifest::parse_str("render_resource"));

crates/bevy_macro_utils/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ impl Default for BevyManifest {
3232
}
3333
}
3434
}
35+
const BEVY: &str = "bevy";
36+
const BEVY_INTERNAL: &str = "bevy_internal";
3537

3638
impl BevyManifest {
3739
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
38-
const BEVY: &str = "bevy";
39-
const BEVY_INTERNAL: &str = "bevy_internal";
40-
4140
fn dep_package(dep: &Value) -> Option<&str> {
4241
if dep.as_str().is_some() {
4342
None
@@ -103,6 +102,19 @@ impl BevyManifest {
103102
pub fn parse_str<T: syn::parse::Parse>(path: &str) -> T {
104103
syn::parse(path.parse::<TokenStream>().unwrap()).unwrap()
105104
}
105+
106+
pub fn get_subcrate(&self, subcrate: &str) -> Option<syn::Path> {
107+
self.maybe_get_path(BEVY)
108+
.map(|bevy_path| {
109+
let mut segments = bevy_path.segments;
110+
segments.push(BevyManifest::parse_str(subcrate));
111+
syn::Path {
112+
leading_colon: None,
113+
segments,
114+
}
115+
})
116+
.or_else(|| self.maybe_get_path(&format!("bevy_{subcrate}")))
117+
}
106118
}
107119

108120
/// Derive a label trait

0 commit comments

Comments
 (0)