lib-conf-derive 0.3.2

Proc-macro providing library authors with first-class configuration for their end-users
Documentation
use crate::parse::attr::DOC_ATTR;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use std::fmt::Display;
use std::ops::AddAssign;
use syn::{Attribute, Generics, Ident, Type, parse_quote};

// !- Token manipulation

pub fn build_type(ident: &Ident, generics: &Generics) -> Type {
    let ty_gen = generics.split_for_impl().1;
    parse_quote!(#ident #ty_gen)
}

// !- Common trait impls

pub fn bare_default_impl_tokens(
    ident: &Ident,
    generics: &Generics,
) -> TokenStream {
    let (
        impl_generics,
        ty_generics,
        where_clause,
    ) = generics.split_for_impl();
    quote! {
        #[automatically_derived]
        impl #impl_generics Default for #ident #ty_generics #where_clause {
            fn default() -> Self {
                Self::new()
            }
        }
    }
}

// !- Doc attr addition helper

/// Handles trim + prefix of leading space
pub fn doc_line(text: impl Display, trim: bool) -> TokenStream {
    let line = text.to_string();
    doc_lines(vec![&line], trim)
}

/// Handles trim + prefix of leading space
pub fn doc_lines(lines: Vec<&str>, trim: bool) -> TokenStream {
    let lines: Vec<_> = lines.into_iter().map(|s| {
        let text = s.to_string();
        let trimmed = if trim {
            text.trim()
        } else {
            &text
        };
        if trimmed.is_empty() {
            String::new()
        } else {
            format!(" {trimmed}")
        }
    }).collect();

    quote! {
        #(#[doc = #lines])*
    }
}
/// Handles line split + trim + prefix of leading space
pub fn doc_lines_split(lines: impl Display, trim: bool) -> TokenStream {
    doc_lines(lines.to_string().lines().collect(), trim)
}

#[cfg_attr(feature = "syn-debug", derive(Debug))]
#[derive(Clone)]
pub(crate) struct AppendDoc {
    source: Vec<Attribute>,
    append: Vec<String>,
}
impl AppendDoc {
    pub fn new(source: Vec<Attribute>) -> Self {
        for attr in &source {
            assert!(attr.path().is_ident(DOC_ATTR));
        }
        Self {
            source,
            append: Vec::new(),
        }
    }

    pub fn source(&self) -> &Vec<Attribute> {
        &self.source
    }

    pub fn line(&mut self, line: impl Display) {
        // TODO: assert has no newlines?
        self.append.push(line.to_string());
    }
    /// iterates over provided lines and adds each as a new element
    pub fn lines(&mut self, text: impl Display) {
        let text = text.to_string();
        for line in text.lines() {
            self.line(line);
        }
    }
    pub fn newl(&mut self) {
        self.append.push(String::new());
    }
    pub fn to_append_tokens(&self) -> TokenStream {
        doc_lines(self.append.iter().map(String::as_str).collect(), true)
    }
}
impl From<Vec<Attribute>> for AppendDoc {
    fn from(value: Vec<Attribute>) -> Self {
        Self::new(value)
    }
}
impl AddAssign<String> for AppendDoc {
    fn add_assign(&mut self, rhs: String) {
        self.lines(rhs);
    }
}
impl AddAssign<Vec<String>> for AppendDoc {
    fn add_assign(&mut self, rhs: Vec<String>) {
        for line in rhs {
            self.line(line);
        }
    }
}
impl ToTokens for AppendDoc {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let source = &self.source;
        let append = self.to_append_tokens();
        tokens.extend(quote! {
            #(#source)*
            #append
        });
    }
}

// !- Pretty expression rendering

// NOTE: these must match the output provided by prettyplease
// TODO: unit tests to prevent breakage from prettyplease changes
static RENDER_PREFIX: &str = "static x: x = ";
static RENDER_SUFFIX: &str = ";";

// this feels so very wrong..
// dtolnay, please forgive me for the flagrant crimes I'm committing against
// your APIs
//
// hopefully some parse()/peek() work on the attrs can replace this without
// falling back to str attr values.
//
/// Pretty renders non-litstr expressions pulled in from atts, in an incredibly
///hacky fashion
pub fn render_expr(tokens: &TokenStream) -> Option<String> {
    let content = format!("{RENDER_PREFIX}{tokens}{RENDER_SUFFIX}");
    if let Ok(parsed) = syn::parse_file(&content) {
        let pretty = prettyplease::unparse(&parsed);

        if pretty.len() > RENDER_PREFIX.len() + RENDER_SUFFIX.len() {
            let inner_i = RENDER_PREFIX.len();
            let inner_f = pretty.len() - RENDER_SUFFIX.len();

            return Some(pretty[inner_i..inner_f - 1].to_string());
        }
    }

    None
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn extract_generics_works() {
//         let empty: Generics = parse_quote!();
//         let extracted = extract_generic_params_for_type(&empty);
//         assert_eq!(extracted.to_string(), quote!().to_string());

//         let basic: Generics = parse_quote!(<A,B,C>);
//         let extracted = extract_generic_params_for_type(&basic);
//         assert_eq!(extracted.to_string(), quote!(<A,B,C>).to_string());

//         let bounded: Generics = parse_quote!(<A: Copy, B: Copy + Clone>);
//         let extracted = extract_generic_params_for_type(&bounded);
//         assert_eq!(extracted.to_string(), quote!(<A, B>).to_string());
//     }
// }