David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 1 | use {AngleBracketedParameterData, Generics, Ident, Lifetime, ParenthesizedParameterData, Path, |
| 2 | PathParameters, PathSegment, Ty, TypeBinding}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 3 | use aster::ident::ToIdent; |
| 4 | use aster::invoke::{Invoke, Identity}; |
| 5 | use aster::lifetime::IntoLifetime; |
| 6 | use aster::ty::TyBuilder; |
| 7 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 8 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 9 | |
| 10 | pub trait IntoPath { |
| 11 | fn into_path(self) -> Path; |
| 12 | } |
| 13 | |
| 14 | impl IntoPath for Path { |
| 15 | fn into_path(self) -> Path { |
| 16 | self |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl IntoPath for Ident { |
| 21 | fn into_path(self) -> Path { |
| 22 | PathBuilder::new().id(self).build() |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<'a> IntoPath for &'a str { |
| 27 | fn into_path(self) -> Path { |
| 28 | PathBuilder::new().id(self).build() |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl IntoPath for String { |
| 33 | fn into_path(self) -> Path { |
| 34 | (&*self).into_path() |
| 35 | } |
| 36 | } |
| 37 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 38 | impl<'a, T> IntoPath for &'a [T] |
| 39 | where T: ToIdent |
| 40 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 41 | fn into_path(self) -> Path { |
| 42 | PathBuilder::new().ids(self).build() |
| 43 | } |
| 44 | } |
| 45 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 46 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 47 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 48 | pub struct PathBuilder<F = Identity> { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 49 | callback: F, |
| 50 | global: bool, |
| 51 | } |
| 52 | |
| 53 | impl PathBuilder { |
| 54 | pub fn new() -> Self { |
| 55 | PathBuilder::with_callback(Identity) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<F> PathBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 60 | where F: Invoke<Path> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 61 | { |
| 62 | pub fn with_callback(callback: F) -> Self { |
| 63 | PathBuilder { |
| 64 | callback: callback, |
| 65 | global: false, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn build(self, path: Path) -> F::Result { |
| 70 | self.callback.invoke(path) |
| 71 | } |
| 72 | |
| 73 | pub fn global(mut self) -> Self { |
| 74 | self.global = true; |
| 75 | self |
| 76 | } |
| 77 | |
| 78 | pub fn ids<I, T>(self, ids: I) -> PathSegmentsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 79 | where I: IntoIterator<Item = T>, |
| 80 | T: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 81 | { |
| 82 | let mut ids = ids.into_iter(); |
| 83 | let id = ids.next().expect("passed path with no id"); |
| 84 | |
| 85 | self.id(id).ids(ids) |
| 86 | } |
| 87 | |
| 88 | pub fn id<I>(self, id: I) -> PathSegmentsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 89 | where I: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 90 | { |
| 91 | self.segment(id).build() |
| 92 | } |
| 93 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 94 | pub fn segment<I>(self, id: I) -> PathSegmentBuilder<PathSegmentsBuilder<F>> |
| 95 | where I: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 96 | { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 97 | PathSegmentBuilder::with_callback(id, |
| 98 | PathSegmentsBuilder { |
| 99 | callback: self.callback, |
| 100 | global: self.global, |
| 101 | segments: Vec::new(), |
| 102 | }) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 106 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 107 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 108 | pub struct PathSegmentsBuilder<F = Identity> { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 109 | callback: F, |
| 110 | global: bool, |
| 111 | segments: Vec<PathSegment>, |
| 112 | } |
| 113 | |
| 114 | impl<F> PathSegmentsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 115 | where F: Invoke<Path> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 116 | { |
| 117 | pub fn ids<I, T>(mut self, ids: I) -> PathSegmentsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 118 | where I: IntoIterator<Item = T>, |
| 119 | T: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 120 | { |
| 121 | for id in ids { |
| 122 | self = self.id(id); |
| 123 | } |
| 124 | |
| 125 | self |
| 126 | } |
| 127 | |
| 128 | pub fn id<T>(self, id: T) -> PathSegmentsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 129 | where T: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 130 | { |
| 131 | self.segment(id).build() |
| 132 | } |
| 133 | |
| 134 | pub fn segment<T>(self, id: T) -> PathSegmentBuilder<Self> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 135 | where T: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 136 | { |
| 137 | PathSegmentBuilder::with_callback(id, self) |
| 138 | } |
| 139 | |
| 140 | pub fn build(self) -> F::Result { |
| 141 | self.callback.invoke(Path { |
| 142 | global: self.global, |
| 143 | segments: self.segments, |
| 144 | }) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<F> Invoke<PathSegment> for PathSegmentsBuilder<F> { |
| 149 | type Result = Self; |
| 150 | |
| 151 | fn invoke(mut self, segment: PathSegment) -> Self { |
| 152 | self.segments.push(segment); |
| 153 | self |
| 154 | } |
| 155 | } |
| 156 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 157 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 158 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 159 | pub struct PathSegmentBuilder<F = Identity> { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 160 | callback: F, |
| 161 | id: Ident, |
| 162 | lifetimes: Vec<Lifetime>, |
| 163 | tys: Vec<Ty>, |
| 164 | bindings: Vec<TypeBinding>, |
| 165 | } |
| 166 | |
| 167 | impl<F> PathSegmentBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 168 | where F: Invoke<PathSegment> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 169 | { |
| 170 | pub fn with_callback<I>(id: I, callback: F) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 171 | where I: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 172 | { |
| 173 | PathSegmentBuilder { |
| 174 | callback: callback, |
| 175 | id: id.to_ident(), |
| 176 | lifetimes: Vec::new(), |
| 177 | tys: Vec::new(), |
| 178 | bindings: Vec::new(), |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | pub fn with_generics(self, generics: Generics) -> Self { |
| 183 | // Strip off the bounds. |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 184 | let lifetimes = generics.lifetimes |
| 185 | .iter() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 186 | .map(|lifetime_def| lifetime_def.lifetime.clone()); |
| 187 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 188 | let tys = generics.ty_params |
| 189 | .iter() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 190 | .map(|ty_param| TyBuilder::new().id(ty_param.ident.clone())); |
| 191 | |
| 192 | self.with_lifetimes(lifetimes) |
| 193 | .with_tys(tys) |
| 194 | } |
| 195 | |
| 196 | pub fn with_lifetimes<I, L>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 197 | where I: IntoIterator<Item = L>, |
| 198 | L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 199 | { |
| 200 | let iter = iter.into_iter().map(|lifetime| lifetime.into_lifetime()); |
| 201 | self.lifetimes.extend(iter); |
| 202 | self |
| 203 | } |
| 204 | |
| 205 | pub fn with_lifetime<L>(mut self, lifetime: L) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 206 | where L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 207 | { |
| 208 | self.lifetimes.push(lifetime.into_lifetime()); |
| 209 | self |
| 210 | } |
| 211 | |
| 212 | pub fn lifetime<N>(self, name: N) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 213 | where N: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 214 | { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 215 | let lifetime = Lifetime { ident: name.to_ident() }; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 216 | self.with_lifetime(lifetime) |
| 217 | } |
| 218 | |
| 219 | pub fn with_tys<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 220 | where I: IntoIterator<Item = Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 221 | { |
| 222 | self.tys.extend(iter); |
| 223 | self |
| 224 | } |
| 225 | |
| 226 | pub fn with_ty(mut self, ty: Ty) -> Self { |
| 227 | self.tys.push(ty); |
| 228 | self |
| 229 | } |
| 230 | |
| 231 | pub fn ty(self) -> TyBuilder<Self> { |
| 232 | TyBuilder::with_callback(self) |
| 233 | } |
| 234 | |
| 235 | pub fn with_binding(mut self, binding: TypeBinding) -> Self { |
| 236 | self.bindings.push(binding); |
| 237 | self |
| 238 | } |
| 239 | |
| 240 | pub fn binding<T>(self, id: T) -> TyBuilder<TypeBindingBuilder<F>> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 241 | where T: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 242 | { |
| 243 | TyBuilder::with_callback(TypeBindingBuilder { |
| 244 | id: id.to_ident(), |
| 245 | builder: self, |
| 246 | }) |
| 247 | } |
| 248 | |
| 249 | pub fn no_return(self) -> F::Result { |
| 250 | self.build_return(None) |
| 251 | } |
| 252 | |
| 253 | pub fn return_(self) -> TyBuilder<PathSegmentReturnBuilder<F>> { |
| 254 | TyBuilder::with_callback(PathSegmentReturnBuilder(self)) |
| 255 | } |
| 256 | |
| 257 | pub fn build_return(self, output: Option<Ty>) -> F::Result { |
| 258 | let data = ParenthesizedParameterData { |
| 259 | inputs: self.tys, |
| 260 | output: output, |
| 261 | }; |
| 262 | |
| 263 | let parameters = PathParameters::Parenthesized(data); |
| 264 | |
| 265 | self.callback.invoke(PathSegment { |
| 266 | ident: self.id, |
| 267 | parameters: parameters, |
| 268 | }) |
| 269 | } |
| 270 | |
| 271 | pub fn build(self) -> F::Result { |
| 272 | let data = AngleBracketedParameterData { |
| 273 | lifetimes: self.lifetimes, |
| 274 | types: self.tys, |
| 275 | bindings: self.bindings, |
| 276 | }; |
| 277 | |
| 278 | let parameters = PathParameters::AngleBracketed(data); |
| 279 | |
| 280 | self.callback.invoke(PathSegment { |
| 281 | ident: self.id, |
| 282 | parameters: parameters, |
| 283 | }) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | impl<F> Invoke<Ty> for PathSegmentBuilder<F> |
| 288 | where F: Invoke<PathSegment> |
| 289 | { |
| 290 | type Result = Self; |
| 291 | |
| 292 | fn invoke(self, ty: Ty) -> Self { |
| 293 | self.with_ty(ty) |
| 294 | } |
| 295 | } |
| 296 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 297 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 298 | |
| 299 | pub struct TypeBindingBuilder<F> { |
| 300 | id: Ident, |
| 301 | builder: PathSegmentBuilder<F>, |
| 302 | } |
| 303 | |
| 304 | impl<F> Invoke<Ty> for TypeBindingBuilder<F> |
| 305 | where F: Invoke<PathSegment> |
| 306 | { |
| 307 | type Result = PathSegmentBuilder<F>; |
| 308 | |
| 309 | fn invoke(self, ty: Ty) -> Self::Result { |
| 310 | let id = self.id; |
| 311 | |
| 312 | self.builder.with_binding(TypeBinding { |
| 313 | ident: id, |
| 314 | ty: ty, |
| 315 | }) |
| 316 | } |
| 317 | } |
| 318 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame^] | 319 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 320 | |
| 321 | pub struct PathSegmentReturnBuilder<F>(PathSegmentBuilder<F>); |
| 322 | |
| 323 | impl<F> Invoke<Ty> for PathSegmentReturnBuilder<F> |
| 324 | where F: Invoke<PathSegment> |
| 325 | { |
| 326 | type Result = F::Result; |
| 327 | |
| 328 | fn invoke(self, ty: Ty) -> Self::Result { |
| 329 | self.0.build_return(Some(ty)) |
| 330 | } |
| 331 | } |