-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Alternative way for serializing booleans
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use core::fmt; | ||
use serde::de::{Error, Visitor}; | ||
use serde::Deserializer; | ||
|
||
pub fn deserialize_bool<'de, D>(deserializer: D) -> Result<bool, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_any(BoolVisitor) | ||
} | ||
|
||
pub fn deserialize_option_bool<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_any(BoolVisitor).map(Some) | ||
} | ||
|
||
pub struct BoolVisitor; | ||
|
||
impl Visitor<'_> for BoolVisitor { | ||
type Value = bool; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("bool or i8") | ||
} | ||
|
||
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(value == 1) | ||
} | ||
|
||
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
Ok(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod arrays; | ||
pub mod bool; | ||
pub mod de; | ||
pub mod ser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters