diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 77e9a45..2e844f8 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -41,28 +41,32 @@ impl From> for ParseError<'_> { } } -impl From> for TypeExpr { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for TypeExpr { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let rule = pair.as_rule(); let mut inner = pair.into_inner(); match rule { - Rule::type_expr => TypeExpr( + Rule::type_expr => Ok(TypeExpr( TypeExprKind::from(inner.next().unwrap()), inner.map(TypePostfix::from).collect(), - ), - Rule::type_expr_param => TypeExpr::from(inner.next().unwrap()), - Rule::lifetime => TypeExpr( + )), + Rule::type_expr_param => Self::try_from(inner.next().unwrap()), + Rule::lifetime => Ok(TypeExpr( TypeExprKind::Lifetime(Identifier::from(inner.next().unwrap())), Vec::new(), - ), + )), _ => unimplemented!("{rule:#?}"), } } } -impl From> for TypePostfix { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for TypePostfix { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let rule = pair.as_rule(); let mut inner = pair.into_inner(); @@ -91,8 +95,10 @@ impl From> for TypePostfix { } } -impl From> for TypeExprKind { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for TypeExprKind { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let rule = pair.as_rule(); let mut inner = pair.into_inner(); @@ -113,8 +119,10 @@ impl From> for TypeExprKind { } } -impl From> for Path { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for Path { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::static_path => Path(pair.into_inner().map(Identifier::from).collect()), _ => unimplemented!("{pair:#?}"), @@ -122,14 +130,16 @@ impl From> for Path { } } -impl From> for ParamList { +impl<'a> TryFrom> for ParamList { fn from(pair: pest::iterators::Pair) -> Self { ParamList(pair.into_inner().map(VarDecl::from).collect()) } } -impl From> for Attribute { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for Attribute { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::attribute => { // unwrap #[ ... ] @@ -185,8 +195,10 @@ impl From> for Attribute { } } -impl From> for TopLevel { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for TopLevel { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let mut inner = pair.into_inner(); let attributes = inner @@ -206,8 +218,10 @@ impl From> for TopLevel { } } -impl From> for StatementBranch { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for StatementBranch { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let mut inner = pair.into_inner(); let condition = Expression::from(inner.next().unwrap()); @@ -220,8 +234,10 @@ impl From> for StatementBranch { } } -impl From> for ClassConstructor { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for ClassConstructor { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let mut inner = pair.into_inner(); let visibility = Visibility::from(&mut inner); @@ -243,8 +259,10 @@ impl From> for ClassConstructor { } } -impl From> for Generics { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for Generics { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let rule = pair.as_rule(); let inner = pair.clone().into_inner(); @@ -269,8 +287,10 @@ impl From> for Generics { } } -impl From> for TopLevelKind { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for TopLevelKind { + 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(); @@ -343,8 +363,10 @@ impl From> for TopLevelKind { } } -impl From> for ClassItem { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for ClassItem { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let rule = pair.as_rule(); match rule { @@ -357,8 +379,10 @@ impl From> for ClassItem { } } -impl From> for ImplDecl { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for ImplDecl { + 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(); @@ -386,8 +410,10 @@ impl From> for ImplDecl { } } -impl From> for EnumItem { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for EnumItem { + 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(); @@ -417,7 +443,7 @@ impl From> for EnumItem { } } -impl From> for Block { +impl<'a> TryFrom> for Block { fn from(pair: pest::iterators::Pair) -> Self { let statements = pair .into_inner() @@ -433,7 +459,7 @@ impl From> for Block { } } -impl From> for Statement { +impl<'a> TryFrom> for Statement { fn from(pair: pest::iterators::Pair) -> Self { let rule = pair.as_rule(); let mut inner = pair.clone().into_inner(); @@ -517,8 +543,10 @@ impl From> for Statement { } } -impl From> for Literal { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for Literal { + 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(); @@ -535,7 +563,7 @@ impl From> for Literal { } } -impl From> for Expression { +impl<'a> TryFrom> for Expression { fn from(pair: pest::iterators::Pair) -> Self { let rule = pair.as_rule(); let mut inner = pair.clone().into_inner(); @@ -567,7 +595,7 @@ impl From> for Expression { } } -impl From> for Prefix { +impl<'a> TryFrom> for Prefix { fn from(pair: pest::iterators::Pair) -> Self { match pair.as_rule() { Rule::prefix => Self::from(pair.into_inner().next().unwrap()), @@ -581,7 +609,7 @@ impl From> for Prefix { } } -impl From> for Postfix { +impl<'a> TryFrom> for Postfix { fn from(pair: pest::iterators::Pair) -> Self { let rule = pair.as_rule(); let mut inner = pair.into_inner(); @@ -638,8 +666,10 @@ impl From> for Postfix { } } -impl From> for VarDeclStmt { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for VarDeclStmt { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::var_decl_statement => { let mut inner = pair.into_inner(); @@ -656,8 +686,10 @@ impl From> for VarDeclStmt { } } -impl From> for FieldDeclStmt { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for FieldDeclStmt { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::class_field => { let mut inner = pair.into_inner(); @@ -674,8 +706,10 @@ impl From> for FieldDeclStmt { } } -impl From> for Pattern { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +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(); @@ -703,8 +737,10 @@ impl From> for Pattern { } } -impl From> for VarDecl { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for VarDecl { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::var_decl => { let mut inner = pair.into_inner(); @@ -732,8 +768,10 @@ impl From> for VarDecl { } } -impl From> for FieldDecl { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for FieldDecl { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { Rule::field => { let mut inner = pair.into_inner(); @@ -754,8 +792,10 @@ impl From> for FieldDecl { } } -impl From> for FunctionDecl { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for FunctionDecl { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let mut inner = pair.into_inner(); let visibility = Visibility::from(&mut inner); let return_type = TypeExpr::from(inner.next().unwrap()); @@ -855,8 +895,10 @@ pub fn consume_rule<'a>( if consumed { pairs.next() } else { None } } -impl From> for Identifier { - fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { +impl<'a> TryFrom> for Identifier { + type Error = ParseError<'a>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { Identifier(pair.as_str().to_string()) } }