Skip to content

Commit

Permalink
refactor: Rename into_js_either_node helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Apr 9, 2024
1 parent 02dab51 commit 790662a
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 18 deletions.
12 changes: 9 additions & 3 deletions crates/codegen/parser/runtime/src/napi_interface/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ pub enum NodeType {
}

impl RustNode {
/// Converts a Rust node into a choice of NAPI-exposed wrapper structs.
pub fn into_either(self) -> Either<RuleNode, TokenNode> {
/// Converts the node into `napi` wrapper for `RuleNode | TokenNode` JS object.
pub fn into_js_either_node(self) -> Either<RuleNode, TokenNode> {
match self {
RustNode::Rule(rule) => Either::A(RuleNode(rule)),
RustNode::Token(token) => Either::B(TokenNode(token)),
}
}
}

impl From<RustNode> for Either<RuleNode, TokenNode> {
fn from(value: RustNode) -> Self {
value.into_js_either_node()
}
}

#[napi(namespace = "cst")]
pub struct RuleNode(pub(crate) Rc<RustRuleNode>);

Expand Down Expand Up @@ -63,7 +69,7 @@ impl RuleNode {
self.0
.children
.iter()
.map(|child| child.node.clone().into_either())
.map(|child| child.node.clone().into())
.collect()
}

Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/parser/runtime/src/napi_interface/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Cursor {

#[napi(ts_return_type = "cst.Node", catch_unwind)]
pub fn node(&self) -> Either<RuleNode, TokenNode> {
self.0.node().into_either()
self.0.node().into()
}

#[napi(getter, ts_return_type = "kinds.NodeLabel", catch_unwind)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl From<RustParseOutput> for ParseOutput {
impl ParseOutput {
#[napi(ts_return_type = "cst.Node", catch_unwind)]
pub fn tree(&self) -> Either<RuleNode, TokenNode> {
self.0.tree().into_either()
self.0.tree().into()
}

#[napi(ts_return_type = "Array<parse_error.ParseError>", catch_unwind)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Selector {
}
labeled if filter(labeled) => {
self.index += 1;
return Ok(Some(labeled.node.clone().into_either()));
return Ok(Some(labeled.node.clone().into_js_either_node()));
}
_ => {
break;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 790662a

Please sign in to comment.