Skip to content

Commit

Permalink
Remove FilterOpt::BySome from (de-)serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Polyakov <arjentix@gmail.com>
  • Loading branch information
Arjentix authored and appetrosyan committed Sep 21, 2022
1 parent a9d558f commit 6de5613
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
47 changes: 42 additions & 5 deletions data_model/src/events/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use super::*;
/// Filter for all events
pub type EventFilter = FilterOpt<EntityFilter>;

/// Optional filter. May pass all items or may filter them by `F`
///
/// It's better than `Optional<F>` because `Optional` already has its own `filter` method and it
/// would be ugly to use fully qualified syntax to call `Filter::filter()` method on it.
/// Also `FilterOpt` variant names look better for filter needs
#[derive(
Clone,
PartialEq,
Expand All @@ -21,18 +26,50 @@ pub type EventFilter = FilterOpt<EntityFilter>;
IntoSchema,
Hash,
)]
/// Optional filter. May pass all items or may filter them by `F`
///
/// It's better than `Optional<F>` because `Optional` already has its own `filter` method and it
/// would be ugly to use fully qualified syntax to call `Filter::filter()` method on it.
/// Also `FilterOpt` variant names look better for filter needs
#[serde(untagged)]
pub enum FilterOpt<F: Filter> {
/// Accept all items that will be passed to `filter()` method
#[serde(with = "accept_all_as_string")]
AcceptAll,
/// Use filter `F` to choose acceptable items passed to `filter()` method
BySome(F),
}

mod accept_all_as_string {
//! Module to (de-)serialize `FilterOpt::AcceptAll` variant as string

#[cfg(not(feature = "std"))]
use alloc::format;

use serde::{Deserializer, Serializer};

/// Serialize bytes using `base64`
pub fn serialize<S: Serializer>(serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str("AcceptAll")
}

/// Deserialize bytes using `base64`
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<(), D::Error> {
struct Vis;
impl serde::de::Visitor<'_> for Vis {
type Value = ();

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("an AcceptAll string")
}

fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
if s == "AcceptAll" {
Ok(())
} else {
Err(E::custom(format!("expected AcceptAll, got {}", s)))
}
}
}
deserializer.deserialize_str(Vis)
}
}

impl<F: Filter> Filter for FilterOpt<F> {
type Event = F::Event;

Expand Down
2 changes: 1 addition & 1 deletion data_model/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ macro_rules! expression_serde_internal_repr {
}

mod serde_internal_repr {
/// Module with internal representation of [`Expression`] for (de-)serialization.
//! Module with internal representation of [`Expression`] for (de-)serialization.

use super::*;

Expand Down
4 changes: 1 addition & 3 deletions tools/parity_scale_decoder/samples/trigger.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
"technical_account": "alice@wonderland",
"filter": {
"Data": {
"BySome": {
"ByAccount": "AcceptAll"
}
"ByAccount": "AcceptAll"
}
},
"metadata": {}
Expand Down

0 comments on commit 6de5613

Please sign in to comment.