refactor(xatu): move binding filters into rules

This commit is contained in:
2026-09-19 20:41:14 -04:00
parent e949e526c7
commit ab68fa3b05
4 changed files with 57 additions and 47 deletions

View File

@@ -33,14 +33,21 @@ pub enum NodeData {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Binding { pub struct Binding {
pub name: Ident, pub path: Vec<Attr>,
pub value: Expr, pub value: Expr,
} }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Attr {
Ident(Ident),
Other,
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr { pub enum Expr {
Ident(Ident), Ident(Ident),
Select(Box<Select>), Select(Box<Select>),
Other,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -51,7 +58,8 @@ pub struct Ident {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Select { pub struct Select {
pub receiver: Expr, pub receiver: Expr,
pub attrs: Vec<Ident>, pub path: Vec<Attr>,
pub has_default: bool,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]

View File

@@ -44,7 +44,7 @@ fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> { fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> {
let binding = rnix::ast::AttrpathValue::cast(node)?; let binding = rnix::ast::AttrpathValue::cast(node)?;
let range = binding.syntax().text_range().into(); let range = binding.syntax().text_range().into();
let data = binding.try_into().ok()?; let data = binding.into();
Some(ast::Node { Some(ast::Node {
range, range,
@@ -52,38 +52,35 @@ fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> {
}) })
} }
impl TryFrom<rnix::ast::AttrpathValue> for ast::Binding { impl From<rnix::ast::AttrpathValue> for ast::Binding {
type Error = (); fn from(binding: rnix::ast::AttrpathValue) -> Self {
Self {
fn try_from(binding: rnix::ast::AttrpathValue) -> Result<Self, Self::Error> { path: binding
let mut attrs = binding.attrpath().ok_or(())?.attrs(); .attrpath()
let rnix::ast::Attr::Ident(name) = attrs.next().ok_or(())? else { .into_iter()
return Err(()); .flat_map(|path| path.attrs())
}; .map(Into::into)
if attrs.next().is_some() { .collect(),
return Err(()); value: binding.value().map(Into::into).unwrap_or(ast::Expr::Other),
} }
let value = binding.value().ok_or(())?;
if matches!(&value, rnix::ast::Expr::Select(select) if select.or_token().is_some()) {
return Err(());
}
Ok(Self {
name: name.into(),
value: value.try_into()?,
})
} }
} }
impl TryFrom<rnix::ast::Expr> for ast::Expr { impl From<rnix::ast::Attr> for ast::Attr {
type Error = (); fn from(attr: rnix::ast::Attr) -> Self {
match attr {
rnix::ast::Attr::Ident(ident) => Self::Ident(ident.into()),
_ => Self::Other,
}
}
}
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> { impl From<rnix::ast::Expr> for ast::Expr {
fn from(expr: rnix::ast::Expr) -> Self {
match expr { match expr {
rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.into())), rnix::ast::Expr::Ident(ident) => Self::Ident(ident.into()),
rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))), rnix::ast::Expr::Select(select) => Self::Select(Box::new(select.into())),
_ => Err(()), _ => Self::Other,
} }
} }
} }
@@ -96,22 +93,18 @@ impl From<rnix::ast::Ident> for ast::Ident {
} }
} }
impl TryFrom<rnix::ast::Select> for ast::Select { impl From<rnix::ast::Select> for ast::Select {
type Error = (); fn from(select: rnix::ast::Select) -> Self {
Self {
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> { receiver: select.expr().map(Into::into).unwrap_or(ast::Expr::Other),
Ok(Self { path: select
receiver: select.expr().ok_or(())?.try_into()?,
attrs: select
.attrpath() .attrpath()
.ok_or(())? .into_iter()
.attrs() .flat_map(|path| path.attrs())
.map(|attr| match attr { .map(Into::into)
rnix::ast::Attr::Ident(ident) => Ok(ident.into()), .collect(),
_ => Err(()), has_default: select.or_token().is_some(),
}) }
.collect::<Result<Vec<_>, _>>()?,
})
} }
} }

View File

@@ -13,10 +13,13 @@ impl NodeRule for ManualInherit {
} }
fn check(binding: &Self::Node) -> Option<&'static str> { fn check(binding: &Self::Node) -> Option<&'static str> {
let [ast::Attr::Ident(name)] = binding.path.as_slice() else {
return None;
};
let ast::Expr::Ident(value) = &binding.value else { let ast::Expr::Ident(value) = &binding.value else {
return None; return None;
}; };
(binding.name == *value).then_some("Use `inherit` for this binding") (name == value).then_some("Use `inherit` for this binding")
} }
} }

View File

@@ -13,16 +13,22 @@ impl NodeRule for ManualInheritFrom {
} }
fn check(binding: &Self::Node) -> Option<&'static str> { fn check(binding: &Self::Node) -> Option<&'static str> {
let [ast::Attr::Ident(name)] = binding.path.as_slice() else {
return None;
};
let ast::Expr::Select(select) = &binding.value else { let ast::Expr::Select(select) = &binding.value else {
return None; return None;
}; };
let ast::Expr::Ident(_) = &select.receiver else { let ast::Expr::Ident(_) = &select.receiver else {
return None; return None;
}; };
let [selected] = select.attrs.as_slice() else { let [ast::Attr::Ident(selected)] = select.path.as_slice() else {
return None; return None;
}; };
if select.has_default {
return None;
}
(binding.name == *selected).then_some("Use `inherit (source)` for this binding") (name == selected).then_some("Use `inherit (source)` for this binding")
} }
} }