| 1 | #![cfg_attr(not(test), no_std)] |
| 2 | #![cfg_attr(feature = "nightly", feature(trusted_len))] |
| 3 | #![cfg_attr(feature = "nightly", feature(try_trait_v2))] |
| 4 | |
| 5 | //! Joinery provides generic joining of iterables with separators. While it is |
| 6 | //! primarily designed the typical use case of writing to a [writer][core::fmt::Write] |
| 7 | //! or creating a `String` by joining a list of data with some kind of string |
| 8 | //! separator (such as `", "`), it is fully generic and can be used to combine |
| 9 | //! any iterator or collection with any separator. In this way it is intended |
| 10 | //! to supercede the existing `SliceConcatExt::join` method, which only works |
| 11 | //! on slices and can only join with a matching type. |
| 12 | //! |
| 13 | //! # Examples |
| 14 | //! |
| 15 | //! Create a comma separated list: |
| 16 | //! |
| 17 | //! ``` |
| 18 | //! use joinery::Joinable; |
| 19 | //! |
| 20 | //! let result = vec![1, 2, 3, 4].join_with(", ").to_string(); |
| 21 | //! assert_eq!(result, "1, 2, 3, 4") |
| 22 | //! ``` |
| 23 | //! |
| 24 | //! `write!` a comma-separated list: |
| 25 | //! |
| 26 | //! ``` |
| 27 | //! use joinery::Joinable; |
| 28 | //! use std::fmt::Write; |
| 29 | //! |
| 30 | //! let join = vec![1, 2, 3, 4, 5].join_with(", "); |
| 31 | //! |
| 32 | //! let mut result = String::new(); |
| 33 | //! |
| 34 | //! write!(&mut result, "Numbers: {}", join); |
| 35 | //! assert_eq!(result, "Numbers: 1, 2, 3, 4, 5"); |
| 36 | //! |
| 37 | //! // Note that joins are stateless; they can be reused after writing |
| 38 | //! let result2 = join.to_string(); |
| 39 | //! assert_eq!(result2, "1, 2, 3, 4, 5"); |
| 40 | //! ``` |
| 41 | //! |
| 42 | //! Join any iterator: |
| 43 | //! |
| 44 | //! ``` |
| 45 | //! use joinery::JoinableIterator; |
| 46 | //! |
| 47 | //! let join = (0..10) |
| 48 | //! .filter(|x| *x % 2 == 0) |
| 49 | //! .map(|x: i32| x.pow(2)) |
| 50 | //! .join_with(", "); |
| 51 | //! |
| 52 | //! let result = join.to_string(); |
| 53 | //! assert_eq!(result, "0, 4, 16, 36, 64") |
| 54 | //! ``` |
| 55 | //! |
| 56 | //! Iterate over joins: |
| 57 | //! |
| 58 | //! ``` |
| 59 | //! use joinery::{Joinable, JoinItem}; |
| 60 | //! |
| 61 | //! // Note that the collection values and the separator can be different types |
| 62 | //! let join = ["some", "sample", "text"].join_with(' '); |
| 63 | //! let mut join_iter = (&join).into_iter(); |
| 64 | //! |
| 65 | //! assert_eq!(join_iter.next(), Some(JoinItem::Element(&"some"))); |
| 66 | //! assert_eq!(join_iter.next(), Some(JoinItem::Separator(&' '))); |
| 67 | //! assert_eq!(join_iter.next(), Some(JoinItem::Element(&"sample"))); |
| 68 | //! assert_eq!(join_iter.next(), Some(JoinItem::Separator(&' '))); |
| 69 | //! assert_eq!(join_iter.next(), Some(JoinItem::Element(&"text"))); |
| 70 | //! assert_eq!(join_iter.next(), None); |
| 71 | //! ``` |
| 72 | //! |
| 73 | //! Display the first 5 consecutive multiples of 1-5 on separate lines: |
| 74 | //! |
| 75 | //! ``` |
| 76 | //! use joinery::{Joinable, JoinableIterator}; |
| 77 | //! let multiples = 1..=5; |
| 78 | //! let ranges = multiples.map(|m| (1..=5).map(move |n| n * m)); |
| 79 | //! |
| 80 | //! let lines = ranges.map(|range| range.join_with(", ")); |
| 81 | //! let result = lines.join_with('\n').to_string(); |
| 82 | //! assert_eq!(result, "1, 2, 3, 4, 5\n\ |
| 83 | //! 2, 4, 6, 8, 10\n\ |
| 84 | //! 3, 6, 9, 12, 15\n\ |
| 85 | //! 4, 8, 12, 16, 20\n\ |
| 86 | //! 5, 10, 15, 20, 25"); |
| 87 | //! ``` |
| 88 | |
| 89 | pub mod iter; |
| 90 | pub mod join; |
| 91 | pub mod separators; |
| 92 | |
| 93 | pub use crate::iter::{JoinItem, JoinIter, JoinableIterator}; |
| 94 | pub use crate::join::{Join, Joinable, Separator}; |
| 95 | |
| 96 | /// The joinery prelude |
| 97 | pub mod prelude { |
| 98 | pub use crate::iter::JoinableIterator; |
| 99 | pub use crate::join::Joinable; |
| 100 | } |