Skip to content

Commit 2feabc4

Browse files
committed
Auto merge of #14622 - alibektas:make_ty_alias, r=alibektas
Add syntax::make::ty_alias There was until now no function that returns TypeAlias. This commit introduces a func that is fully compliant with the Rust Reference. I had problems working with Ident so for now the function uses simple string manipulation until ast_from_text function is called. I am however open to any ideas that could replace ident param in such a way that it accepts syntax::ast::Ident
2 parents 5750d81 + e275f77 commit 2feabc4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

crates/syntax/src/ast/make.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,47 @@ fn ty_from_text(text: &str) -> ast::Type {
158158
ast_from_text(&format!("type _T = {text};"))
159159
}
160160

161+
/// Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html)
162+
/// Type Alias syntax is
163+
/// ```
164+
/// TypeAlias :
165+
/// type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ;
166+
/// ```
167+
/// FIXME : ident should be of type ast::Ident
168+
pub fn ty_alias(
169+
ident: &str,
170+
generic_param_list: Option<ast::GenericParamList>,
171+
type_param_bounds: Option<ast::TypeParam>,
172+
where_clause: Option<ast::WhereClause>,
173+
assignment: Option<(ast::Type, Option<ast::WhereClause>)>,
174+
) -> ast::TypeAlias {
175+
let mut s = String::new();
176+
s.push_str(&format!("type {}", ident));
177+
178+
if let Some(list) = generic_param_list {
179+
s.push_str(&list.to_string());
180+
}
181+
182+
if let Some(list) = type_param_bounds {
183+
s.push_str(&format!(" : {}", &list));
184+
}
185+
186+
if let Some(cl) = where_clause {
187+
s.push_str(&format!(" {}", &cl.to_string()));
188+
}
189+
190+
if let Some(exp) = assignment {
191+
if let Some(cl) = exp.1 {
192+
s.push_str(&format!("= {} {}", &exp.0.to_string(), &cl.to_string()));
193+
} else {
194+
s.push_str(&format!("= {}", &exp.0.to_string()));
195+
}
196+
}
197+
198+
s.push_str(";");
199+
ast_from_text(&s)
200+
}
201+
161202
pub fn assoc_item_list() -> ast::AssocItemList {
162203
ast_from_text("impl C for D {}")
163204
}

0 commit comments

Comments
 (0)