refactor(xatu): use conversion traits in parser

This commit is contained in:
2026-09-19 10:36:57 -04:00
parent 0b67331ca3
commit d455dcfb46

View File

@@ -61,7 +61,7 @@ fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> { fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
let apply = rnix::ast::Apply::cast(node)?; let apply = rnix::ast::Apply::cast(node)?;
let range = apply.syntax().text_range().into(); let range = apply.syntax().text_range().into();
let data = convert_apply_data(apply)?; let data = apply.try_into().ok()?;
Some(ast::Node { Some(ast::Node {
range, range,
@@ -74,50 +74,67 @@ fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
Some(ast::Node { Some(ast::Node {
range: inherit.syntax().text_range().into(), range: inherit.syntax().text_range().into(),
data: ast::NodeData::Inherit(convert_inherit_data(inherit)), data: ast::NodeData::Inherit(inherit.into()),
}) })
} }
fn convert_apply_data(apply: rnix::ast::Apply) -> Option<ast::Apply> { impl TryFrom<rnix::ast::Apply> for ast::Apply {
Some(ast::Apply { type Error = ();
function: convert_expr(apply.lambda()?)?,
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
Ok(Self {
function: apply.lambda().ok_or(())?.try_into()?,
}) })
}
} }
fn convert_expr(expr: rnix::ast::Expr) -> Option<ast::Expr> { impl TryFrom<rnix::ast::Expr> for ast::Expr {
type Error = ();
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
match expr { match expr {
rnix::ast::Expr::Ident(ident) => Some(ast::Expr::Ident(convert_ident(ident)?)), rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.try_into()?)),
rnix::ast::Expr::Select(select) => { rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))),
Some(ast::Expr::Select(Box::new(convert_select(select)?))) _ => Err(()),
} }
_ => None,
} }
} }
fn convert_ident(ident: rnix::ast::Ident) -> Option<ast::Ident> { impl TryFrom<rnix::ast::Ident> for ast::Ident {
Some(ast::Ident { type Error = ();
name: ident.ident_token()?.text().to_owned(),
fn try_from(ident: rnix::ast::Ident) -> Result<Self, Self::Error> {
Ok(Self {
name: ident.ident_token().ok_or(())?.text().to_owned(),
}) })
}
} }
fn convert_select(select: rnix::ast::Select) -> Option<ast::Select> { impl TryFrom<rnix::ast::Select> for ast::Select {
Some(ast::Select { type Error = ();
receiver: convert_expr(select.expr()?)?,
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> {
Ok(Self {
receiver: select.expr().ok_or(())?.try_into()?,
attrs: select attrs: select
.attrpath()? .attrpath()
.ok_or(())?
.attrs() .attrs()
.map(|attr| match attr { .map(|attr| match attr {
rnix::ast::Attr::Ident(ident) => convert_ident(ident), rnix::ast::Attr::Ident(ident) => ident.try_into(),
_ => None, _ => Err(()),
}) })
.collect::<Option<Vec<_>>>()?, .collect::<Result<Vec<_>, _>>()?,
}) })
}
} }
fn convert_inherit_data(inherit: rnix::ast::Inherit) -> ast::Inherit { impl From<rnix::ast::Inherit> for ast::Inherit {
ast::Inherit { fn from(inherit: rnix::ast::Inherit) -> Self {
Self {
attr_count: inherit.attrs().count(), attr_count: inherit.attrs().count(),
} }
}
} }
impl From<rnix::TextRange> for ast::SourceRange { impl From<rnix::TextRange> for ast::SourceRange {