Jonas Platte a68b854734
api: Refactor macro code and improve error handling
* Inline lots of methods that were only used once
* Create a separate error case for missing headers
2021-04-10 14:50:01 +02:00

42 lines
1.3 KiB
Rust

//! Functions to aid the `Api::to_tokens` method.
use std::collections::BTreeSet;
use proc_macro2::{Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;
use syn::{AttrStyle, Attribute, Ident, Lifetime};
/// Generates a `TokenStream` of lifetime identifiers `<'lifetime>`.
pub(crate) fn unique_lifetimes_to_tokens<'a, I: IntoIterator<Item = &'a Lifetime>>(
lifetimes: I,
) -> TokenStream {
let lifetimes = lifetimes.into_iter().collect::<BTreeSet<_>>();
if lifetimes.is_empty() {
TokenStream::new()
} else {
let lifetimes = quote! { #( #lifetimes ),* };
quote! { < #lifetimes > }
}
}
pub(crate) fn is_valid_endpoint_path(string: &str) -> bool {
string.as_bytes().iter().all(|b| (0x21..=0x7E).contains(b))
}
pub(crate) fn import_ruma_api() -> TokenStream {
if let Ok(FoundCrate::Name(possibly_renamed)) = crate_name("ruma-api") {
let import = Ident::new(&possibly_renamed, Span::call_site());
quote! { ::#import }
} else if let Ok(FoundCrate::Name(possibly_renamed)) = crate_name("ruma") {
let import = Ident::new(&possibly_renamed, Span::call_site());
quote! { ::#import::api }
} else {
quote! { ::ruma_api }
}
}
pub(crate) fn is_cfg_attribute(attr: &Attribute) -> bool {
attr.style == AttrStyle::Outer && attr.path.is_ident("cfg")
}