]> git.lizzy.rs Git - enumset.git/commitdiff
Use proc-macro-crate to allow easier use when enumset is renamed.
authorAlissa Rao <lymia@lymiahugs.com>
Mon, 4 Apr 2022 07:56:10 +0000 (00:56 -0700)
committerAlissa Rao <lymia@lymiahugs.com>
Mon, 4 Apr 2022 07:56:10 +0000 (00:56 -0700)
enumset/src/lib.rs
enumset_derive/Cargo.toml
enumset_derive/src/lib.rs

index b05fcf76e50d939717f5bd0e470b2737ccaeebf9..0ff95fb4583f744e9d8bf7a957e3ab62d2e5c9a8 100644 (file)
@@ -164,7 +164,8 @@ use crate::repr::EnumSetTypeRepr;
 ///   automatically derived versions, or unintentional behavior may be a result.
 /// * `#[enumset(no_ops)` prevents the derive from implementing any operator traits.
 /// * `#[enumset(crate_name = "enumset2")]` may be used to change the name of the `enumset` crate
-///   used in the generated code, if you have renamed the crate via cargo options.
+///   used in the generated code. By default enumset parses `Cargo.toml` to determine the name of
+///   the crate, or falls back to `enumset` if it cannot be parsed.
 ///
 /// When the `serde` feature is used, the following features may also be specified. These options
 /// may be used (with no effect) when building without the feature enabled:
index aeed2dc730bbec0e569eeb01cd09af23d6fbd541..48f40d9fc6d1e0aec0461eb76979a1edb53ffe97 100644 (file)
@@ -22,3 +22,4 @@ darling = { version = "0.13.0", default-features = false }
 syn = "1"
 quote = "1"
 proc-macro2 = "1"
+proc-macro-crate = "1.1.3"
index 993ccc071af965edfb2bfdbff0b37019dc8b4f96..b779b8e743521982bce3048f722e872847dd97d9 100644 (file)
@@ -6,6 +6,7 @@ use darling::*;
 use proc_macro::TokenStream;
 use proc_macro2::{TokenStream as SynTokenStream, Literal, Span};
 use std::collections::HashSet;
+use proc_macro_crate::FoundCrate;
 use syn::{*, Result, Error};
 use syn::spanned::Spanned;
 use quote::*;
@@ -242,7 +243,16 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream {
     let name = &info.name;
     let enumset = match &info.crate_name {
         Some(crate_name) => quote!(::#crate_name),
-        None => quote!(::enumset),
+        None => {
+            let crate_name = proc_macro_crate::crate_name("enumset");
+            match crate_name {
+                Ok(FoundCrate::Name(name)) => {
+                    let ident = Ident::new(&name, Span::call_site());
+                    quote!(::#ident)
+                }
+                _ => quote!(::enumset),
+            }
+        },
     };
     let typed_enumset = quote!(#enumset::EnumSet<#name>);
     let core = quote!(#enumset::__internal::core_export);