char_collection/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// Generate a [CharCollection] from a sequence of `char`s,
/// [CharRanges](unic_char_range::CharRange), or Unicode [Blocks](unic_ucd_block::Block).
///
/// The macro can be used with either a comma-separated list of items, or with an expression
/// representing set operations.
///
/// ```
/// use char_collection::char_collect;
/// use unicode_blocks::UnicodeBlockId;
/// use unic_char_range::CharRange;
///
/// let c1 = char_collect!(
///     'a'..='z',
///     CharRange::closed('D', 'G'),
///     UnicodeBlockId::Cyrillic,
///     0x01..=0x05,
///     '@');
///
/// let c2 = char_collect!({ ('a'..='z') - ('p'..='t') + UnicodeBlockId::Bengali });
/// ```
///
/// *NOTE:* Parenthetical expressions currently aren't supported unless they start with a
/// `CharCollection`.
/// ```
/// use char_collection::char_collect;
///
/// // This works:
/// let c1 = char_collect!({ ('a'..='z') + (char_collect!('A'..='Z') - ('L'..='P')) });
///
/// // This doesn't:
/// let c1 = char_collect!({ ('a'..='z') + (('A'..='Z') - ('L'..='P')) });
/// ```
#[macro_export]
macro_rules! char_collect {
    ({ $($x:tt)+ }) => {
        {
            $crate::CharCollection::new() + $($x)*
        }
    };

    ( $( $x:expr ),* ) => {
        {
            // Allow unused mut in case the collection is empty.
            #[allow(unused_mut)]
            let mut col = $crate::CharCollection::new();
            $(
                col.insert(& $x);
            )*
            col
        }
    };
}