pub enum Patch<T> {
Keep,
Clear,
Set(T),
}Expand description
A field of a modify request that can be left unchanged, cleared or set (D-083, D-101).
Discord’s PATCH endpoints distinguish three cases for nullable fields: a field that is
left out keeps its current value, null clears it, and a value sets it. Patch names
these cases after what they do:
| Variant | Sent as | Effect |
|---|---|---|
Keep (default) | (field left out) | the value stays as it is |
Clear | null | the value is removed or reset |
Set | the value | the value is replaced |
Request bodies skip Keep fields with #[serde(skip_serializing_if = "Patch::is_keep")].
Serializing a Keep on its own is an error, because no JSON value means “leave out”.
discord.js counterpart: passing undefined, null or a value in the options of an edit
method.
§Examples
use djs_model::util::Patch;
let mut nickname: Patch<String> = Patch::default();
assert!(nickname.is_keep());
nickname = "Ferris".to_owned().into();
assert_eq!(nickname, Patch::Set("Ferris".to_owned()));
// `None` clears, `Some` sets.
assert_eq!(Patch::<u32>::from(None), Patch::Clear);
assert_eq!(Patch::from(Some(5)), Patch::Set(5));
assert_eq!(serde_json::to_string(&Patch::<u32>::Clear).unwrap(), "null");
assert_eq!(serde_json::to_string(&Patch::Set(5)).unwrap(), "5");Variants§
Keep
Leave the current value unchanged: the field is not sent.
Clear
Clear the value: the field is sent as null.
Set(T)
Set the value.
Implementations§
Source§impl<T> Patch<T>
impl<T> Patch<T>
Sourcepub const fn is_keep(&self) -> bool
pub const fn is_keep(&self) -> bool
Returns true for Keep (used to skip the field when serializing).
discord.js counterpart: none (an omitted option in discord.js).
Sourcepub const fn is_clear(&self) -> bool
pub const fn is_clear(&self) -> bool
Returns true for Clear.
discord.js counterpart: none (a null option in discord.js).
Sourcepub const fn is_set(&self) -> bool
pub const fn is_set(&self) -> bool
Returns true for Set.
discord.js counterpart: none (a given option in discord.js).
Sourcepub fn into_set(self) -> Option<T>
pub fn into_set(self) -> Option<T>
Returns the value of Set, if any.
discord.js counterpart: none (a given option in discord.js).