#![doc = include_str!(env!("README_PATH"))]
#![deny(unsafe_code)]
use core::ops::Deref;
use proc_macro2::Span;
use quote::{quote_spanned, ToTokens};
mod test;
#[derive(Clone)]
pub enum Warning {
Deprecated {
name: String,
index: Option<usize>,
message: String,
links: Vec<String>,
span: Span,
},
}
#[derive(Clone)]
pub enum FormattedWarning {
Deprecated {
name: syn::Ident,
note: String,
span: Option<Span>,
},
}
impl FormattedWarning {
#[must_use]
pub fn new_deprecated<'a, S, T>(name: S, note: T, span: Span) -> Self
where
S: Into<&'a str>,
T: Into<String>,
{
Self::Deprecated {
name: syn::Ident::new(name.into(), span),
note: note.into(),
span: Some(span),
}
}
}
#[derive(Default)]
pub struct DeprecatedWarningBuilder {
title: String,
index: Option<usize>,
old: Option<String>,
new: Option<String>,
links: Vec<String>,
span: Option<Span>,
}
impl DeprecatedWarningBuilder {
#[must_use]
pub fn from_title(title: &str) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { title: title.into(), ..Default::default() }
}
#[must_use]
pub fn index(self, index: usize) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { index: Some(index), ..self }
}
#[must_use]
pub fn old(self, old: &str) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { old: Some(old.into()), ..self }
}
#[must_use]
pub fn new(self, new: &str) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { new: Some(new.into()), ..self }
}
#[must_use]
pub fn help_link(self, link: &str) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { links: vec![link.into()], ..self }
}
#[must_use]
pub fn help_links(self, links: &[&str]) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { links: links.iter().map(|s| s.deref().into()).collect(), ..self }
}
#[must_use]
pub fn span(self, span: Span) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder { span: Some(span), ..self }
}
pub fn maybe_build(self) -> Result<Warning, String> {
let span = self.span.unwrap_or_else(Span::call_site);
let title = self.title;
let old = self.old.ok_or("Missing old")?;
let new = self.new.ok_or("Missing new")?;
let message = format!("It is deprecated to {}.\nPlease instead {}.", old, new);
Ok(Warning::Deprecated { name: title, index: self.index, message, links: self.links, span })
}
#[must_use]
pub fn build(self) -> Warning {
self.maybe_build().expect("maybe_build failed")
}
}
impl Warning {
#[must_use]
pub fn new_deprecated(title: &str) -> DeprecatedWarningBuilder {
DeprecatedWarningBuilder::from_title(title)
}
fn final_deprecated_message(&self) -> String {
let (message, links) = match self {
Warning::Deprecated { message, links, .. } => (message, links),
};
let lines = message.trim().lines().map(|line| line.trim_start());
let message = lines.map(|line| format!("\t\t{}", line)).collect::<Vec<_>>().join("\n");
if !links.is_empty() {
let link =
links.iter().map(|l| format!("<{}>", l)).collect::<Vec<_>>().join("\n\t\t\t");
format!("\n{}\n\n\t\tFor more info see:\n\t\t\t{}", message, link)
} else {
format!("\n{}", message)
}
}
fn final_deprecated_name(&self) -> syn::Ident {
let (index, name, span) = match self {
Warning::Deprecated { index, name, span, .. } => (*index, name, *span),
};
let name = match index {
Some(i) => format!("{}_{}", name, i),
None => name.clone(),
};
syn::Ident::new(&name, span)
}
}
impl From<Warning> for FormattedWarning {
fn from(val: Warning) -> Self {
match val {
Warning::Deprecated { span, .. } => FormattedWarning::Deprecated {
name: val.final_deprecated_name(),
note: val.final_deprecated_message(),
span: Some(span),
},
}
}
}
impl ToTokens for Warning {
fn to_tokens(&self, stream: &mut proc_macro2::TokenStream) {
let formatted: FormattedWarning = self.clone().into();
formatted.to_tokens(stream);
}
}
impl ToTokens for FormattedWarning {
fn to_tokens(&self, stream: &mut proc_macro2::TokenStream) {
let (name, note, span) = match self {
FormattedWarning::Deprecated { name, note, span } => (name, note, span),
};
let span = span.unwrap_or_else(Span::call_site);
let q = quote_spanned!(span =>
#[allow(dead_code)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
fn #name() {
#[deprecated(note = #note)]
#[allow(non_upper_case_globals)]
const _w: () = ();
let _ = _w;
}
);
q.to_tokens(stream);
}
}