refactor(xatu): order parser conversions by use
This commit is contained in:
@@ -21,6 +21,91 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
match node.kind() {
|
||||
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
||||
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let apply = rnix::ast::Apply::cast(node)?;
|
||||
let range = apply.syntax().text_range().into();
|
||||
let data = apply.try_into().ok()?;
|
||||
|
||||
Some(ast::Node {
|
||||
range,
|
||||
data: ast::NodeData::Apply(data),
|
||||
})
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Apply> for ast::Apply {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
function: apply.lambda().ok_or(())?.try_into()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Expr> for ast::Expr {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
|
||||
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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rnix::ast::Ident> for ast::Ident {
|
||||
fn from(ident: rnix::ast::Ident) -> Self {
|
||||
Self {
|
||||
name: ident.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
.attrpath()
|
||||
.ok_or(())?
|
||||
.attrs()
|
||||
.map(|attr| match attr {
|
||||
rnix::ast::Attr::Ident(ident) => Ok(ident.into()),
|
||||
_ => Err(()),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let inherit = rnix::ast::Inherit::cast(node)?;
|
||||
|
||||
Some(ast::Node {
|
||||
range: inherit.syntax().text_range().into(),
|
||||
data: ast::NodeData::Inherit(inherit.into()),
|
||||
})
|
||||
}
|
||||
|
||||
impl From<rnix::ast::Inherit> for ast::Inherit {
|
||||
fn from(inherit: rnix::ast::Inherit) -> Self {
|
||||
Self {
|
||||
attr_count: inherit.attrs().count(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_parse_error(error: &rnix::ParseError, source_len: usize) -> ast::ParseError {
|
||||
ast::ParseError {
|
||||
range: parse_error_range(error, source_len),
|
||||
@@ -50,93 +135,6 @@ fn parse_error_range(error: &rnix::ParseError, source_len: usize) -> ast::Source
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
match node.kind() {
|
||||
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
||||
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let apply = rnix::ast::Apply::cast(node)?;
|
||||
let range = apply.syntax().text_range().into();
|
||||
let data = apply.try_into().ok()?;
|
||||
|
||||
Some(ast::Node {
|
||||
range,
|
||||
data: ast::NodeData::Apply(data),
|
||||
})
|
||||
}
|
||||
|
||||
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let inherit = rnix::ast::Inherit::cast(node)?;
|
||||
|
||||
Some(ast::Node {
|
||||
range: inherit.syntax().text_range().into(),
|
||||
data: ast::NodeData::Inherit(inherit.into()),
|
||||
})
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Apply> for ast::Apply {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
function: apply.lambda().ok_or(())?.try_into()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Expr> for ast::Expr {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
|
||||
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<rnix::ast::Ident> for ast::Ident {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(ident: rnix::ast::Ident) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
name: ident.ident_token().ok_or(())?.text().to_owned(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
.attrpath()
|
||||
.ok_or(())?
|
||||
.attrs()
|
||||
.map(|attr| match attr {
|
||||
rnix::ast::Attr::Ident(ident) => ident.try_into(),
|
||||
_ => Err(()),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rnix::ast::Inherit> for ast::Inherit {
|
||||
fn from(inherit: rnix::ast::Inherit) -> Self {
|
||||
Self {
|
||||
attr_count: inherit.attrs().count(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rnix::TextRange> for ast::SourceRange {
|
||||
fn from(range: rnix::TextRange) -> Self {
|
||||
Self {
|
||||
|
||||
Reference in New Issue
Block a user