Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #232 from teloxide/chat_private_type__removal
Browse files Browse the repository at this point in the history
Remove `ChatPrivate::type_`
  • Loading branch information
WaffleLapkin authored Jun 26, 2022
2 parents 376b71a + c1c2000 commit 46561b0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## unreleased

### Removed

- `ChatPrivate::type_` field ([#232][pr232])

[pr232]: https://github.com/teloxide/teloxide-core/pull/232

## 0.6.3 - 2022-06-21

### Fixed
Expand Down
111 changes: 67 additions & 44 deletions src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,8 @@ pub struct ChatPublic {

#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(from = "serde_helper::ChatPrivate", into = "serde_helper::ChatPrivate")]
pub struct ChatPrivate {
/// A dummy field. Used to ensure that the `type` field is equal to
/// `private`.
#[serde(rename = "type")]
#[serde(deserialize_with = "assert_private_field")]
#[serde(serialize_with = "serialize_private_field")]
// FIXME(waffle): remove this entirely (replace with custom De/Serialize impl)
pub type_: (),

/// A username, for private chats, supergroups and channels if
/// available.
pub username: Option<String>,
Expand Down Expand Up @@ -185,40 +178,6 @@ pub struct PublicChatSupergroup {
pub location: Option<ChatLocation>,
}

struct PrivateChatKindVisitor;

impl<'de> serde::de::Visitor<'de> for PrivateChatKindVisitor {
type Value = ();

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, r#"field equal to "private""#)
}

fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
match v {
"private" => Ok(()),
_ => Err(E::invalid_value(
serde::de::Unexpected::Str(v),
&r#""private""#,
)),
}
}
}

fn assert_private_field<'de, D>(des: D) -> Result<(), D::Error>
where
D: serde::Deserializer<'de>,
{
des.deserialize_str(PrivateChatKindVisitor)
}

fn serialize_private_field<S>(_: &(), ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ser.serialize_str("private")
}

impl Chat {
pub fn is_private(&self) -> bool {
matches!(self.kind, ChatKind::Private(_))
Expand Down Expand Up @@ -450,6 +409,72 @@ impl Chat {
}
}

mod serde_helper {
use crate::types::True;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
enum Type {
#[allow(non_camel_case_types)]
private,
}

#[derive(Serialize, Deserialize)]
pub(super) struct ChatPrivate {
/// A dummy field. Used to ensure that the `type` field is equal to
/// `private`.
r#type: Type,

username: Option<String>,
first_name: Option<String>,
last_name: Option<String>,
bio: Option<String>,
has_private_forwards: Option<True>,
}

impl From<ChatPrivate> for super::ChatPrivate {
fn from(
ChatPrivate {
r#type: _,
username,
first_name,
last_name,
bio,
has_private_forwards,
}: ChatPrivate,
) -> Self {
Self {
username,
first_name,
last_name,
bio,
has_private_forwards,
}
}
}

impl From<super::ChatPrivate> for ChatPrivate {
fn from(
super::ChatPrivate {
username,
first_name,
last_name,
bio,
has_private_forwards,
}: super::ChatPrivate,
) -> Self {
Self {
r#type: Type::private,
username,
first_name,
last_name,
bio,
has_private_forwards,
}
}
}
}

#[cfg(test)]
mod tests {
use serde_json::{from_str, to_string};
Expand Down Expand Up @@ -484,7 +509,6 @@ mod tests {
Chat {
id: ChatId(0),
kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some("username".into()),
first_name: Some("Anon".into()),
last_name: None,
Expand All @@ -505,7 +529,6 @@ mod tests {
let chat = Chat {
id: ChatId(0),
kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some("username".into()),
first_name: Some("Anon".into()),
last_name: None,
Expand Down
1 change: 0 additions & 1 deletion src/types/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ mod test {
chat: Chat {
id: ChatId(218_485_655),
kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some(String::from("WaffleLapkin")),
first_name: Some(String::from("Waffle")),
last_name: None,
Expand Down

0 comments on commit 46561b0

Please sign in to comment.