Amarula.Protocol.Binary.NodeUtils (amarula v0.1.0)

View Source

Utility functions for working with binary nodes.

Provides helper functions for inspecting, traversing, and manipulating binary node structures used in the WhatsApp protocol.

Summary

Functions

Convert a binary node to a readable string representation.

Get all child nodes from a node.

Extract attribute value from a node.

Get the first child node with the specified tag.

Get all child nodes with the specified tag.

Extract content from a child node.

Get the first child node's tag.

Check if a node has a specific attribute value.

Functions

binary_node_to_string(node)

@spec binary_node_to_string(Amarula.Protocol.Binary.Node.t()) :: String.t()

Convert a binary node to a readable string representation.

Useful for debugging and logging node structures.

Examples

iex> node = %Node{tag: "iq", attrs: %{"type" => "set", "id" => "123"}, content: [%Node{tag: "ping"}]}
iex> NodeUtils.binary_node_to_string(node)
"<iq type="set" id="123"><ping/></iq>"

get_all_binary_node_children(node)

@spec get_all_binary_node_children(Amarula.Protocol.Binary.Node.t()) :: [
  Amarula.Protocol.Binary.Node.t()
]

Get all child nodes from a node.

Returns a list of all child nodes, or empty list if no children.

Examples

iex> node = %Node{tag: "iq", content: [%Node{tag: "ref"}, %Node{tag: "pair-device"}]}
iex> NodeUtils.get_all_binary_node_children(node)
[%Node{tag: "ref"}, %Node{tag: "pair-device"}]

iex> NodeUtils.get_all_binary_node_children(%Node{tag: "empty"})
[]

get_attr(node, key)

@spec get_attr(Amarula.Protocol.Binary.Node.t(), String.t()) :: any()

Extract attribute value from a node.

Returns the value of the specified attribute, or nil if not found.

Examples

iex> node = %Node{tag: "iq", attrs: %{"type" => "set", "id" => "123"}}
iex> NodeUtils.get_attr(node, "type")
"set"

iex> NodeUtils.get_attr(node, "nonexistent")
nil

get_binary_node_child(node, tag)

@spec get_binary_node_child(Amarula.Protocol.Binary.Node.t(), String.t()) ::
  Amarula.Protocol.Binary.Node.t() | nil

Get the first child node with the specified tag.

Returns the first child node matching the tag, or nil if not found.

Examples

iex> node = %Node{tag: "iq", content: [%Node{tag: "pair-device"}, %Node{tag: "ref"}]}
iex> NodeUtils.get_binary_node_child(node, "pair-device")
%Node{tag: "pair-device"}

iex> NodeUtils.get_binary_node_child(node, "nonexistent")
nil

get_binary_node_children(node, tag)

@spec get_binary_node_children(Amarula.Protocol.Binary.Node.t(), String.t()) :: [
  Amarula.Protocol.Binary.Node.t()
]

Get all child nodes with the specified tag.

Returns a list of all child nodes matching the tag.

Examples

iex> node = %Node{tag: "iq", content: [%Node{tag: "ref"}, %Node{tag: "ref"}]}
iex> NodeUtils.get_binary_node_children(node, "ref")
[%Node{tag: "ref"}, %Node{tag: "ref"}]

iex> NodeUtils.get_binary_node_children(node, "nonexistent")
[]

get_child_content(node, tag)

@spec get_child_content(Amarula.Protocol.Binary.Node.t(), String.t()) :: any()

Extract content from a child node.

Returns the content of the first child node with the specified tag, or nil if not found.

Examples

iex> node = %Node{tag: "iq", content: [%Node{tag: "ref", content: "abc123"}]}
iex> NodeUtils.get_child_content(node, "ref")
"abc123"

iex> NodeUtils.get_child_content(node, "nonexistent")
nil

get_first_child_tag(node)

@spec get_first_child_tag(Amarula.Protocol.Binary.Node.t()) :: String.t()

Get the first child node's tag.

Returns the tag of the first child node, or empty string if no children.

Examples

iex> node = %Node{tag: "iq", content: [%Node{tag: "pair-device"}, %Node{tag: "ref"}]}
iex> NodeUtils.get_first_child_tag(node)
"pair-device"

iex> NodeUtils.get_first_child_tag(%Node{tag: "empty"})
""

has_attr_value?(node, key, value)

@spec has_attr_value?(Amarula.Protocol.Binary.Node.t(), String.t(), String.t()) ::
  boolean()

Check if a node has a specific attribute value.

Examples

iex> node = %Node{tag: "iq", attrs: %{"type" => "set", "id" => "123"}}
iex> NodeUtils.has_attr_value?(node, "type", "set")
true

iex> NodeUtils.has_attr_value?(node, "type", "get")
false