Merge pull request #69 from telebofr/false_type

false_type
This commit is contained in:
Temirkhan Myrzamadi 2019-10-20 16:40:17 +00:00 committed by GitHub
commit 8bf578cf63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 2 deletions

View file

@ -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,

View file

@ -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
View 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);
}
}