serenity
serenity copied to clipboard
Statically prevent ‘invalid form body’ error when missing fields on `CreateButton`
There are a number of rules around fields that must be present in button objects:
- Non-link buttons must have a
custom_id, and cannot have aurl- Link buttons must have a
url, and cannot have acustom_id- Link buttons do not send an interaction to your app when clicked
Furthermore, type and style are never optional, and must always be present.
However, CreateButton allows leaving all of these blank; when such an incomplete button object is sent to Discord, it returns an ‘invalid form body’ error.
This could be avoided statically through the typesystem by requiring certain fields for certain types of button. For instance, something like this might work:
struct CreateButton {
kind: CreateButtonKind,
label: String,
emoji: Option<ReactionType>,
disabled: bool,
}
impl CreateButton {
fn new(kind: CreateButtonKind, label: String) -> Self {
Self {
kind,
label,
emoji: None,
disabled: false,
}
}
// methods to set fields
}
enum CreateButtonKind {
Link {
url: String,
},
NonLink {
style: Style,
custom_id: String,
},
}
enum Style {
Primary,
Secondary,
Success,
Danger,
}
Related: https://github.com/serenity-rs/serenity/issues/1905
Implemented in #2166, this issue can be closed