From c9de25aa4e16d8352329c3d05b557af9cb9ab651 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Sat, 15 May 2021 14:16:26 -0700 Subject: adding serialize to true --- src/models/search.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/models/search.rs b/src/models/search.rs index 7b14aa2..7cd23dd 100644 --- a/src/models/search.rs +++ b/src/models/search.rs @@ -1,5 +1,5 @@ use crate::models::paging::Paging; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Copy, Clone)] #[serde(rename_all = "snake_case")] @@ -60,10 +60,19 @@ pub enum TextCondition { DoesNotContain(String), StartsWith(String), EndsWith(String), + #[serde(serialize_with = "serialize_to_true")] IsEmpty, + #[serde(serialize_with = "serialize_to_true")] IsNotEmpty, } +fn serialize_to_true(serializer: S) -> Result +where + S: Serializer, +{ + serializer.serialize_bool(true) +} + #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] #[serde(rename_all = "lowercase")] pub enum PropertyCondition { @@ -144,5 +153,33 @@ mod tests { Ok(()) } + + #[test] + fn text_property_contains() -> Result<(), Box> { + let json = serde_json::to_string(&FilterCondition { + property: "Name".to_string(), + condition: Text(TextCondition::Contains("Test".to_string())), + })?; + assert_eq!( + dbg!(json), + r#"{"property":"Name","text":{"contains":"Test"}}"# + ); + + Ok(()) + } + + #[test] + fn text_property_is_empty() -> Result<(), Box> { + let json = serde_json::to_string(&FilterCondition { + property: "Name".to_string(), + condition: Text(TextCondition::IsEmpty), + })?; + assert_eq!( + dbg!(json), + r#"{"property":"Name","text":{"is_empty":true}}"# + ); + + Ok(()) + } } } -- cgit v1.2.3