1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::ast;
use crate::error::{ParseError, Result};
use crate::parser::Parser;
use crate::traits::Parse;
use runestick::unit::Span;

/// A tuple pattern.
#[derive(Debug, Clone)]
pub struct PatTuple {
    /// The path, if the tuple is typed.
    pub path: Option<ast::Path>,
    /// The open bracket.
    pub open: ast::OpenParen,
    /// The numbers matched against.
    pub items: Vec<(Box<ast::Pat>, Option<ast::Comma>)>,
    /// Indicates if the pattern is open or not.
    pub open_pattern: Option<ast::DotDot>,
    /// The close bracket.
    pub close: ast::CloseParen,
}

impl PatTuple {
    /// Get the span of the pattern.
    pub fn span(&self) -> Span {
        if let Some(path) = &self.path {
            path.span().join(self.close.span())
        } else {
            self.open.span().join(self.close.span())
        }
    }

    /// Parse a tuple pattern with a known preceeding path.
    pub fn parse_with_path(
        parser: &mut Parser<'_>,
        path: Option<ast::Path>,
    ) -> Result<Self, ParseError> {
        let open = parser.parse()?;
        let mut items = Vec::new();

        let mut is_open = true;

        while !parser.peek::<ast::CloseParen>()? && !parser.peek::<ast::DotDot>()? {
            let pat = parser.parse()?;

            is_open = parser.peek::<ast::Comma>()?;

            if !is_open {
                items.push((Box::new(pat), None));
                break;
            }

            items.push((Box::new(pat), Some(parser.parse()?)));
        }

        let open_pattern = if is_open && parser.peek::<ast::DotDot>()? {
            Some(parser.parse()?)
        } else {
            None
        };

        let close = parser.parse()?;

        Ok(Self {
            path,
            open,
            items,
            open_pattern,
            close,
        })
    }
}

impl Parse for PatTuple {
    fn parse(parser: &mut Parser) -> Result<Self, ParseError> {
        let path = if parser.peek::<ast::Path>()? {
            Some(parser.parse()?)
        } else {
            None
        };

        Self::parse_with_path(parser, path)
    }
}