Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7c9e4e1

Browse files
committed
Add syntax::make::ty_alias
The function is fully compliant with the specifications from the Rust Reference.
1 parent a646439 commit 7c9e4e1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

crates/syntax/src/ast/make.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use stdx::{format_to, never};
1414

1515
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
1616

17+
use super::WhereClause;
18+
1719
/// While the parent module defines basic atomic "constructors", the `ext`
1820
/// module defines shortcuts for common things.
1921
///
@@ -158,6 +160,52 @@ fn ty_from_text(text: &str) -> ast::Type {
158160
ast_from_text(&format!("type _T = {text};"))
159161
}
160162

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

0 commit comments

Comments
 (0)