def handle_structs(
    node: Any,
    path: str | None,
    subtree_renderer: renderers.TreescopeSubtreeRenderer,
) -> (
    rendering_parts.RenderableTreePart
    | rendering_parts.RenderableAndLineAnnotations
    | type(NotImplemented)
):
  """Renders a penzai struct or layer.

  Differences in handling structs vs other dataclasses:

  * We use ``cls.from_attributes`` instead of
    ``penzai.util.dataclasses.dataclass_from_attributes(cls)`` for dataclasses
    with custom init logic.

  * Colors are retrieved from the method ``treescope_color`` instead of
    ``__treescope_color__``.

  * Dataclass fields that are pytree children are rendered in a different style.

  Args:
    node: The node to render.
    path: Optionally, a path to this node.
    subtree_renderer: A recursive renderer for subtrees of this node.

  Returns:
    A rendering, or NotImplemented.
  """
  if not isinstance(node, struct.Struct):
    return NotImplemented

  assert dataclasses.is_dataclass(node), "Every struct.Struct is a dataclass"
  constructor_open = render_struct_constructor(node)
  fields = dataclasses.fields(node)

  background_color, background_pattern = (
      formatting_util.parse_simple_color_and_pattern_spec(
          node.treescope_color(), type(node).__name__
      )
  )

  children = rendering_parts.build_field_children(
      node,
      path,
      subtree_renderer,
      fields_or_attribute_names=fields,
      attr_style_fn=struct_attr_style_fn_for_fields(fields),
  )

  return rendering_parts.build_foldable_tree_node_from_children(
      prefix=constructor_open,
      children=children,
      suffix=")",
      path=path,
      background_color=background_color,
      background_pattern=background_pattern,
  )
Renders a penzai struct or layer.

  Differences in handling structs vs other dataclasses:

  * We use ``cls.from_attributes`` instead of
    ``penzai.util.dataclasses.dataclass_from_attributes(cls)`` for dataclasses
    with custom init logic.

  * Colors are retrieved from the method ``treescope_color`` instead of
    ``__treescope_color__``.

  * Dataclass fields that are pytree children are rendered in a different style.

  Args:
    node: The node to render.
    path: Optionally, a path to this node.
    subtree_renderer: A recursive renderer for subtrees of this node.

  Returns:
    A rendering, or NotImplemented.
  
penzai/core/_treescope_handlers/struct_handler.py