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

Code before:
            .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.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))?;

            stream.extend(quote! {
                    #(#aliases)|* | #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);
    }

    Ok(stream)
}


Review comment (bug):
Bit of an odd question, but have you tested stuff like `" "`? So empty strings.

When converted to tokens the spaces should be ignored, so I'd assume this would bypass this error handling but still fail to compile. I'd imagine you'd have to `value.trim().is_empty()` to handle this properly?

Code after:
            .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))?;

            stream.extend(quote! {
                    #(#aliases)|* | #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);
    }

    Ok(stream)
}