aboutsummaryrefslogtreecommitdiff
path: root/src/utils/builder/edit_profile.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-04-11 08:15:37 -0700
committerZeyla Hellyer <[email protected]>2017-04-11 10:52:43 -0700
commitf6b27eb39c042e6779edc2d5d4b6e6c27d133eaf (patch)
treea6169fee3bf9ea75391101577dcb2982e3daa388 /src/utils/builder/edit_profile.rs
parentClippy lints + permission byte literals (diff)
downloadserenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.tar.xz
serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.zip
Switch to using serde for deserialization
The current build system is rudimentary, incomplete, and rigid, offering little in the way of customizing decoding options. To solve this, switch to using serde-derive with custom Deserialization implementations. This allows very simple deserialization when special logic does not need to be applied, yet allows us to implement our own deserialization logic when required. The problem with the build system was that it built enums and structs from YAML files. This is not so good, because it requires creating a custom build system (which was rudimentary), creating "special struct configs" when logic needed to be ever so slightly extended (rigid), and if special logic needed to be applied, a custom deserialization method would have been needed to be made anyway (incomplete). To solve this, switch to serde-derive and implementing Deserialize ourselves where required. This reduces YAML definitions that might look like: ```yaml --- name: Group description: > A group channel, potentially including other users, separate from a [`Guild`]. [`Guild`]: struct.Guild.html fields: - name: channel_id description: The Id of the group channel. from: id type: ChannelId - name: icon description: The optional icon of the group channel. optional: true type: string - name: last_message_id description: The Id of the last message sent. optional: true type: MessageId - name: last_pin_timestamp description: Timestamp of the latest pinned message. optional: true type: string - name: name description: The name of the group channel. optional: true type: string - name: owner_id description: The Id of the group channel creator. type: UserId - name: recipients description: Group channel's members. custom: decode_users t: UserId, Arc<RwLock<User>> type: hashmap ``` to: ```rs /// A group channel - potentially including other [`User`]s - separate from a /// [`Guild`]. /// /// [`Guild`]: struct.Guild.html /// [`User`]: struct.User.html pub struct Group { /// The Id of the group channel. #[serde(rename="id")] pub channel_id: ChannelId, /// The optional icon of the group channel. pub icon: Option<String>, /// The Id of the last message sent. pub last_message_id: Option<MessageId>, /// Timestamp of the latest pinned message. pub last_pin_timestamp: Option<String>, /// The name of the group channel. pub name: Option<String>, /// The Id of the group owner. pub owner_id: UserId, /// A map of the group's recipients. #[serde(deserialize_with="deserialize_users")] pub recipients: HashMap<UserId, Arc<RwLock<User>>>, } ``` This is much simpler and does not have as much boilerplate. There should not be any backwards incompatible changes other than the old, public - yet undocumented (and hidden from documentation) - decode methods being removed. Due to the nature of this commit, field names may be incorrect, and will need to be corrected as deserialization errors are found.
Diffstat (limited to 'src/utils/builder/edit_profile.rs')
-rw-r--r--src/utils/builder/edit_profile.rs46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/utils/builder/edit_profile.rs b/src/utils/builder/edit_profile.rs
index 83f1da2..680a371 100644
--- a/src/utils/builder/edit_profile.rs
+++ b/src/utils/builder/edit_profile.rs
@@ -1,12 +1,11 @@
-use serde_json::builder::ObjectBuilder;
-use serde_json::Value;
-use std::default::Default;
+use ::internal::prelude::*;
/// A builder to edit the current user's settings, to be used in conjunction
/// with [`Context::edit_profile`].
///
/// [`Context::edit_profile`]: ../../client/struct.Context.html#method.edit_profile
-pub struct EditProfile(pub ObjectBuilder);
+#[derive(Default)]
+pub struct EditProfile(pub JsonMap);
impl EditProfile {
/// Sets the avatar of the current user. `None` can be passed to remove an
@@ -33,11 +32,12 @@ impl EditProfile {
/// ```
///
/// [`utils::read_image`]: ../fn.read_image.html
- pub fn avatar(self, icon: Option<&str>) -> Self {
- EditProfile(self.0
- .insert("avatar",
- icon.map_or_else(|| Value::Null,
- |x| Value::String(x.to_owned()))))
+ pub fn avatar(mut self, avatar: Option<&str>) -> Self {
+ let avatar = avatar.map_or(Value::Null, |x| Value::String(x.to_owned()));
+
+ self.0.insert("avatar".to_owned(), avatar);
+
+ self
}
/// Modifies the current user's email address.
@@ -50,8 +50,10 @@ impl EditProfile {
/// **Note**: This can only be used by user accounts.
///
/// [provided]: #method.password
- pub fn email(self, email: &str) -> Self {
- EditProfile(self.0.insert("email", email))
+ pub fn email(mut self, email: &str) -> Self {
+ self.0.insert("email".to_owned(), Value::String(email.to_owned()));
+
+ self
}
/// Modifies the current user's password.
@@ -60,8 +62,10 @@ impl EditProfile {
/// [provided].
///
/// [provided]: #method.password
- pub fn new_password(self, new_password: &str) -> Self {
- EditProfile(self.0.insert("new_password", new_password))
+ pub fn new_password(mut self, new_password: &str) -> Self {
+ self.0.insert("new_password".to_owned(), Value::String(new_password.to_owned()));
+
+ self
}
/// Used for providing the current password as verification when
@@ -69,8 +73,10 @@ impl EditProfile {
///
/// [modifying the password]: #method.new_password
/// [modifying the associated email address]: #method.email
- pub fn password(self, password: &str) -> Self {
- EditProfile(self.0.insert("password", password))
+ pub fn password(mut self, password: &str) -> Self {
+ self.0.insert("password".to_owned(), Value::String(password.to_owned()));
+
+ self
}
/// Modifies the current user's username.
@@ -79,13 +85,9 @@ impl EditProfile {
/// and current discriminator, a new unique discriminator will be assigned.
/// If there are no available discriminators with the requested username,
/// an error will occur.
- pub fn username(self, username: &str) -> Self {
- EditProfile(self.0.insert("username", username))
- }
-}
+ pub fn username(mut self, username: &str) -> Self {
+ self.0.insert("username".to_owned(), Value::String(username.to_owned()));
-impl Default for EditProfile {
- fn default() -> EditProfile {
- EditProfile(ObjectBuilder::new())
+ self
}
}