Skip to content

Suggest possible variants #362

Description

@nik-rev

I have the following:

#[derive(Debug, Copy, Clone, FromMeta)]
pub enum RenameCase {
    #[darling(rename = "lowercase")]
    Lower,
    #[darling(rename = "UPPERCASE")]
    Upper,
    #[darling(rename = "PascalCase")]
    Pascal,
    #[darling(rename = "camelCase")]
    Camel,
    #[darling(rename = "snake_case")]
    Snake,
    #[darling(rename = "SCREAMING_SNAKE_CASE")]
    ScreamingSnake,
    #[darling(rename = "kebab-case")]
    Kebab,
    #[darling(rename = "SCREAMING-KEBAB-CASE")]
    ScreamingKebab,
    #[darling(rename = "Train-Case")]
    Train,
    #[darling(rename = "Title Case")]
    Title,
}

When the user misspells a word, they should see a list of suggestions. Currently, they will see an unhelpful message:

Error

rename_all = "unknown")]
             ├─Unknown literal value `unknown`

It would be nice if darling automatically showed what the possible variants are.

In my derive macro, I work around this by manually implementing the FromMeta trait and have to duplicate all of the strings.

impl FromMeta for RenameCase {
    fn from_string(value: &str) -> darling::Result<Self> {
        Ok(match value {
            "lowercase" => Self::Lower,
            "UPPERCASE" => Self::Upper,
            "PascalCase" => Self::Pascal,
            "camelCase" => Self::Camel,
            "snake_case" => Self::Snake,
            "SCREAMING_SNAKE_CASE" => Self::ScreamingSnake,
            "kebab-case" => Self::Kebab,
            "SCREAMING-KEBAB-CASE" => Self::ScreamingKebab,
            "Train-Case" => Self::Train,
            "Title Case" => Self::Title,
            _ => {
                return Err(darling::Error::custom(
                    r#"expected one of:
- "lowercase"
- "UPPERCASE"
- "PascalCase"
- "camelCase"
- "snake_case"
- "SCREAMING_SNAKE_CASE"
- "kebab-case"
- "SCREAMING-KEBAB-CASE"
- "Train-Case"
- "Title Case""#,
                ));
            }
        })
    }
}

This gives me the following helpful message:

rename_all = "unknown")]
             ├─expected one of:
             │ - "lowercase"
             │ - "UPPERCASE"
             │ - "PascalCase"
             │ - "camelCase"
             │ - "snake_case"
             │ - "SCREAMING_SNAKE_CASE"
             │ - "kebab-case"
             │ - "SCREAMING-KEBAB-CASE"
             │ - "Train-Case"
             │ - "Title Case"

It could be possible do define them in 1 place with strum but it'd be unfortunate to have to bring in a new dependency for this

I would really appreciate it if darling could provide this, e.g. with an attribute or (even better!) out of the box

Even better: Would be fantastic to provide an "did you mean:" option

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions