def apply_overlays_plyara(
        self, overlays: set[str], parsed_rule: dict | None = None
    ):
        """Apply an overlay to a Yara rule.

        Overlays are used to modify the metadata of a Yara rule. The Yara rule's
        context attribute will be traversed, looking for `source` matching the
        overlay string. If found, the context key-value pairs will be used to
        update the Yara rule's metadata section.

        Args:
            overlay: The overlays to apply.
            parsed_rule: The parsed rule to apply the overlays to. If not provided
                the rule will be parsed from the pattern.
        """
        if not parsed_rule:
            parsed_rule = plyara.Plyara().parse_string(self.pattern)[0]

        metadata_overlay: dict[str, str | int] = {}
        parsed_rule_meta = parsed_rule.get("metadata", [])

        for context in self.context:
            if context["source"] in overlays:
                if metadata_overlay:
                    raise ValueError(f"Multiple overlays detected: {context['source']}")
                del context["source"]
                metadata_overlay.update(context)

        remaining = set(metadata_overlay.keys())
        for item in parsed_rule_meta:
            for key in metadata_overlay:
                if key in item:
                    item[key] = metadata_overlay[key]
                    remaining.remove(key)

        for key in sorted(remaining):
            parsed_rule_meta.append({key: metadata_overlay[key]})
        self.pattern = plyara.utils.rebuild_yara_rule(parsed_rule)

        return self
Apply an overlay to a Yara rule.

        Overlays are used to modify the metadata of a Yara rule. The Yara rule's
        context attribute will be traversed, looking for `source` matching the
        overlay string. If found, the context key-value pairs will be used to
        update the Yara rule's metadata section.

        Args:
            overlay: The overlays to apply.
            parsed_rule: The parsed rule to apply the overlays to. If not provided
                the rule will be parsed from the pattern.
        
core/schemas/indicators/yara.py