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}
34
35impl Spanned for Ty {
36 fn span(&self) -> Span {
37 match self {
38 Ty::Path(path_type) => path_type.span(),
39 Ty::Tuple(tuple_type) => tuple_type.span(),
40 Ty::Array(array_type) => array_type.span(),
41 Ty::StringSlice(str_token) => str_token.span(),
42 Ty::StringArray { str_token, length } => Span::join(str_token.span(), &length.span()),
43 Ty::Infer { underscore_token } => underscore_token.span(),
44 Ty::Ptr { ptr_token, ty } => Span::join(ptr_token.span(), &ty.span()),
45 Ty::Slice { slice_token, ty } => {
46 let span = slice_token
47 .as_ref()
48 .map(|s| Span::join(s.span(), &ty.span()));
49 span.unwrap_or_else(|| ty.span())
50 }
51 Ty::Ref {
52 ampersand_token,
53 mut_token: _,
54 ty,
55 } => Span::join(ampersand_token.span(), &ty.span()),
56 Ty::Never { bang_token } => bang_token.span(),
57 }
58 }
59}
60
61impl Ty {
62 pub fn name_span(&self) -> Option<Span> {
63 if let Ty::Path(path_type) = self {
64 Some(path_type.last_segment().name.span())
65 } else {
66 None
67 }
68 }
69}
70
71#[derive(Clone, Debug, Serialize)]
72pub enum TyTupleDescriptor {
73 Nil,
74 Cons {
75 head: Box<Ty>,
76 comma_token: CommaToken,
77 tail: Punctuated<Ty, CommaToken>,
78 },
79}
80
81impl TyTupleDescriptor {
82 pub fn to_tys(self) -> Vec<Ty> {
83 match self {
84 TyTupleDescriptor::Nil => vec![],
85 TyTupleDescriptor::Cons { head, tail, .. } => {
86 let mut tys = vec![*head];
87 for ty in tail.into_iter() {
88 tys.push(ty);
89 }
90 tys
91 }
92 }
93 }
94}
95
96#[derive(Clone, Debug, Serialize)]
97pub struct TyArrayDescriptor {
98 pub ty: Box<Ty>,
99 pub semicolon_token: SemicolonToken,
100 pub length: Box<Expr>,
101}