def handle_layer(
    node: layer.Layer,
    path: str | None,
    subtree_renderer: renderers.TreescopeSubtreeRenderer,
) -> (
    rendering_parts.RenderableTreePart
    | rendering_parts.RenderableAndLineAnnotations
):
  """Renders a penzai layer.

  Layers render like Structs in general, except that they render with input and
  output structure annotations.

  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 of the layer.
  """

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

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

  background_color, background_pattern = (
      formatting_util.parse_simple_color_and_pattern_spec(
          node.treescope_color(), type(node).__name__  # pytype: disable=attribute-error  # pylint: disable=line-too-long
      )
  )

  # pylint: disable=unidiomatic-typecheck
  if (
      isinstance(node, grouping.Sequential)
      and type(node) is not grouping.Sequential
  ):
    first_line_annotation = rendering_parts.comment_color(
        rendering_parts.text(" # Sequential")
    )
  elif (
      isinstance(node, grouping.CheckedSequential)
      and type(node) is not grouping.CheckedSequential
  ):
    first_line_annotation = rendering_parts.comment_color(
        rendering_parts.text(" # CheckedSequential")
    )
  else:
    first_line_annotation = None
  # pylint: enable=unidiomatic-typecheck

  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,
      first_line_annotation=first_line_annotation,
  )
Renders a penzai layer.

  Layers render like Structs in general, except that they render with input and
  output structure annotations.

  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 of the layer.
  
penzai/nn/_treescope_handlers/layer_handler.py