From 0b67331ca3e6cf30e080a0ef80222fa27ff476d3 Mon Sep 17 00:00:00 2001 From: alexion Date: Sat, 19 Sep 2026 10:34:47 -0400 Subject: [PATCH] refactor(xatu): clarify parser conversions --- src/xatu/parser.rs | 106 +++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 61 deletions(-) diff --git a/src/xatu/parser.rs b/src/xatu/parser.rs index 0c92745..4f8490b 100644 --- a/src/xatu/parser.rs +++ b/src/xatu/parser.rs @@ -61,10 +61,11 @@ fn convert_node(node: rnix::SyntaxNode) -> Option { fn convert_apply(node: rnix::SyntaxNode) -> Option { let apply = rnix::ast::Apply::cast(node)?; let range = apply.syntax().text_range().into(); + let data = convert_apply_data(apply)?; Some(ast::Node { range, - data: ast::NodeData::Apply(apply.try_into().ok()?), + data: ast::NodeData::Apply(data), }) } @@ -73,10 +74,52 @@ fn convert_inherit(node: rnix::SyntaxNode) -> Option { Some(ast::Node { range: inherit.syntax().text_range().into(), - data: ast::NodeData::Inherit(inherit.into()), + data: ast::NodeData::Inherit(convert_inherit_data(inherit)), }) } +fn convert_apply_data(apply: rnix::ast::Apply) -> Option { + Some(ast::Apply { + function: convert_expr(apply.lambda()?)?, + }) +} + +fn convert_expr(expr: rnix::ast::Expr) -> Option { + match expr { + rnix::ast::Expr::Ident(ident) => Some(ast::Expr::Ident(convert_ident(ident)?)), + rnix::ast::Expr::Select(select) => { + Some(ast::Expr::Select(Box::new(convert_select(select)?))) + } + _ => None, + } +} + +fn convert_ident(ident: rnix::ast::Ident) -> Option { + Some(ast::Ident { + name: ident.ident_token()?.text().to_owned(), + }) +} + +fn convert_select(select: rnix::ast::Select) -> Option { + Some(ast::Select { + receiver: convert_expr(select.expr()?)?, + attrs: select + .attrpath()? + .attrs() + .map(|attr| match attr { + rnix::ast::Attr::Ident(ident) => convert_ident(ident), + _ => None, + }) + .collect::>>()?, + }) +} + +fn convert_inherit_data(inherit: rnix::ast::Inherit) -> ast::Inherit { + ast::Inherit { + attr_count: inherit.attrs().count(), + } +} + impl From for ast::SourceRange { fn from(range: rnix::TextRange) -> Self { Self { @@ -85,62 +128,3 @@ impl From for ast::SourceRange { } } } - -impl TryFrom for ast::Apply { - type Error = (); - - fn try_from(apply: rnix::ast::Apply) -> Result { - Ok(Self { - function: apply.lambda().ok_or(())?.try_into()?, - }) - } -} - -impl TryFrom for ast::Expr { - type Error = (); - - fn try_from(expr: rnix::ast::Expr) -> Result { - match expr { - rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.try_into()?)), - rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))), - _ => Err(()), - } - } -} - -impl TryFrom for ast::Ident { - type Error = (); - - fn try_from(ident: rnix::ast::Ident) -> Result { - Ok(Self { - name: ident.ident_token().ok_or(())?.text().to_owned(), - }) - } -} - -impl TryFrom for ast::Select { - type Error = (); - - fn try_from(select: rnix::ast::Select) -> Result { - Ok(Self { - receiver: select.expr().ok_or(())?.try_into()?, - attrs: select - .attrpath() - .ok_or(())? - .attrs() - .map(|attr| match attr { - rnix::ast::Attr::Ident(ident) => ident.try_into(), - _ => Err(()), - }) - .collect::, _>>()?, - }) - } -} - -impl From for ast::Inherit { - fn from(inherit: rnix::ast::Inherit) -> Self { - Self { - attr_count: inherit.attrs().count(), - } - } -}