Use IntoValue in config code (#13751)

# Description

Cleans up and refactors the config code using the `IntoValue` macro.
Shoutout to @cptpiepmatz for making the macro!

# User-Facing Changes

Should be none.

# After Submitting

Somehow refactor the reverse transformation.
This commit is contained in:
Ian Manske
2024-09-05 00:44:23 -07:00
committed by GitHub
parent 4792328d0e
commit abd230e12e
37 changed files with 992 additions and 1206 deletions

View File

@ -126,7 +126,7 @@ fn derive_struct_from_value(
/// .remove("favorite_toy")
/// .map(|v| <#ty as nu_protocol::FromValue>::from_value(v))
/// .transpose()?
/// .flatten(),
/// .flatten(),
/// })
/// }
/// }
@ -491,7 +491,7 @@ fn enum_from_value(data: &DataEnum, attrs: &[Attribute]) -> Result {
}
/// Implements `FromValue::expected_type` for enums.
///
///
/// Since it's difficult to name the type of an enum in the current type system, we want to use the
/// default implementation if `#[nu_value(type_name = "...")]` was *not* given.
/// For that, a `None` value is returned, for a passed type name we return something like this:

View File

@ -1,5 +1,6 @@
use proc_macro2::Span;
use std::collections::HashMap;
use syn::ext::IdentExt;
use syn::Ident;
use crate::attributes::{ContainerAttributes, MemberAttributes};
@ -33,18 +34,17 @@ impl NameResolver {
/// If a duplicate identifier is detected, it returns [`DeriveError::NonUniqueName`].
pub fn resolve_ident<M>(
&mut self,
ident: &'_ Ident,
container_attrs: &'_ ContainerAttributes,
member_attrs: &'_ MemberAttributes,
ident: &Ident,
container_attrs: &ContainerAttributes,
member_attrs: &MemberAttributes,
default: impl Into<Option<Case>>,
) -> Result<String, DeriveError<M>> {
let span = ident.span();
let rename_all = container_attrs.rename_all;
let rename = member_attrs.rename.as_ref();
let ident = match (rename, rename_all) {
(Some(rename), _) => rename.to_string(),
(None, Some(case)) => ident.to_case(case),
(None, None) => ident.to_case(default),
let ident = if let Some(rename) = &member_attrs.rename {
rename.clone()
} else {
let case = container_attrs.rename_all.or(default.into());
ident.unraw().to_case(case)
};
if let Some(seen) = self.seen_names.get(&ident) {