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