<reponame>xavierhardy/yamlfix
"""
new-line-at-end-of-file
"""

from typing import Any

from yamllint.rules.new_line_at_end_of_file import ID  # noqa: F401

from yamlfix.rules.types import FormattingResult, FormattingRule


def apply_before_load(text: str, rule: FormattingRule, rules: dict) -> FormattingResult:
    return FormattingResult(text=text, dumping_config={}, loading_config={})


def apply_before_dump(data: Any, rule: FormattingRule, text: str, rules: dict) -> Any:
    return data


def apply_on_result(
    result: str, original: str, rule: FormattingRule, rules: dict
) -> str:
    line_break = "\r\n" if "\r\n" in result else "\n"
    has_new_line_at_end_of_file = original.endswith(line_break)

    if not rule and not has_new_line_at_end_of_file and result.endswith(line_break):
        return result[: -len(line_break)]
    if (rule or has_new_line_at_end_of_file) and not result.endswith(line_break):
        return result + line_break
    return result

"""
new-line-at-end-of-file
"""

from typing import Any

from yamllint.rules.new_line_at_end_of_file import ID  # noqa: F401

from yamlfix.rules.types import FormattingResult, FormattingRule


def apply_before_load(text: str, rule: FormattingRule, rules: dict) -> FormattingResult:
    return FormattingResult(text=text, dumping_config={}, loading_config={})


def apply_before_dump(data: Any, rule: FormattingRule, text: str, rules: dict) -> Any:
    return data


def apply_on_result(
    result: str, original: str, rule: FormattingRule, rules: dict
) -> str:
    line_break = "\r\n" if "\r\n" in result else "\n"
    has_new_line_at_end_of_file = original.endswith(line_break)

    if not rule and not has_new_line_at_end_of_file and result.endswith(line_break):
        return result[: -len(line_break)]
    if (rule or has_new_line_at_end_of_file) and not result.endswith(line_break):
        return result + line_break
    return result
new-line-at-end-of-file # noqa: F401