mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
commit
8bf578cf63
3 changed files with 83 additions and 2 deletions
|
@ -2,6 +2,7 @@ use reqwest::StatusCode;
|
|||
|
||||
use crate::{
|
||||
requests::ResponseResult, types::ResponseParameters, RequestError,
|
||||
types::{True, False}
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -10,14 +11,14 @@ pub enum TelegramResponse<R> {
|
|||
Ok {
|
||||
/// A dummy field. Used only for deserialization.
|
||||
#[allow(dead_code)]
|
||||
ok: bool, // TODO: True type
|
||||
ok: True,
|
||||
|
||||
result: R,
|
||||
},
|
||||
Err {
|
||||
/// A dummy field. Used only for deserialization.
|
||||
#[allow(dead_code)]
|
||||
ok: bool, // TODO: False type
|
||||
ok: False,
|
||||
|
||||
description: String,
|
||||
error_code: u16,
|
||||
|
|
|
@ -47,6 +47,7 @@ pub use sticker::*;
|
|||
pub use sticker_set::*;
|
||||
pub use successful_payment::*;
|
||||
pub use unit_true::*;
|
||||
pub use unit_false::*;
|
||||
pub use update::*;
|
||||
pub use user::*;
|
||||
pub use user_profile_photos::*;
|
||||
|
@ -130,6 +131,7 @@ mod shipping_query;
|
|||
mod sticker;
|
||||
mod sticker_set;
|
||||
mod successful_payment;
|
||||
mod unit_false;
|
||||
mod unit_true;
|
||||
mod update;
|
||||
mod user;
|
||||
|
|
78
src/types/unit_false.rs
Normal file
78
src/types/unit_false.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::de::Visitor;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
|
||||
pub struct False;
|
||||
|
||||
impl std::convert::TryFrom<bool> for False {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: bool) -> Result<Self, Self::Error> {
|
||||
#[allow(clippy::match_bool)]
|
||||
match value {
|
||||
true => Err(()),
|
||||
false => Ok(False),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for False {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>
|
||||
{
|
||||
deserializer.deserialize_bool(FalseVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct FalseVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for FalseVisitor {
|
||||
type Value = False;
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(formatter, "bool, equal to `false`")
|
||||
}
|
||||
|
||||
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
#[allow(clippy::match_bool)]
|
||||
match value {
|
||||
true => Err(E::custom("expected `false`, found `true`")),
|
||||
false => Ok(False)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for False {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer
|
||||
{
|
||||
serializer.serialize_bool(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::{from_str, to_string};
|
||||
|
||||
use super::False;
|
||||
|
||||
#[test]
|
||||
fn unit_false_de() {
|
||||
let json = "false";
|
||||
let expected = False;
|
||||
let actual = from_str(json).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unit_false_se() {
|
||||
let actual = to_string(&False).unwrap();
|
||||
let expected = "false";
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue