Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for simple enums of a single type #20

Closed
jayvdb opened this issue Dec 14, 2022 · 1 comment · Fixed by #72
Closed

Support for simple enums of a single type #20

jayvdb opened this issue Dec 14, 2022 · 1 comment · Fixed by #72

Comments

@jayvdb
Copy link
Collaborator

jayvdb commented Dec 14, 2022

afaics, enums consisting of a single type are not yet automatically supported. I can implement ToSql, but it would be nice to not need to. I see there is a #[butane_type(Text)], but it doesnt appear to help here. I have serde Serialize and Deserialize for the enum, and ToString/FromStr, but I get the error below.

    #[derive(
        Clone,
        Copy,
        Debug,
        Deserialize,
        Eq,
        Hash,
        Ord,
        PartialEq,
        PartialOrd,
        Serialize,
    )]
    pub enum FormatEnum {
        #[serde(rename = "tif")]
        Tif,
        #[serde(rename = "png")]
        Png,
    }

    impl ToString for FormatEnum {
        fn to_string(&self) -> String {
            match *self {
                Self::Tif => "tif".to_string(),
                Self::Png => "png".to_string(),
            }
        }
    }

    impl std::str::FromStr for FormatEnum {
        type Err = &'static str;
        fn from_str(value: &str) -> Result<Self, Self::Err> {
            match value {
                "tif" => Ok(Self::Tif),
                "png" => Ok(Self::Png),
                _ => Err("invalid value"),
            }
        }
    }

    impl Default for FormatEnum {
        fn default() -> Self {
            FormatEnum::Tif
        }
    }

error:

error[E0277]: the trait bound `FormatEnum: ToSql` is not satisfied
   --> rust/models/src/lib.rs:388:5
    |
388 |     #[model]
    |     ^^^^^^^^ the trait `ToSql` is not implemented for `FormatEnum`
    |
    = help: the following other types implement trait `ToSql`:
              ...
            and 32 others
    = note: required for `std::option::Option<FormatEnum>` to implement `ToSql`
    = note: required for `SqlVal` to implement `From<std::option::Option<FormatEnum>>`
    = note: required for `std::option::Option<FormatEnum>` to implement `Into<SqlVal>`

enums are mentioned at https://github.com/Electron100/butane/blob/master/notes.org#roadmap

@jayvdb
Copy link
Collaborator Author

jayvdb commented Feb 3, 2023

fwiw, diesel also doesnt automatically support enums.
https://github.com/terry90/diesel-enum-derive and similar are required to avoid writing boilerplate code.

Electron100 added a commit that referenced this issue May 2, 2023
Support automatic deriving of `FieldType` for simple enums. The
database representation is the string form of the variant name.

Fixes ##20

Future work could allow an option to use discriminant value instead of name.
Electron100 added a commit that referenced this issue May 3, 2023
Support automatic deriving of `FieldType` for simple enums. The
database representation is the string form of the variant name.

Fixes #20

Future work could allow an option to use discriminant value instead of name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant