summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/src/ast/pat.rs
blob: c85f625ab1092489055252d310e38b79de40f89c (plain)
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
use super::*;

#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)]
pub enum Pat<'a> {
	Paren(Box<Pat<'a>>),
	Ident(Ident<'a>),
}

impl<'a> Pat<'a> {
	pub fn validate(&self) -> Result<(), ValidationError> {
		match self {
			Pat::Paren(pat) => pat.validate(),
			Pat::Ident(_) => Ok(()),
		}
	}
}

#[derive(Debug, Clone, PartialEq, Eq, IntoOwned, Borrowed)]
pub enum DestrPat<'a> {
	Index {
		base: Box<DestrPat<'a>>,
		index: Box<Expr<'a>>,
	},
	Field {
		base: Box<DestrPat<'a>>,
		field: Ident<'a>,
	},
	Paren(Box<DestrPat<'a>>),
	Path(Path<'a>),
}

impl<'a> DestrPat<'a> {
	pub fn validate(&self) -> Result<(), ValidationError> {
		match self {
			DestrPat::Index { base, index } => {
				base.validate()?;
				index.validate()?;
				Ok(())
			}
			DestrPat::Field { base, field: _ } => base.validate(),
			DestrPat::Paren(pat) => pat.validate(),
			DestrPat::Path(_) => Ok(()),
		}
	}
}

impl<'a> From<Pat<'a>> for DestrPat<'a> {
	fn from(value: Pat<'a>) -> Self {
		match value {
			Pat::Paren(pat) => DestrPat::Paren(Box::new((*pat).into())),
			Pat::Ident(ident) => DestrPat::Path(Path {
				root: PathRoot::Relative,
				components: vec![ident],
			}),
		}
	}
}