refactor(xatu): move binding filters into rules
This commit is contained in:
@@ -33,14 +33,21 @@ pub enum NodeData {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Binding {
|
||||
pub name: Ident,
|
||||
pub path: Vec<Attr>,
|
||||
pub value: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Attr {
|
||||
Ident(Ident),
|
||||
Other,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Expr {
|
||||
Ident(Ident),
|
||||
Select(Box<Select>),
|
||||
Other,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -51,7 +58,8 @@ pub struct Ident {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Select {
|
||||
pub receiver: Expr,
|
||||
pub attrs: Vec<Ident>,
|
||||
pub path: Vec<Attr>,
|
||||
pub has_default: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
@@ -44,7 +44,7 @@ fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let binding = rnix::ast::AttrpathValue::cast(node)?;
|
||||
let range = binding.syntax().text_range().into();
|
||||
let data = binding.try_into().ok()?;
|
||||
let data = binding.into();
|
||||
|
||||
Some(ast::Node {
|
||||
range,
|
||||
@@ -52,38 +52,35 @@ fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
})
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::AttrpathValue> for ast::Binding {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(binding: rnix::ast::AttrpathValue) -> Result<Self, Self::Error> {
|
||||
let mut attrs = binding.attrpath().ok_or(())?.attrs();
|
||||
let rnix::ast::Attr::Ident(name) = attrs.next().ok_or(())? else {
|
||||
return Err(());
|
||||
};
|
||||
if attrs.next().is_some() {
|
||||
return Err(());
|
||||
impl From<rnix::ast::AttrpathValue> for ast::Binding {
|
||||
fn from(binding: rnix::ast::AttrpathValue) -> Self {
|
||||
Self {
|
||||
path: binding
|
||||
.attrpath()
|
||||
.into_iter()
|
||||
.flat_map(|path| path.attrs())
|
||||
.map(Into::into)
|
||||
.collect(),
|
||||
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 {
|
||||
type Error = ();
|
||||
impl From<rnix::ast::Attr> for ast::Attr {
|
||||
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 {
|
||||
rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.into())),
|
||||
rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))),
|
||||
_ => Err(()),
|
||||
rnix::ast::Expr::Ident(ident) => Self::Ident(ident.into()),
|
||||
rnix::ast::Expr::Select(select) => Self::Select(Box::new(select.into())),
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,22 +93,18 @@ impl From<rnix::ast::Ident> for ast::Ident {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Select> for ast::Select {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
receiver: select.expr().ok_or(())?.try_into()?,
|
||||
attrs: select
|
||||
impl From<rnix::ast::Select> for ast::Select {
|
||||
fn from(select: rnix::ast::Select) -> Self {
|
||||
Self {
|
||||
receiver: select.expr().map(Into::into).unwrap_or(ast::Expr::Other),
|
||||
path: select
|
||||
.attrpath()
|
||||
.ok_or(())?
|
||||
.attrs()
|
||||
.map(|attr| match attr {
|
||||
rnix::ast::Attr::Ident(ident) => Ok(ident.into()),
|
||||
_ => Err(()),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
.into_iter()
|
||||
.flat_map(|path| path.attrs())
|
||||
.map(Into::into)
|
||||
.collect(),
|
||||
has_default: select.or_token().is_some(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,13 @@ impl NodeRule for ManualInherit {
|
||||
}
|
||||
|
||||
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 {
|
||||
return None;
|
||||
};
|
||||
|
||||
(binding.name == *value).then_some("Use `inherit` for this binding")
|
||||
(name == value).then_some("Use `inherit` for this binding")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,16 +13,22 @@ impl NodeRule for ManualInheritFrom {
|
||||
}
|
||||
|
||||
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 {
|
||||
return None;
|
||||
};
|
||||
let ast::Expr::Ident(_) = &select.receiver else {
|
||||
return None;
|
||||
};
|
||||
let [selected] = select.attrs.as_slice() else {
|
||||
let [ast::Attr::Ident(selected)] = select.path.as_slice() else {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user