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

Add custom deserializer for PermissionOverwriteType #512

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/types/entities/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::Debug;
use std::fmt::{Debug, Formatter};
Fixed Show fixed Hide fixed
use std::str::FromStr;

use crate::types::{
PermissionFlags, Shared,
Expand All @@ -24,6 +25,8 @@

#[cfg(feature = "client")]
use chorus_macros::{observe_option_vec, Composite, Updateable};
use serde::de::{Error, Visitor};
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
use crate::types::serde::string_or_u64;

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
Expand Down Expand Up @@ -155,14 +158,40 @@
}


#[derive(Debug, Serialize_repr, Deserialize_repr, Clone, PartialEq, Eq, PartialOrd)]
#[derive(Debug, Serialize_repr, Clone, PartialEq, Eq, PartialOrd)]
#[repr(u8)]
/// # Reference
pub enum PermissionOverwriteType {
Role = 0,
Member = 1,
}

impl From<u8> for PermissionOverwriteType {
fn from(v: u8) -> Self {
match v {
0 => PermissionOverwriteType::Role,
1 => PermissionOverwriteType::Member,
_ => unreachable!(),
}
}
}

impl FromStr for PermissionOverwriteType {
type Err = std::num::ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse::<u8>().map(PermissionOverwriteType::from)
}
}
kozabrada123 marked this conversation as resolved.
Show resolved Hide resolved

impl<'de> Deserialize<'de> for PermissionOverwriteType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let raw = string_or_u64(deserializer)?;

Ok(PermissionOverwriteType::from(raw as u8))
}
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
/// # Reference
/// See <https://discord-userdoccers.vercel.app/resources/channel#thread-metadata-object>
Expand Down