char_collection/
macros.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/// Generate a [CharCollection] from a sequence of `char`s,
6/// [CharRanges](unic_char_range::CharRange), or Unicode [Blocks](unic_ucd_block::Block).
7///
8/// The macro can be used with either a comma-separated list of items, or with an expression
9/// representing set operations.
10///
11/// ```
12/// use char_collection::char_collect;
13/// use unicode_blocks::UnicodeBlockId;
14/// use unic_char_range::CharRange;
15///
16/// let c1 = char_collect!(
17///     'a'..='z',
18///     CharRange::closed('D', 'G'),
19///     UnicodeBlockId::Cyrillic,
20///     0x01..=0x05,
21///     '@');
22///
23/// let c2 = char_collect!({ ('a'..='z') - ('p'..='t') + UnicodeBlockId::Bengali });
24/// ```
25///
26/// *NOTE:* Parenthetical expressions currently aren't supported unless they start with a
27/// `CharCollection`.
28/// ```
29/// use char_collection::char_collect;
30///
31/// // This works:
32/// let c1 = char_collect!({ ('a'..='z') + (char_collect!('A'..='Z') - ('L'..='P')) });
33///
34/// // This doesn't:
35/// let c1 = char_collect!({ ('a'..='z') + (('A'..='Z') - ('L'..='P')) });
36/// ```
37#[macro_export]
38macro_rules! char_collect {
39    ({ $($x:tt)+ }) => {
40        {
41            $crate::CharCollection::new() + $($x)*
42        }
43    };
44
45    ( $( $x:expr ),* ) => {
46        {
47            // Allow unused mut in case the collection is empty.
48            #[allow(unused_mut)]
49            let mut col = $crate::CharCollection::new();
50            $(
51                col.insert(& $x);
52            )*
53            col
54        }
55    };
56}