Skip to content

Commit

Permalink
add helper for macro to get either bevy::x or bevy_x depending on how…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
mockersf authored and ItsDoot committed Feb 1, 2023
1 parent 43d8dcb commit 2c846ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
13 changes: 1 addition & 12 deletions crates/bevy_encase_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
use bevy_macro_utils::BevyManifest;
use encase_derive_impl::{implement, syn};

const BEVY: &str = "bevy";
const BEVY_RENDER: &str = "bevy_render";
const ENCASE: &str = "encase";

fn bevy_encase_path() -> syn::Path {
let bevy_manifest = BevyManifest::default();
bevy_manifest
.maybe_get_path(BEVY)
.map(|bevy_path| {
let mut segments = bevy_path.segments;
segments.push(BevyManifest::parse_str("render"));
syn::Path {
leading_colon: None,
segments,
}
})
.or_else(|| bevy_manifest.maybe_get_path(BEVY_RENDER))
.get_subcrate("render")
.map(|bevy_render_path| {
let mut segments = bevy_render_path.segments;
segments.push(BevyManifest::parse_str("render_resource"));
Expand Down
18 changes: 15 additions & 3 deletions crates/bevy_macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ impl Default for BevyManifest {
}
}
}
const BEVY: &str = "bevy";
const BEVY_INTERNAL: &str = "bevy_internal";

impl BevyManifest {
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
const BEVY: &str = "bevy";
const BEVY_INTERNAL: &str = "bevy_internal";

fn dep_package(dep: &Value) -> Option<&str> {
if dep.as_str().is_some() {
None
Expand Down Expand Up @@ -103,6 +102,19 @@ impl BevyManifest {
pub fn parse_str<T: syn::parse::Parse>(path: &str) -> T {
syn::parse(path.parse::<TokenStream>().unwrap()).unwrap()
}

pub fn get_subcrate(&self, subcrate: &str) -> Option<syn::Path> {
self.maybe_get_path(BEVY)
.map(|bevy_path| {
let mut segments = bevy_path.segments;
segments.push(BevyManifest::parse_str(subcrate));
syn::Path {
leading_colon: None,
segments,
}
})
.or_else(|| self.maybe_get_path(&format!("bevy_{subcrate}")))
}
}

/// Derive a label trait
Expand Down

0 comments on commit 2c846ac

Please sign in to comment.