Repository: alacritty/alacritty
File: alacritty_config_derive/src/serde_replace.rs (Rust)

Code before:

/// Create SerdeReplace recursive match arms.
fn match_arms<T>(fields: &Punctuated<Field, T>) -> TokenStream2 {
    let mut stream = TokenStream2::default();
    let mut flattened_arm = None;

    // Create arm for each field.
    for field in fields {
        let ident = field.ident.as_ref().expect("unreachable tuple struct");
        let literal = ident.to_string();

        // Check if #[config(flattened)] attribute is present.
        let flatten = field
            .attrs
            .iter()
            .filter_map(|attr| attr.parse_args::<Attr>().ok())
            .any(|parsed| parsed.ident.as_str() == "flatten");

        if flatten && flattened_arm.is_some() {
            return Error::new(ident.span(), MULTIPLE_FLATTEN_ERROR).to_compile_error();
        } else if flatten {
            flattened_arm = Some(quote! {
                _ => alacritty_config::SerdeReplace::replace(&mut self.#ident, value)?,
            });
        } else {
            // Check if #[config(alias = "some alias")], and, if present, extract its value
            let alias_value = field
                .attrs
                .iter()
                .filter_map(|attr| attr.parse_args::<Attr>().ok())
                .find(|parsed| parsed.ident.as_str() == "alias")
                .and_then(|alias| alias.param.map(|v| v.value()));

            match alias_value {
                Some(alias) => {
                    stream.extend(quote! {
                        #literal | #alias => alacritty_config::SerdeReplace::replace(&mut self.#ident, next_value)?,
                    });
                },
                None => {
                    stream.extend(quote! {
                        #literal => alacritty_config::SerdeReplace::replace(&mut self.#ident, next_value)?,
                    });
                },
            }
        }
    }

    // Add the flattened catch-all as last match arm.
    if let Some(flattened_arm) = flattened_arm.take() {
        stream.extend(flattened_arm);

Review comment (refactor):
```suggestion
            // Extract `#[config(alias = "...")]` attribute values.
```

Code after:
        }
    }
}

/// Create SerdeReplace recursive match arms.
fn match_arms<T>(fields: &Punctuated<Field, T>) -> Result<TokenStream2, syn::Error> {
    let mut stream = TokenStream2::default();
    let mut flattened_arm = None;

    // Create arm for each field.
    for field in fields {
        let ident = field.ident.as_ref().expect("unreachable tuple struct");
        let literal = ident.to_string();

        // Check if #[config(flattened)] attribute is present.
        let flatten = field
            .attrs
            .iter()
            .filter(|attr| (*attr).path().is_ident("config"))
            .filter_map(|attr| attr.parse_args::<Attr>().ok())
            .any(|parsed| parsed.ident.as_str() == "flatten");

        if flatten && flattened_arm.is_some() {
            return Err(Error::new(ident.span(), MULTIPLE_FLATTEN_ERROR));
        } else if flatten {
            flattened_arm = Some(quote! {
                _ => alacritty_config::SerdeReplace::replace(&mut self.#ident, value)?,
            });
        } else {
            // Extract all `#[config(alias = "...")]` attribute values.
            let aliases = field
                .attrs
                .iter()
                .filter(|attr| (*attr).path().is_ident("config"))
                .filter_map(|attr| attr.parse_args::<Attr>().ok())
                .filter(|parsed| parsed.ident.as_str() == "alias")
                .map(|parsed| {
                    let value = parsed
                        .param
                        .ok_or_else(|| format!("Field \"{}\" has no alias value", ident))?
                        .value();

                    if value.trim().is_empty() {
                        return Err(format!("Field \"{}\" has an empty alias value", ident));
                    }

                    Ok(value)
                })
                .collect::<Result<Vec<String>, String>>()
                .map_err(|msg| Error::new(ident.span(), msg))?;