Best practices for using rust enum

enum CustomErrorA {
    Io(std::io::ErrorKind),
    Json(serde_json::Error),
    CallFilter(String)
    CallFunction(String),
    CallTest(String),
}
enum CustomErrorA {
    Io(std::io::ErrorKind),
    Json(serde_json::Error),
    CallEror(CallType, String),
}

enum CallType {
    Function,
    Filter,
    Test
}

Which of the above two ways of defining enum is more recommended? What is the reason?

I'd say it depends on how many variants the CallType enum has. If it's three, I can see why you'd flatten them into CustomErrorA and avoid an extra type. But if it has ten variants? I'd go with separating CallType into its own enum.

1 Like

The fact that the 3 enum variants have names starting with Call... seems to say they're related, which would make me want to group them in CallType for clarity.

2 Likes

I would say if the Strings in CAllFilter, CallFunction and CAllTest are interchangeable (for example, if they are just error messages), then the second version should be used. If the Strings are unrelated, then the first version is better.

1 Like