types error handling

This commit is contained in:
2026-05-15 08:54:48 +02:00
parent 527e450f9e
commit f9c5fb4ec8
+29 -37
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult}, error::{AstError, GetParseError, collect_recovered},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -17,7 +17,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypePostfix {
let mutable = listen_rule(&mut inner, Rule::mutable); let mutable = listen_rule(&mut inner, Rule::mutable);
let lifetime = consume_rule(&mut inner, Rule::lifetime) let lifetime = consume_rule(&mut inner, Rule::lifetime)
.map(|pair| Identifier::try_from(pair.into_inner().next().unwrap())) .map(|pair| Identifier::try_from(pair.into_inner().next().unwrap()))
.transpose()?; .transpose()
.get()?;
Ok(if mutable { Ok(if mutable {
if let Some(lifetime) = lifetime { if let Some(lifetime) = lifetime {
@@ -46,16 +47,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
match rule { match rule {
Rule::tuple_type => Ok(TypeExprKind::Tuple( Rule::tuple_type => Ok(TypeExprKind::Tuple(collect_recovered(inner).get()?)),
inner
.map(TypeExpr::try_from)
.collect::<AstResult<'a, _>>()?,
)),
Rule::path_type => { Rule::path_type => {
let path = Path::try_from(inner.next().unwrap())?; let path = Path::try_from(inner.next().unwrap()).get()?;
let params = inner let params = collect_recovered(inner).get()?;
.map(TypeExpr::try_from)
.collect::<AstResult<'a, Vec<_>>>()?;
if params.len() == 0 { if params.len() == 0 {
Ok(TypeExprKind::Path(path)) Ok(TypeExprKind::Path(path))
@@ -77,14 +72,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
match rule { match rule {
Rule::type_expr => Ok(TypeExpr( Rule::type_expr => Ok(TypeExpr(
TypeExprKind::try_from(inner.next().unwrap())?, inner.next().unwrap().try_into().get()?,
inner collect_recovered(inner).get()?,
.map(TypePostfix::try_from)
.collect::<AstResult<'a, _>>()?,
)), )),
Rule::type_expr_param => Self::try_from(inner.next().unwrap()), Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
Rule::lifetime => Ok(TypeExpr( Rule::lifetime => Ok(TypeExpr(
TypeExprKind::Lifetime(Identifier::try_from(inner.next().unwrap())?), TypeExprKind::Lifetime(inner.next().unwrap().try_into().get()?),
Vec::new(), Vec::new(),
)), )),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
@@ -100,28 +93,27 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
let inner = pair.clone().into_inner(); let inner = pair.clone().into_inner();
match rule { match rule {
Rule::generics => Ok(Generics( Rule::generics => Ok(Generics(collect_recovered(inner).get()?)),
inner
.map(|pair| -> AstResult<'a, Generic> {
let mut inner = pair.into_inner();
Ok(
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
Generic::Lifetime(Identifier::try_from(
pair.into_inner().next().unwrap(),
)?)
} else {
Generic::Type(
Identifier::try_from(inner.next().unwrap())?,
inner
.map(TypeExpr::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
)
},
)
})
.collect::<AstResult<'a, Vec<_>>>()?,
)),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
Ok(Generic::Lifetime(
pair.into_inner().next().unwrap().try_into().get()?,
))
} else {
Ok(Generic::Type(
inner.next().unwrap().try_into().get()?,
collect_recovered(inner).get()?,
))
}
}
}