1use crate::priv_prelude::*;
2
3#[allow(clippy::large_enum_variant)]
4#[derive(Clone, Debug, Serialize)]
5pub enum Ty {
6 Path(PathType),
7 Tuple(Parens<TyTupleDescriptor>),
8 Array(SquareBrackets<TyArrayDescriptor>),
9 StringSlice(StrToken),
10 StringArray {
11 str_token: StrToken,
12 length: SquareBrackets<Box<Expr>>,
13 },
14 Infer {
15 underscore_token: UnderscoreToken,
16 },
17 Ptr {
18 ptr_token: PtrToken,
19 ty: SquareBrackets<Box<Ty>>,
20 },
21 Slice {
22 slice_token: Option<SliceToken>,
23 ty: SquareBrackets<Box<Ty>>,
24 },
25 Ref {
26 ampersand_token: AmpersandToken,
27 mut_token: Option<MutToken>,
28 ty: Box<Ty>,
29 },
30 Never {
31 bang_token: BangToken,
32 },
33 Expr(Box<Expr>),
34}
35
36impl Spanned for Ty {
37 fn span(&self) -> Span {
38 match self {
39 Ty::Path(path_type) => path_type.span(),
40 Ty::Tuple(tuple_type) => tuple_type.span(),
41 Ty::Array(array_type) => array_type.span(),
42 Ty::StringSlice(str_token) => str_token.span(),
43 Ty::StringArray { str_token, length } => Span::join(str_token.span(), &length.span()),
44 Ty::Infer { underscore_token } => underscore_token.span(),
45 Ty::Ptr { ptr_token, ty } => Span::join(ptr_token.span(), &ty.span()),
46 Ty::Slice { slice_token, ty } => {
47 let span = slice_token
48 .as_ref()
49 .map(|s| Span::join(s.span(), &ty.span()));
50 span.unwrap_or_else(|| ty.span())
51 }
52 Ty::Ref {
53 ampersand_token,
54 mut_token: _,
55 ty,
56 } => Span::join(ampersand_token.span(), &ty.span()),
57 Ty::Never { bang_token } => bang_token.span(),
58 Ty::Expr(expr) => expr.span(),
59 }
60 }
61}
62
63impl Ty {
64 pub fn name_span(&self) -> Option<Span> {
65 if let Ty::Path(path_type) = self {
66 Some(path_type.last_segment().name.span())
67 } else {
68 None
69 }
70 }
71}
72
73#[derive(Clone, Debug, Serialize)]
74pub enum TyTupleDescriptor {
75 Nil,
76 Cons {
77 head: Box<Ty>,
78 comma_token: CommaToken,
79 tail: Punctuated<Ty, CommaToken>,
80 },
81}
82
83impl TyTupleDescriptor {
84 pub fn to_tys(self) -> Vec<Ty> {
85 match self {
86 TyTupleDescriptor::Nil => vec![],
87 TyTupleDescriptor::Cons { head, tail, .. } => {
88 let mut tys = vec![*head];
89 for ty in tail.into_iter() {
90 tys.push(ty);
91 }
92 tys
93 }
94 }
95 }
96}
97
98#[derive(Clone, Debug, Serialize)]
99pub struct TyArrayDescriptor {
100 pub ty: Box<Ty>,
101 pub semicolon_token: SemicolonToken,
102 pub length: Box<Expr>,
103}