palette_derive/
util.rs
1use proc_macro2::{Span, TokenStream};
2use quote::quote;
3use syn::{parse_quote, Ident, Type};
4
5pub fn path<'a, P: AsRef<[&'a str]>>(path: P, internal: bool) -> TokenStream {
6 let path = path
7 .as_ref()
8 .iter()
9 .map(|&ident| Ident::new(ident, Span::call_site()));
10
11 if internal {
12 quote! {crate::#(#path)::*}
13 } else {
14 let crate_name = find_crate_name();
15 quote! {#crate_name::#(#path)::*}
16 }
17}
18
19pub fn path_type(path: &[&str], internal: bool) -> Type {
20 let path = path
21 .iter()
22 .map(|&ident| Ident::new(ident, Span::call_site()));
23
24 if internal {
25 parse_quote! {crate::#(#path)::*}
26 } else {
27 let crate_name = find_crate_name();
28 parse_quote! {#crate_name::#(#path)::*}
29 }
30}
31
32#[cfg(feature = "find-crate")]
33fn find_crate_name() -> Ident {
34 use find_crate::Error;
35
36 match find_crate::find_crate(|name| name == "palette") {
37 Ok(package) => Ident::new(&package.name, Span::call_site()),
38 Err(Error::NotFound) => Ident::new("palette", Span::call_site()),
39 Err(error) => panic!(
40 "error when trying to find the name of the `palette` crate: {}",
41 error
42 ),
43 }
44}
45
46#[cfg(not(feature = "find-crate"))]
47fn find_crate_name() -> Ident {
48 Ident::new("palette", Span::call_site())
49}