mirror of
https://github.com/nushell/nushell.git
synced 2025-02-24 06:22:17 +01:00
# Description This PR allows the helper attribute `nu_value(rename = "...")` to be used on struct fields and enum variants. This allows renaming keys and variants just like [`#[serde(rename = "name")]`](https://serde.rs/field-attrs.html#rename). This has no singular variants for `IntoValue` or `FromValue`, both need to use the same (but I think this shouldn't be an issue for now). # User-Facing Changes Users of the derive macros `IntoValue` and `FromValue` may now use `#[nu_value(rename = "...")]` to rename single fields, but no already existing code will break.
72 lines
2.9 KiB
Rust
72 lines
2.9 KiB
Rust
//! Macro implementations of `#[derive(FromValue, IntoValue)]`.
|
|
//!
|
|
//! As this crate is a [`proc_macro`] crate, it is only allowed to export
|
|
//! [procedural macros](https://doc.rust-lang.org/reference/procedural-macros.html).
|
|
//! Therefore, it only exports [`IntoValue`] and [`FromValue`].
|
|
//!
|
|
//! To get documentation for other functions and types used in this crate, run
|
|
//! `cargo doc -p nu-derive-value --document-private-items`.
|
|
//!
|
|
//! This crate uses a lot of
|
|
//! [`proc_macro2::TokenStream`](https://docs.rs/proc-macro2/1.0.24/proc_macro2/struct.TokenStream.html)
|
|
//! as `TokenStream2` to allow testing the behavior of the macros directly, including the output
|
|
//! token stream or if the macro errors as expected.
|
|
//! The tests for functionality can be found in `nu_protocol::value::test_derive`.
|
|
//!
|
|
//! This documentation is often less reference-heavy than typical Rust documentation.
|
|
//! This is because this crate is a dependency for `nu_protocol`, and linking to it would create a
|
|
//! cyclic dependency.
|
|
//! Also all examples in the documentation aren't tested as this crate cannot be compiled as a
|
|
//! normal library very easily.
|
|
//! This might change in the future if cargo allows building a proc-macro crate differently for
|
|
//! `cfg(doctest)` as they are already doing for `cfg(test)`.
|
|
//!
|
|
//! The generated code from the derive macros tries to be as
|
|
//! [hygienic](https://doc.rust-lang.org/reference/macros-by-example.html#hygiene) as possible.
|
|
//! This ensures that the macro can be called anywhere without requiring specific imports.
|
|
//! This results in obtuse code, which isn't recommended for manual, handwritten Rust
|
|
//! but ensures that no other code may influence this generated code or vice versa.
|
|
|
|
use proc_macro::TokenStream;
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
use proc_macro_error::{proc_macro_error, Diagnostic};
|
|
|
|
mod attributes;
|
|
mod case;
|
|
mod error;
|
|
mod from;
|
|
mod into;
|
|
mod names;
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
const HELPER_ATTRIBUTE: &str = "nu_value";
|
|
|
|
/// Derive macro generating an impl of the trait `IntoValue`.
|
|
///
|
|
/// For further information, see the docs on the trait itself.
|
|
#[proc_macro_derive(IntoValue, attributes(nu_value))]
|
|
#[proc_macro_error]
|
|
pub fn derive_into_value(input: TokenStream) -> TokenStream {
|
|
let input = TokenStream2::from(input);
|
|
let output = match into::derive_into_value(input) {
|
|
Ok(output) => output,
|
|
Err(e) => Diagnostic::from(e).abort(),
|
|
};
|
|
TokenStream::from(output)
|
|
}
|
|
|
|
/// Derive macro generating an impl of the trait `FromValue`.
|
|
///
|
|
/// For further information, see the docs on the trait itself.
|
|
#[proc_macro_derive(FromValue, attributes(nu_value))]
|
|
#[proc_macro_error]
|
|
pub fn derive_from_value(input: TokenStream) -> TokenStream {
|
|
let input = TokenStream2::from(input);
|
|
let output = match from::derive_from_value(input) {
|
|
Ok(output) => output,
|
|
Err(e) => Diagnostic::from(e).abort(),
|
|
};
|
|
TokenStream::from(output)
|
|
}
|