lc3_macros/
lib.rs

1//! Helper proc-macros.
2//!
3//! TODO!
4
5// TODO: forbid
6#![warn(
7    bad_style,
8    const_err,
9    dead_code,
10    improper_ctypes,
11    legacy_directory_ownership,
12    non_shorthand_field_patterns,
13    no_mangle_generic_items,
14    overflowing_literals,
15    path_statements,
16    patterns_in_fns_without_body,
17    plugin_as_library,
18    private_in_public,
19    safe_extern_statics,
20    unconditional_recursion,
21    unused,
22    unused_allocation,
23    unused_lifetimes,
24    unused_comparisons,
25    unused_parens,
26    while_true
27)]
28// TODO: deny
29#![warn(
30    missing_debug_implementations,
31    intra_doc_link_resolution_failure,
32    missing_docs,
33    unsafe_code,
34    trivial_casts,
35    trivial_numeric_casts,
36    unused_extern_crates,
37    unused_import_braces,
38    unused_qualifications,
39    unused_results,
40    rust_2018_idioms
41)]
42#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
43#![doc(html_logo_url = "")] // TODO!
44
45extern crate proc_macro;
46use proc_macro2::Span;
47use quote::quote_spanned;
48
49/// Report an error with the given `span` and message.
50pub(crate) fn spanned_err(span: Span, msg: impl Into<String>) -> proc_macro::TokenStream {
51    let msg = msg.into();
52    quote_spanned!(span.into() => {
53        compile_error!(#msg);
54    })
55    .into()
56}
57
58// pub mod lifetime_to_identifier;
59
60// pub use lifetime_to_identifier::lifetime_to_ident;
61
62use proc_macro::TokenTree;
63use quote::quote;
64use syn::{parse::Parse, parse::ParseStream, parse::Result as ParseResult, Ident};
65use syn::{parse_macro_input, Lifetime};
66
67/// Takes a lifetime and turns it into an identifier.
68#[proc_macro]
69pub fn lifetime_to_ident(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
70    let input = parse_macro_input!(tokens as Lifetime);
71
72    proc_macro::TokenStream::from(quote!(#input.ident))
73}
74
75struct LifetimeVarDecl {
76    ident: Ident,
77    rest: proc_macro2::TokenTree,
78}
79
80impl Parse for LifetimeVarDecl {
81    fn parse(input: ParseStream<'_>) -> ParseResult<Self> {
82        let lifetime = input.parse::<Lifetime>()?;
83
84        let tree = input.cursor().token_tree().ok_or_else(|| {
85            syn::Error::new(Span::call_site(), "Expected a lifetime and a token tree.")
86        })?;
87
88        Ok(LifetimeVarDecl {
89            ident: lifetime.ident,
90            rest: tree.0,
91        })
92    }
93}
94
95#[proc_macro]
96pub fn create_label(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
97    let input = parse_macro_input!(tokens as LifetimeVarDecl);
98
99    let ident = input.ident;
100    let tree = input.rest;
101
102    proc_macro::TokenStream::from(quote!(let #ident: Addr = #tree;))
103}
104
105use syn::DeriveInput;
106
107// TODO: this should eventually become the macro we've waiting for, for ordered enums
108// with variants that have no associated data. It should bestow upon such enums:
109//  - an associated const specifying the number of enums
110//  - optionally, to and from impls for the variant's number (i.e. 0 -> R0, R0 -> 0)
111//  - optionally, an array type w/Deref+Index impls
112//  - a display impl (indep of Debug? not sure)
113#[proc_macro_derive(DisplayUsingDebug)]
114pub fn derive_display_from_debug(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
115    let item = parse_macro_input!(item as DeriveInput);
116    let ty_name = item.ident;
117
118    quote! (
119        impl core::fmt::Display for #ty_name {
120            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
121                <Self as core::fmt::Debug>::fmt(self, f)
122            }
123        }
124    ).into()
125}