Skip to main content

miniextendr_macros/
util.rs

1//! Small cross-cutting helpers used by multiple macro modules.
2
3/// Extract `#[cfg(...)]` attributes from a list of attributes.
4///
5/// These should be propagated to generated items so they are conditionally
6/// compiled along with the original function.
7pub(crate) fn extract_cfg_attrs(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
8    attrs
9        .iter()
10        .filter(|attr| attr.path().is_ident("cfg"))
11        .cloned()
12        .collect()
13}
14
15/// Format a human-readable source location note from a syntax span.
16///
17/// Column is reported as 1-based for consistency with editor displays.
18pub(crate) fn source_location_doc(span: proc_macro2::Span) -> String {
19    let start = span.start();
20    format!(
21        "Generated from source location line {}, column {}.",
22        start.line,
23        start.column + 1
24    )
25}
26
27/// Build a `TokenStream` containing a raw string literal from an R wrapper string.
28pub(crate) fn r_wrapper_raw_literal(s: &str) -> proc_macro2::TokenStream {
29    use std::str::FromStr;
30    let raw = format!("r#\"\n{}\n\"#", s);
31    proc_macro2::TokenStream::from_str(&raw).expect("valid raw string literal")
32}