Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use std::iter::FromIterator; |
| 2 | use std::slice; |
| 3 | use std::vec; |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 4 | #[cfg(feature = "extra-traits")] |
| 5 | use std::fmt::{self, Debug}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 7 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 8 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 9 | pub struct Punctuated<T, P> { |
| 10 | inner: Vec<(T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 11 | } |
| 12 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 13 | impl<T, P> Punctuated<T, P> { |
| 14 | pub fn new() -> Punctuated<T, P> { |
| 15 | Punctuated { inner: Vec::new() } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | pub fn is_empty(&self) -> bool { |
| 19 | self.inner.len() == 0 |
| 20 | } |
| 21 | |
| 22 | pub fn len(&self) -> usize { |
| 23 | self.inner.len() |
| 24 | } |
| 25 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 26 | pub fn first(&self) -> Option<Element<&T, &P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 27 | self.inner.first().map(|&(ref t, ref d)| match *d { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 28 | Some(ref d) => Element::Punctuated(t, d), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 29 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 30 | }) |
| 31 | } |
| 32 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 33 | pub fn last(&self) -> Option<Element<&T, &P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 34 | self.inner.last().map(|&(ref t, ref d)| match *d { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 35 | Some(ref d) => Element::Punctuated(t, d), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 36 | None => Element::End(t), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 37 | }) |
| 38 | } |
| 39 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 40 | pub fn last_mut(&mut self) -> Option<Element<&mut T, &mut P>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 41 | self.inner |
| 42 | .last_mut() |
| 43 | .map(|&mut (ref mut t, ref mut d)| match *d { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 44 | Some(ref mut d) => Element::Punctuated(t, d), |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 45 | None => Element::End(t), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 46 | }) |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 49 | pub fn iter(&self) -> Iter<T, P> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 50 | Iter { |
| 51 | inner: self.inner.iter(), |
| 52 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 53 | } |
| 54 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 55 | pub fn iter_mut(&mut self) -> IterMut<T, P> { |
| 56 | IterMut { |
| 57 | inner: self.inner.iter_mut(), |
| 58 | } |
| 59 | } |
| 60 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 61 | pub fn elements(&self) -> Elements<T, P> { |
| 62 | Elements { |
| 63 | inner: self.inner.iter(), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub fn elements_mut(&mut self) -> ElementsMut<T, P> { |
| 68 | ElementsMut { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 69 | inner: self.inner.iter_mut(), |
| 70 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 73 | pub fn into_elements(self) -> IntoElements<T, P> { |
| 74 | IntoElements { |
| 75 | inner: self.inner.into_iter(), |
| 76 | } |
| 77 | } |
| 78 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 79 | pub fn push_item(&mut self, item: T) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 80 | assert!(self.empty_or_trailing()); |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 81 | self.inner.push((item, None)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 82 | } |
| 83 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 84 | pub fn push_punct(&mut self, punctuation: P) { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 85 | assert!(!self.is_empty()); |
| 86 | let last = self.inner.last_mut().unwrap(); |
| 87 | assert!(last.1.is_none()); |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 88 | last.1 = Some(punctuation); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 91 | pub fn pop(&mut self) -> Option<Element<T, P>> { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 92 | self.inner.pop().map(|(t, d)| Element::new(t, d)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 95 | pub fn trailing_punct(&self) -> bool { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 96 | self.inner.last().map(|last| last.1.is_some()).unwrap_or(false) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 97 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 98 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 99 | /// Returns true if either this `Punctuated` is empty, or it has a trailing |
| 100 | /// punctuation. |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 101 | /// |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 102 | /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 103 | pub fn empty_or_trailing(&self) -> bool { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 104 | self.inner.last().map(|last| last.1.is_some()).unwrap_or(true) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 105 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 108 | impl<T, P> Punctuated<T, P> |
| 109 | where |
| 110 | P: Default, |
| 111 | { |
| 112 | pub fn push(&mut self, item: T) { |
| 113 | if !self.empty_or_trailing() { |
| 114 | self.push_punct(Default::default()); |
| 115 | } |
| 116 | self.push_item(item); |
| 117 | } |
| 118 | } |
| 119 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 120 | #[cfg(feature = "extra-traits")] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 121 | impl<T: Debug, P: Debug> Debug for Punctuated<T, P> { |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 122 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 123 | self.inner.fmt(f) |
| 124 | } |
| 125 | } |
| 126 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 127 | impl<T, P> FromIterator<Element<T, P>> for Punctuated<T, P> { |
| 128 | fn from_iter<I: IntoIterator<Item = Element<T, P>>>(i: I) -> Self { |
| 129 | let mut ret = Punctuated::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 130 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 131 | ret |
| 132 | } |
| 133 | } |
| 134 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 135 | impl<T, P> Extend<Element<T, P>> for Punctuated<T, P> { |
| 136 | fn extend<I: IntoIterator<Item = Element<T, P>>>(&mut self, i: I) { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 137 | for elem in i { |
| 138 | match elem { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 139 | Element::Punctuated(a, b) => self.inner.push((a, Some(b))), |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 140 | Element::End(a) => self.inner.push((a, None)), |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 146 | impl<T, P> IntoIterator for Punctuated<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 147 | type Item = T; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 148 | type IntoIter = IntoIter<T, P>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 149 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 150 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 151 | IntoIter { |
| 152 | inner: self.inner.into_iter(), |
| 153 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 157 | impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> { |
| 158 | type Item = &'a T; |
| 159 | type IntoIter = Iter<'a, T, P>; |
| 160 | |
| 161 | fn into_iter(self) -> Self::IntoIter { |
| 162 | Punctuated::iter(self) |
| 163 | } |
| 164 | } |
| 165 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 166 | impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> { |
| 167 | type Item = &'a mut T; |
| 168 | type IntoIter = IterMut<'a, T, P>; |
| 169 | |
| 170 | fn into_iter(self) -> Self::IntoIter { |
| 171 | Punctuated::iter_mut(self) |
| 172 | } |
| 173 | } |
| 174 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 175 | impl<T, P> Default for Punctuated<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 176 | fn default() -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 177 | Punctuated::new() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 181 | pub struct Elements<'a, T: 'a, P: 'a> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 182 | inner: slice::Iter<'a, (T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | } |
| 184 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 185 | impl<'a, T, P> Iterator for Elements<'a, T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 186 | type Item = Element<&'a T, &'a P>; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 187 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 188 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 189 | self.inner.next().map(|pair| match pair.1 { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 190 | Some(ref p) => Element::Punctuated(&pair.0, p), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 191 | None => Element::End(&pair.0), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 192 | }) |
| 193 | } |
| 194 | } |
| 195 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 196 | pub struct ElementsMut<'a, T: 'a, P: 'a> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 197 | inner: slice::IterMut<'a, (T, Option<P>)>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 198 | } |
| 199 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 200 | impl<'a, T, P> Iterator for ElementsMut<'a, T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 201 | type Item = Element<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 202 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 203 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 204 | self.inner.next().map(|pair| match pair.1 { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 205 | Some(ref mut p) => Element::Punctuated(&mut pair.0, p), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 206 | None => Element::End(&mut pair.0), |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 207 | }) |
| 208 | } |
| 209 | } |
| 210 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 211 | pub struct IntoElements<T, P> { |
| 212 | inner: vec::IntoIter<(T, Option<P>)>, |
| 213 | } |
| 214 | |
| 215 | impl<T, P> Iterator for IntoElements<T, P> { |
| 216 | type Item = Element<T, P>; |
| 217 | |
| 218 | fn next(&mut self) -> Option<Self::Item> { |
| 219 | self.inner.next().map(|pair| match pair.1 { |
| 220 | Some(p) => Element::Punctuated(pair.0, p), |
| 221 | None => Element::End(pair.0), |
| 222 | }) |
| 223 | } |
| 224 | } |
| 225 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 226 | pub struct IntoIter<T, P> { |
| 227 | inner: vec::IntoIter<(T, Option<P>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 228 | } |
| 229 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 230 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 231 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 232 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 233 | fn next(&mut self) -> Option<Self::Item> { |
| 234 | self.inner.next().map(|pair| pair.0) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | pub struct Iter<'a, T: 'a, P: 'a> { |
| 239 | inner: slice::Iter<'a, (T, Option<P>)>, |
| 240 | } |
| 241 | |
| 242 | impl<'a, T, P> Iterator for Iter<'a, T, P> { |
| 243 | type Item = &'a T; |
| 244 | |
| 245 | fn next(&mut self) -> Option<Self::Item> { |
| 246 | self.inner.next().map(|pair| &pair.0) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 250 | pub struct IterMut<'a, T: 'a, P: 'a> { |
| 251 | inner: slice::IterMut<'a, (T, Option<P>)>, |
| 252 | } |
| 253 | |
| 254 | impl<'a, T, P> Iterator for IterMut<'a, T, P> { |
| 255 | type Item = &'a mut T; |
| 256 | |
| 257 | fn next(&mut self) -> Option<Self::Item> { |
| 258 | self.inner.next().map(|pair| &mut pair.0) |
| 259 | } |
| 260 | } |
| 261 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 262 | pub enum Element<T, P> { |
| 263 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 264 | End(T), |
| 265 | } |
| 266 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 267 | impl<T, P> Element<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 268 | pub fn into_item(self) -> T { |
| 269 | match self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 270 | Element::Punctuated(t, _) | Element::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | pub fn item(&self) -> &T { |
| 275 | match *self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 276 | Element::Punctuated(ref t, _) | Element::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | pub fn item_mut(&mut self) -> &mut T { |
| 281 | match *self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 282 | Element::Punctuated(ref mut t, _) | Element::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 286 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 287 | match *self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 288 | Element::Punctuated(_, ref d) => Some(d), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 289 | Element::End(_) => None, |
| 290 | } |
| 291 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 292 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 293 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 294 | match d { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 295 | Some(d) => Element::Punctuated(t, d), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 296 | None => Element::End(t), |
| 297 | } |
| 298 | } |
| 299 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 300 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 301 | match self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 302 | Element::Punctuated(t, d) => (t, Some(d)), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 303 | Element::End(t) => (t, None), |
| 304 | } |
| 305 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 308 | #[cfg(feature = "parsing")] |
| 309 | mod parsing { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 310 | use super::Punctuated; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 311 | use synom::Synom; |
| 312 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 313 | use parse_error; |
| 314 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 315 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 316 | impl<T, P> Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 317 | where |
| 318 | T: Synom, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 319 | P: Synom, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 320 | { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 321 | pub fn parse_separated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 322 | Self::parse(input, T::parse, false) |
| 323 | } |
| 324 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 325 | pub fn parse_separated_nonempty(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 326 | Self::parse_separated_nonempty_with(input, T::parse) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 327 | } |
| 328 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 329 | pub fn parse_terminated(input: Cursor) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 330 | Self::parse_terminated_with(input, T::parse) |
| 331 | } |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 332 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 333 | pub fn parse_terminated_nonempty(input: Cursor) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 334 | Self::parse_terminated_nonempty_with(input, T::parse) |
| 335 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 336 | } |
| 337 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 338 | impl<T, P> Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 339 | where |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 340 | P: Synom, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 341 | { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 342 | pub fn parse_separated_with( |
| 343 | input: Cursor, |
| 344 | parse: fn(Cursor) -> PResult<T>, |
| 345 | ) -> PResult<Self> { |
| 346 | Self::parse(input, parse, false) |
| 347 | } |
| 348 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 349 | pub fn parse_separated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 350 | input: Cursor, |
| 351 | parse: fn(Cursor) -> PResult<T>, |
| 352 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 353 | match Self::parse(input, parse, false) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 354 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 355 | other => other, |
| 356 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 359 | pub fn parse_terminated_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 360 | input: Cursor, |
| 361 | parse: fn(Cursor) -> PResult<T>, |
| 362 | ) -> PResult<Self> { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 363 | Self::parse(input, parse, true) |
| 364 | } |
| 365 | |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 366 | pub fn parse_terminated_nonempty_with( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 367 | input: Cursor, |
| 368 | parse: fn(Cursor) -> PResult<T>, |
| 369 | ) -> PResult<Self> { |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 370 | match Self::parse(input, parse, true) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 371 | Ok((ref b, _)) if b.is_empty() => parse_error(), |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 372 | other => other, |
| 373 | } |
| 374 | } |
| 375 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 376 | fn parse( |
| 377 | mut input: Cursor, |
| 378 | parse: fn(Cursor) -> PResult<T>, |
| 379 | terminated: bool, |
| 380 | ) -> PResult<Self> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 381 | let mut res = Punctuated::new(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 382 | |
| 383 | // get the first element |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 384 | match parse(input) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 385 | Err(_) => Ok((res, input)), |
| 386 | Ok((o, i)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 387 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 388 | return parse_error(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 389 | } |
| 390 | input = i; |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 391 | res.push_item(o); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 392 | |
| 393 | // get the separator first |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 394 | while let Ok((s, i2)) = P::parse(input) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 395 | if i2 == input { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | |
| 399 | // get the element next |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 400 | if let Ok((o3, i3)) = parse(i2) { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 401 | if i3 == i2 { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 402 | break; |
| 403 | } |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 404 | res.push_punct(s); |
| 405 | res.push_item(o3); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 406 | input = i3; |
| 407 | } else { |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | if terminated { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 412 | if let Ok((sep, after)) = P::parse(input) { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 413 | res.push_punct(sep); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 414 | input = after; |
| 415 | } |
| 416 | } |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 417 | Ok((res, input)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 424 | #[cfg(feature = "printing")] |
| 425 | mod printing { |
| 426 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 427 | use quote::{ToTokens, Tokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 428 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 429 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 430 | where |
| 431 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 432 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 433 | { |
| 434 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame^] | 435 | tokens.append_all(self.elements()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 439 | impl<T, P> ToTokens for Element<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 440 | where |
| 441 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 442 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 443 | { |
| 444 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 445 | match *self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 446 | Element::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 447 | a.to_tokens(tokens); |
| 448 | b.to_tokens(tokens); |
| 449 | } |
| 450 | Element::End(ref a) => a.to_tokens(tokens), |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |