From 6145234450cc3e8d79c7f4e2105e36ccd96ee3b4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 3 Nov 2020 20:54:44 +0100 Subject: Support struct variants in extract_struct_from_enum_variant --- crates/syntax/src/ast/make.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'crates/syntax/src/ast/make.rs') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 2cf436e7a..b1578820f 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -110,8 +110,16 @@ pub fn record_expr_field(name: ast::NameRef, expr: Option) -> ast::Re } } -pub fn record_field(name: ast::NameRef, ty: ast::Type) -> ast::RecordField { - ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty)) +pub fn record_field( + visibility: Option, + name: ast::Name, + ty: ast::Type, +) -> ast::RecordField { + let visibility = match visibility { + None => String::new(), + Some(it) => format!("{} ", it), + }; + ast_from_text(&format!("struct S {{ {}{}: {}, }}", visibility, name, ty)) } pub fn block_expr( @@ -360,6 +368,13 @@ pub fn tuple_field_list(fields: impl IntoIterator) -> as ast_from_text(&format!("struct f({});", fields)) } +pub fn record_field_list( + fields: impl IntoIterator, +) -> ast::RecordFieldList { + let fields = fields.into_iter().join(", "); + ast_from_text(&format!("struct f {{ {} }}", fields)) +} + pub fn tuple_field(visibility: Option, ty: ast::Type) -> ast::TupleField { let visibility = match visibility { None => String::new(), @@ -368,6 +383,14 @@ pub fn tuple_field(visibility: Option, ty: ast::Type) -> ast::T ast_from_text(&format!("struct f({}{});", visibility, ty)) } +pub fn variant(name: ast::Name, field_list: Option) -> ast::Variant { + let field_list = match field_list { + None => String::new(), + Some(it) => format!("{}", it), + }; + ast_from_text(&format!("enum f {{ {}{} }}", name, field_list)) +} + pub fn fn_( visibility: Option, fn_name: ast::Name, -- cgit v1.2.3