diff --git a/src/expr.rs b/src/expr.rs index 4ced7a374..51c8f9054 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -686,6 +686,12 @@ impl ExprBuilder } } + pub fn for_(self) -> PatBuilder> { + PatBuilder::with_callback(ExprForBuilder { + builder: self, + }) + } + pub fn if_(self) -> ExprBuilder> { let span = self.span; ExprBuilder::with_callback(ExprIfBuilder { @@ -1595,6 +1601,128 @@ impl Invoke> for ExprLoopBuilder ////////////////////////////////////////////////////////////////////////////// +pub struct ExprForBuilder { + builder: ExprBuilder, +} + +impl Invoke> for ExprForBuilder + where F: Invoke>, +{ + type Result = ExprBuilder>; + + fn invoke(self, pat: P) -> ExprBuilder> { + ExprBuilder::with_callback( + ExprForItBuilder { + builder: self.builder, + pat: pat + }) + } +} + +pub struct ExprForItBuilder { + builder: ExprBuilder, + pat: P, +} + +impl Invoke> for ExprForItBuilder + where F: Invoke>, +{ + type Result = ExprForBlockBuilder; + + fn invoke(self, it: P) -> ExprForBlockBuilder { + ExprForBlockBuilder { + span: self.builder.span, + builder: self.builder, + pat: self.pat, + it: it, + label: None, + } + } +} + +////////////////////////////////////////////////////////////////////////////// + +pub struct ExprForBlockBuilder { + builder: ExprBuilder, + pat: P, + it: P, + span: Span, + label: Option>, +} + +impl ExprForBlockBuilder { + pub fn span(mut self, span: Span) -> Self { + self.span = span; + self + } + + pub fn label(mut self, id: I) -> Self + where I: ToIdent, + { + self.label = Some(respan(self.span, id.to_ident())); + self + } + + pub fn pat(self) -> PatBuilder { + PatBuilder::with_callback(self) + } + + pub fn it(self) -> ExprBuilder { + ExprBuilder::with_callback(self) + } + + pub fn build_pat(mut self, pat: P) -> Self { + self.pat = pat; + self + } + + pub fn build_it(mut self, it: P) -> Self { + self.it = it; + self + } +} + +impl ExprForBlockBuilder + where F: Invoke>, +{ + pub fn block(self) -> BlockBuilder { + BlockBuilder::with_callback(self) + } + + pub fn build_block(self, block: P) -> F::Result { + self.builder.build_expr_kind(ast::ExprKind::ForLoop(self.pat, self.it, block, self.label)) + } +} + +impl Invoke> for ExprForBlockBuilder { + type Result = Self; + + fn invoke(self, pat: P) -> Self { + self.build_pat(pat) + } +} + +impl Invoke> for ExprForBlockBuilder { + type Result = Self; + + fn invoke(self, expr: P) -> Self { + self.build_it(expr) + } +} + + +impl Invoke> for ExprForBlockBuilder + where F: Invoke>, +{ + type Result = F::Result; + + fn invoke(self, block: P) -> F::Result { + self.build_block(block) + } +} + +////////////////////////////////////////////////////////////////////////////// + pub struct ExprIfBuilder { builder: ExprBuilder, }