diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 0ff9be8..1269b8a 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -124,42 +124,3 @@ impl<'a> TryFrom> for Postfix { }) } } - -impl<'a> TryFrom> for Pattern { - type Error = ParseError<'a>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let rule = pair.as_rule(); - let mut inner = pair.clone().into_inner(); - - Ok(match rule { - Rule::tuple_pattern => Pattern::Tuple( - inner - .map(Identifier::try_from) - .collect::>>()?, - ), - - Rule::named_tuple_pattern => Pattern::NamedTuple( - Path::try_from(inner.next().unwrap())?, - inner - .map(Identifier::try_from) - .collect::>>()?, - ), - - Rule::struct_pattern => Pattern::Struct( - Path::try_from(inner.next().unwrap())?, - inner - .map(Identifier::try_from) - .collect::>>()?, - ), - - Rule::literal => Pattern::Literal(Literal::try_from(pair)?), - - Rule::identifier => Pattern::Id(Identifier::try_from(pair)?), - - Rule::static_path => Pattern::Path(Path::try_from(pair)?), - - _ => unimplemented!("{rule:?}"), - }) - } -} diff --git a/parser/src/parser/common/mod.rs b/parser/src/parser/common/mod.rs index 1f1169f..9504afd 100644 --- a/parser/src/parser/common/mod.rs +++ b/parser/src/parser/common/mod.rs @@ -57,6 +57,45 @@ impl<'a> TryFrom> for Literal { } } +impl<'a> TryFrom> for Pattern { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + let rule = pair.as_rule(); + let mut inner = pair.clone().into_inner(); + + Ok(match rule { + Rule::tuple_pattern => Pattern::Tuple( + inner + .map(Identifier::try_from) + .collect::>>()?, + ), + + Rule::named_tuple_pattern => Pattern::NamedTuple( + Path::try_from(inner.next().unwrap())?, + inner + .map(Identifier::try_from) + .collect::>>()?, + ), + + Rule::struct_pattern => Pattern::Struct( + Path::try_from(inner.next().unwrap())?, + inner + .map(Identifier::try_from) + .collect::>>()?, + ), + + Rule::literal => Pattern::Literal(Literal::try_from(pair)?), + + Rule::identifier => Pattern::Id(Identifier::try_from(pair)?), + + Rule::static_path => Pattern::Path(Path::try_from(pair)?), + + _ => unimplemented!("{rule:?}"), + }) + } +} + impl<'a> TryFrom<&mut pest::iterators::Pairs<'a, Rule>> for Visibility { type Error = ParseError<'a>;