diff options
-rw-r--r-- | Cargo.lock | 6 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | dhall_core/Cargo.toml | 2 | ||||
-rw-r--r-- | dhall_core/src/parser.rs | 2 | ||||
-rw-r--r-- | improved_slice_patterns/Cargo.toml | 10 | ||||
-rw-r--r-- | improved_slice_patterns/README.md | 23 | ||||
-rw-r--r-- | improved_slice_patterns/src/lib.rs (renamed from iter_patterns/src/lib.rs) | 163 | ||||
-rw-r--r-- | iter_patterns/Cargo.toml | 9 |
8 files changed, 125 insertions, 92 deletions
@@ -83,7 +83,7 @@ name = "dhall_core" version = "0.1.0" dependencies = [ "dhall_generated_parser 0.1.0", - "iter_patterns 0.1.0", + "improved_slice_patterns 1.0.1", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -146,8 +146,8 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "iter_patterns" -version = "0.1.0" +name = "improved_slice_patterns" +version = "1.0.1" [[package]] name = "itertools" @@ -7,7 +7,7 @@ members = [ "dhall_generated_parser", "dhall_core", "dhall_generator", - "iter_patterns", + "improved_slice_patterns", ] # # Parser is super slow when not optimized diff --git a/dhall_core/Cargo.toml b/dhall_core/Cargo.toml index a2b242c..bfc418a 100644 --- a/dhall_core/Cargo.toml +++ b/dhall_core/Cargo.toml @@ -12,4 +12,4 @@ doctest = false itertools = "0.8.0" pest = "2.1" dhall_generated_parser = { path = "../dhall_generated_parser" } -iter_patterns = { path = "../iter_patterns" } +improved_slice_patterns = { path = "../improved_slice_patterns" } diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index e520f05..cae6cbd 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -171,7 +171,7 @@ macro_rules! make_parser { #[allow(unused_imports)] use ParsedValue::*; #[allow(unreachable_code)] - let res: $o = iter_patterns::match_vec!($children; + let res: $o = improved_slice_patterns::match_vec!($children; $( [$($args)*] => $body, )* [x..] => Err( format!("Unexpected children: {:?}", x.collect::<Vec<_>>()) diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml new file mode 100644 index 0000000..9c99e0d --- /dev/null +++ b/improved_slice_patterns/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "improved_slice_patterns" +version = "1.0.1" # remember to update html_root_url +authors = ["Nadrieril <nadrieril@users.noreply.github.com>"] +license = "MIT OR Apache-2.0" +edition = "2018" +description = "A tiny crate that provides macros to help matching on Vecs and iterators using the syntax of slice_patterns" +readme = "README.md" +repository = "https://github.com/Nadrieril/dhall-rust" +documentation = "https://docs.rs/improved_slice_patterns/" diff --git a/improved_slice_patterns/README.md b/improved_slice_patterns/README.md new file mode 100644 index 0000000..ab8fd7e --- /dev/null +++ b/improved_slice_patterns/README.md @@ -0,0 +1,23 @@ +# `improved_slice_patterns` + +A tiny crate that provides two macros to help matching +on `Vec`s and iterators using the syntax of [`slice_patterns`][slice_patterns] + +[slice_patterns]: https://doc.rust-lang.org/nightly/unstable-book/language-features/slice-patterns.html + +## License + +Licensed under either of + + * Apache License, Version 2.0 + (http://www.apache.org/licenses/LICENSE-2.0) + * MIT license + (http://opensource.org/licenses/MIT) + +at your option. + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions diff --git a/iter_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 657b538..3e459bb 100644 --- a/iter_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,27 +1,44 @@ #![feature(slice_patterns)] +#![doc(html_root_url = "https://docs.rs/improved_slice_patterns/1.0.1")] -/* Destructure an iterator using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * Contrary to slice_patterns, this allows moving out - * of the iterator. - * A variable length pattern (`x..`) is only allowed as the last - * pattern, unless the iterator is double-ended. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * destructure_iter!(vec.into_iter(); - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * } - * ) - * ``` - * -*/ +//! A tiny crate that provides two macros to help matching +//! on `Vec`s and iterators using the syntax of +//! [`slice_patterns`][slice_patterns] +//! +//! [slice_patterns]: https://doc.rust-lang.org/nightly/unstable-book/language-features/slice-patterns.html + +/// Destructure an iterator using the syntax of slice_patterns. +/// +/// Wraps the match body in `Some` if there was a match; returns +/// `None` otherwise. +/// +/// Contrary to slice_patterns, this allows moving out +/// of the iterator. +/// +/// A variable length pattern (`x..`) is only allowed as the last +/// pattern, unless the iterator is double-ended. +/// +/// Example: +/// ```edition2018 +/// use improved_slice_patterns::destructure_iter; +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = destructure_iter!(vec.into_iter(); +/// [Some(x), y.., z] => { +/// // x: usize +/// // y: impl Iterator<Option<usize>> +/// // z: Option<usize> +/// (x, y.collect::<Vec<_>>(), z) +/// } +/// ); +/// +/// assert_eq!(res, Some((1, vec![Some(2), Some(3)], None))); +/// +/// # Ok::<(), ()>(()) +/// ``` +/// +/// #[macro_export] macro_rules! destructure_iter { // Variable length pattern @@ -108,29 +125,53 @@ macro_rules! destructure_iter { }; } -/* Pattern-match on a vec using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * A variable length pattern (`x..`) returns an iterator. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * match_vec!(vec; - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * } - * [x, Some(0)] => { - * // x: Option<usize> - * }, - * [..] => { } - * ) - * ``` - * -*/ +/// Pattern-match on a vec using the syntax of slice_patterns. +/// +/// Wraps the match body in `Some` if there was a match; returns +/// `None` otherwise. +/// +/// Contrary to slice_patterns, this allows moving out +/// of the `Vec`. +/// +/// A variable length pattern (`x..`) returns an iterator. +/// +/// Example: +/// ```edition2018 +/// #![feature(slice_patterns)] +/// use improved_slice_patterns::match_vec; +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., None] => { +/// y.collect::<Vec<_>>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// [..] => vec![] +/// ); +/// +/// assert_eq!(res, Some(vec![Some(2), Some(3)])); +/// +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., Some(_)] => { +/// y.collect::<Vec<_>>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// ); +/// +/// assert_eq!(res, None); // there was no match +/// +/// # Ok::<(), ()>(()) +/// ``` +/// +/// #[macro_export] macro_rules! match_vec { // Variable length pattern @@ -227,38 +268,6 @@ macro_rules! match_vec { }; } -/* Pattern-match on an iterator using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * match_iter!(vec.into_iter(); - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * }, - * [x, Some(0)] => { - * // x: Option<usize> - * }, - * [..] => { - * ) - * ``` - * -*/ -#[macro_export] -macro_rules! match_iter { - ($arg:expr; $($args:tt)*) => { - { - let vec: Vec<_> = $arg.collect(); - $crate::match_vec!(vec; $($args)*) - } - }; -} - #[test] fn test() { let test = |v: Vec<Option<isize>>| { diff --git a/iter_patterns/Cargo.toml b/iter_patterns/Cargo.toml deleted file mode 100644 index 5a4c45f..0000000 --- a/iter_patterns/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "iter_patterns" -version = "0.1.0" -authors = ["Nadrieril <nadrieril@users.noreply.github.com>"] -license = "BSD-2-Clause" -edition = "2018" - -[lib] -doctest = false |