|
| 1 | +defmodule Diff.Hex.ChunkExtractor do |
| 2 | + @enforce_keys [:file_path, :from_line, :lines_to_read, :direction] |
| 3 | + defstruct Enum.map(@enforce_keys, &{&1, nil}) ++ |
| 4 | + [ |
| 5 | + errors: [], |
| 6 | + raw: nil, |
| 7 | + parsed: nil |
| 8 | + ] |
| 9 | + |
| 10 | + def run(params) do |
| 11 | + __MODULE__ |
| 12 | + |> struct!(params) |
| 13 | + |> parse_integers([:from_line, :lines_to_read]) |
| 14 | + |> validate_direction() |
| 15 | + |> system_read_raw_chunk() |
| 16 | + |> parse_chunk() |
| 17 | + |> remove_trailing_newline() |
| 18 | + end |
| 19 | + |
| 20 | + defp parse_integers(chunk, fields) do |
| 21 | + Enum.reduce(fields, chunk, &parse_integer/2) |
| 22 | + end |
| 23 | + |
| 24 | + defp parse_integer(field, chunk) do |
| 25 | + value = chunk |> Map.get(field) |> parse_value() |
| 26 | + |
| 27 | + case value do |
| 28 | + :error -> %{chunk | errors: {:parse_integer, "#{field} must be a number"}} |
| 29 | + integer -> Map.put(chunk, field, integer) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + defp parse_value(number) when is_integer(number), do: number |
| 34 | + |
| 35 | + defp parse_value(number) when is_binary(number) do |
| 36 | + with {int, _} <- Integer.parse(number), do: int |
| 37 | + end |
| 38 | + |
| 39 | + defp validate_direction(%{direction: direction} = chunk) when direction in ["up", "down"] do |
| 40 | + chunk |
| 41 | + end |
| 42 | + |
| 43 | + defp validate_direction(chunk) do |
| 44 | + error = {:direction, "direction must be either \"up\" or \"down\""} |
| 45 | + %{chunk | errors: [error | chunk.errors]} |
| 46 | + end |
| 47 | + |
| 48 | + defp system_read_raw_chunk(%{errors: [_ | _]} = chunk), do: chunk |
| 49 | + |
| 50 | + defp system_read_raw_chunk(chunk) do |
| 51 | + chunk_sh = Application.app_dir(:diff, ["priv", "chunk.sh"]) |
| 52 | + |
| 53 | + path = chunk.file_path |
| 54 | + from_line = to_string(chunk.from_line) |
| 55 | + lines_to_read = to_string(chunk.lines_to_read) |
| 56 | + direction = chunk.direction |
| 57 | + |
| 58 | + case System.cmd(chunk_sh, [path, from_line, lines_to_read, direction], stderr_to_stdout: true) do |
| 59 | + {raw_chunk, 0} -> |
| 60 | + %{chunk | raw: raw_chunk} |
| 61 | + |
| 62 | + {error, code} -> |
| 63 | + error = {:system, "System command exited with a non-zero status #{code}: #{error}"} |
| 64 | + %{chunk | errors: [error | chunk.errors]} |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + defp parse_chunk(%{errors: [_ | _]} = chunk), do: chunk |
| 69 | + |
| 70 | + defp parse_chunk(chunk) do |
| 71 | + parsed = |
| 72 | + chunk.raw |
| 73 | + |> String.split("\n") |
| 74 | + |> Enum.map(fn line -> %{line_text: line} end) |
| 75 | + |
| 76 | + set_line_numbers(%{chunk | parsed: parsed}) |
| 77 | + end |
| 78 | + |
| 79 | + defp set_line_numbers(%{direction: "down"} = chunk) do |
| 80 | + %{chunk | parsed: parsed_with_line_numbers(chunk.parsed, chunk.from_line)} |
| 81 | + end |
| 82 | + |
| 83 | + defp set_line_numbers(%{direction: "up"} = chunk) do |
| 84 | + offset = chunk.from_line - length(chunk.parsed) + 1 |
| 85 | + |
| 86 | + %{chunk | parsed: parsed_with_line_numbers(chunk.parsed, offset)} |
| 87 | + end |
| 88 | + |
| 89 | + defp parsed_with_line_numbers(parsed_chunk, starting_number) when is_binary(starting_number) do |
| 90 | + parsed_with_line_numbers(parsed_chunk, starting_number) |
| 91 | + end |
| 92 | + |
| 93 | + defp parsed_with_line_numbers(parsed_chunk, starting_number) do |
| 94 | + parsed_chunk |
| 95 | + |> Enum.with_index(starting_number) |
| 96 | + |> Enum.map(fn {line, line_number} -> Map.put_new(line, :line_number, line_number) end) |
| 97 | + end |
| 98 | + |
| 99 | + defp remove_trailing_newline(%{errors: [_ | _]} = chunk), do: {:error, chunk} |
| 100 | + |
| 101 | + defp remove_trailing_newline(chunk) do |
| 102 | + [trailing_line | reversed_tail] = Enum.reverse(chunk.parsed) |
| 103 | + |
| 104 | + chunk = |
| 105 | + case trailing_line do |
| 106 | + %{line_text: ""} -> %{chunk | parsed: Enum.reverse(reversed_tail)} |
| 107 | + %{line_text: _text} -> chunk |
| 108 | + end |
| 109 | + |
| 110 | + {:ok, chunk} |
| 111 | + end |
| 112 | +end |
0 commit comments