use find_crate::{
find_crate,
Manifest,
};
use heck::ToSnakeCase;
use proc_macro2::{
Span,
TokenStream,
};
use quote::ToTokens;
use syn::{
Attribute,
DeriveInput,
Ident,
Lit,
LitStr,
Meta,
MetaNameValue,
Token,
};
pub fn find_crate_name() -> TokenStream {
find_crate(|name| name == "clia_rustorm")
.map(|package| Ident::new(&package.name, Span::call_site()).into_token_stream())
.unwrap_or_else(|error| {
if !matches!(error, find_crate::Error::NotFound) {
panic!("`rustorm` dependency not found: {}", error);
}
let this_crate = Manifest::new()
.expect("failed to read crate manifest")
.crate_package()
.expect("failed to read the name of this crate");
if this_crate.name == "clia_rustorm" {
Token).into_token_stream()
} else {
panic!("`rustorm` dependency not found");
}
})
}
pub fn find_attribute_value(attributes: &[Attribute], key: &str) -> Option<LitStr> {
attributes
.iter()
.find(|attribute| attribute.path.is_ident(key))
.map(|attribute| {
match attribute.parse_meta() {
Ok(Meta::NameValue(MetaNameValue {
lit: Lit::Str(value),
..
})) => value,
_ => panic!("invalid `{}` attribute", key),
}
})
}
pub fn parse_table_name(input: &DeriveInput) -> LitStr {
find_attribute_value(&input.attrs, "table_name").unwrap_or_else(|| {
LitStr::new(&input.ident.to_string().to_snake_case(), input.ident.span())
})
}