Remove ChatPrivate::type_

This commit is contained in:
Maybe Waffle 2022-06-26 20:45:33 +04:00
parent 376b71a12a
commit c1c20005f6
3 changed files with 73 additions and 45 deletions

View file

@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased ## unreleased
### Removed
- `ChatPrivate::type_` field ([#232][pr232])
[pr232]: https://github.com/teloxide/teloxide-core/pull/232
## 0.6.3 - 2022-06-21 ## 0.6.3 - 2022-06-21
### Fixed ### Fixed

View file

@ -76,15 +76,8 @@ pub struct ChatPublic {
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(from = "serde_helper::ChatPrivate", into = "serde_helper::ChatPrivate")]
pub struct 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 /// A username, for private chats, supergroups and channels if
/// available. /// available.
pub username: Option<String>, pub username: Option<String>,
@ -185,40 +178,6 @@ pub struct PublicChatSupergroup {
pub location: Option<ChatLocation>, 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 { impl Chat {
pub fn is_private(&self) -> bool { pub fn is_private(&self) -> bool {
matches!(self.kind, ChatKind::Private(_)) matches!(self.kind, ChatKind::Private(_))
@ -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)] #[cfg(test)]
mod tests { mod tests {
use serde_json::{from_str, to_string}; use serde_json::{from_str, to_string};
@ -484,7 +509,6 @@ mod tests {
Chat { Chat {
id: ChatId(0), id: ChatId(0),
kind: ChatKind::Private(ChatPrivate { kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some("username".into()), username: Some("username".into()),
first_name: Some("Anon".into()), first_name: Some("Anon".into()),
last_name: None, last_name: None,
@ -505,7 +529,6 @@ mod tests {
let chat = Chat { let chat = Chat {
id: ChatId(0), id: ChatId(0),
kind: ChatKind::Private(ChatPrivate { kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some("username".into()), username: Some("username".into()),
first_name: Some("Anon".into()), first_name: Some("Anon".into()),
last_name: None, last_name: None,

View file

@ -337,7 +337,6 @@ mod test {
chat: Chat { chat: Chat {
id: ChatId(218_485_655), id: ChatId(218_485_655),
kind: ChatKind::Private(ChatPrivate { kind: ChatKind::Private(ChatPrivate {
type_: (),
username: Some(String::from("WaffleLapkin")), username: Some(String::from("WaffleLapkin")),
first_name: Some(String::from("Waffle")), first_name: Some(String::from("Waffle")),
last_name: None, last_name: None,