5
5
6
6
//! Subscription filters
7
7
8
- use alloc:: collections:: { BTreeMap , BTreeSet } ;
8
+ #[ cfg( not( feature = "std" ) ) ]
9
+ use alloc:: collections:: { BTreeMap as AllocMap , BTreeSet as AllocSet } ;
9
10
use alloc:: string:: { String , ToString } ;
10
11
use core:: fmt;
11
12
use core:: str:: FromStr ;
13
+ #[ cfg( feature = "std" ) ]
14
+ use std:: collections:: { HashMap as AllocMap , HashSet as AllocSet } ;
12
15
13
16
use bitcoin:: hashes:: sha256:: Hash as Sha256Hash ;
14
17
use bitcoin:: hashes:: Hash ;
@@ -316,17 +319,17 @@ impl IntoGenericTagValue for &str {
316
319
#[ derive( Debug , Clone , Default , PartialEq , Eq , Serialize , Deserialize ) ]
317
320
pub struct Filter {
318
321
/// List of [`EventId`]
319
- #[ serde( skip_serializing_if = "BTreeSet ::is_empty" ) ]
322
+ #[ serde( skip_serializing_if = "AllocSet ::is_empty" ) ]
320
323
#[ serde( default ) ]
321
- pub ids : BTreeSet < EventId > ,
324
+ pub ids : AllocSet < EventId > ,
322
325
/// List of [`XOnlyPublicKey`]
323
- #[ serde( skip_serializing_if = "BTreeSet ::is_empty" ) ]
326
+ #[ serde( skip_serializing_if = "AllocSet ::is_empty" ) ]
324
327
#[ serde( default ) ]
325
- pub authors : BTreeSet < XOnlyPublicKey > ,
328
+ pub authors : AllocSet < XOnlyPublicKey > ,
326
329
/// List of a kind numbers
327
- #[ serde( skip_serializing_if = "BTreeSet ::is_empty" ) ]
330
+ #[ serde( skip_serializing_if = "AllocSet ::is_empty" ) ]
328
331
#[ serde( default ) ]
329
- pub kinds : BTreeSet < Kind > ,
332
+ pub kinds : AllocSet < Kind > ,
330
333
/// It's a string describing a query in a human-readable form, i.e. "best nostr apps"
331
334
///
332
335
/// <https://github.com/nostr-protocol/nips/blob/master/50.md>
@@ -352,7 +355,7 @@ pub struct Filter {
352
355
deserialize_with = "deserialize_generic_tags"
353
356
) ]
354
357
#[ serde( default ) ]
355
- pub generic_tags : BTreeMap < Alphabet , BTreeSet < GenericTagValue > > ,
358
+ pub generic_tags : AllocMap < Alphabet , AllocSet < GenericTagValue > > ,
356
359
}
357
360
358
361
impl Filter {
@@ -644,7 +647,7 @@ impl Filter {
644
647
I : IntoIterator < Item = T > ,
645
648
T : IntoGenericTagValue ,
646
649
{
647
- let values: BTreeSet < GenericTagValue > = values
650
+ let values: AllocSet < GenericTagValue > = values
648
651
. into_iter ( )
649
652
. map ( |v| v. into_generic_tag_value ( ) )
650
653
. collect ( ) ;
@@ -663,7 +666,7 @@ impl Filter {
663
666
I : IntoIterator < Item = T > ,
664
667
T : IntoGenericTagValue ,
665
668
{
666
- let values: BTreeSet < GenericTagValue > = values
669
+ let values: AllocSet < GenericTagValue > = values
667
670
. into_iter ( )
668
671
. map ( |v| v. into_generic_tag_value ( ) )
669
672
. collect ( ) ;
@@ -684,7 +687,7 @@ impl JsonUtil for Filter {
684
687
}
685
688
686
689
fn serialize_generic_tags < S > (
687
- generic_tags : & BTreeMap < Alphabet , BTreeSet < GenericTagValue > > ,
690
+ generic_tags : & AllocMap < Alphabet , AllocSet < GenericTagValue > > ,
688
691
serializer : S ,
689
692
) -> Result < S :: Ok , S :: Error >
690
693
where
@@ -699,14 +702,14 @@ where
699
702
700
703
fn deserialize_generic_tags < ' de , D > (
701
704
deserializer : D ,
702
- ) -> Result < BTreeMap < Alphabet , BTreeSet < GenericTagValue > > , D :: Error >
705
+ ) -> Result < AllocMap < Alphabet , AllocSet < GenericTagValue > > , D :: Error >
703
706
where
704
707
D : Deserializer < ' de > ,
705
708
{
706
709
struct GenericTagsVisitor ;
707
710
708
711
impl < ' de > Visitor < ' de > for GenericTagsVisitor {
709
- type Value = BTreeMap < Alphabet , BTreeSet < GenericTagValue > > ;
712
+ type Value = AllocMap < Alphabet , AllocSet < GenericTagValue > > ;
710
713
711
714
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
712
715
formatter. write_str ( "map in which the keys are \" #X\" for some character X" )
@@ -716,13 +719,13 @@ where
716
719
where
717
720
M : MapAccess < ' de > ,
718
721
{
719
- let mut generic_tags = BTreeMap :: new ( ) ;
722
+ let mut generic_tags = AllocMap :: new ( ) ;
720
723
while let Some ( key) = map. next_key :: < String > ( ) ? {
721
724
let mut chars = key. chars ( ) ;
722
725
if let ( Some ( '#' ) , Some ( ch) , None ) = ( chars. next ( ) , chars. next ( ) , chars. next ( ) ) {
723
726
let tag: Alphabet = Alphabet :: from_str ( ch. to_string ( ) . as_str ( ) )
724
727
. map_err ( serde:: de:: Error :: custom) ?;
725
- let mut values: BTreeSet < GenericTagValue > = map. next_value ( ) ?;
728
+ let mut values: AllocSet < GenericTagValue > = map. next_value ( ) ?;
726
729
727
730
match tag {
728
731
Alphabet :: P => values. retain ( |v| matches ! ( v, GenericTagValue :: Pubkey ( _) ) ) ,
@@ -800,7 +803,7 @@ mod test {
800
803
}
801
804
802
805
#[ test]
803
- #[ cfg( feature = "std" ) ]
806
+ #[ cfg( not ( feature = "std" ) ) ]
804
807
fn test_filter_serialization ( ) {
805
808
let filter = Filter :: new ( )
806
809
. identifier ( "identifier" )
0 commit comments