Packages
kreuzberg
4.7.2
4.10.2
4.10.1
4.10.0
4.9.9
4.9.7
4.9.5
4.9.4
4.9.3
4.9.2
4.9.1
4.8.6
4.8.5
4.8.4
4.8.3
4.8.2
4.8.1
4.8.0
4.7.4
4.7.3
4.7.2
4.7.1
4.7.0
4.6.3
4.6.2
4.6.1
4.6.0
4.5.4
4.5.3
4.5.2
4.5.1
4.4.6
4.4.5
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.8
4.3.7
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.0
4.2.15
4.2.14
4.2.13
4.2.12
4.2.11
4.2.10
4.2.9
4.2.8
4.2.7
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.2
4.1.1
4.1.0
4.0.8
4.0.7
4.0.6
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
4.0.0-rc.27
4.0.0-rc.26
High-performance document intelligence library with OCR support
Current section
Files
Jump to
Current section
Files
lib/kreuzberg/code_span.ex
defmodule Kreuzberg.CodeSpan do
@moduledoc """
Byte and line/column span for a code element.
## Fields
* `:start_byte` - Starting byte offset
* `:end_byte` - Ending byte offset
* `:start_line` - Starting line number
* `:start_column` - Starting column number
* `:end_line` - Ending line number
* `:end_column` - Ending column number
"""
@type t :: %__MODULE__{
start_byte: non_neg_integer(),
end_byte: non_neg_integer(),
start_line: non_neg_integer(),
start_column: non_neg_integer(),
end_line: non_neg_integer(),
end_column: non_neg_integer()
}
defstruct start_byte: 0,
end_byte: 0,
start_line: 0,
start_column: 0,
end_line: 0,
end_column: 0
@doc """
Creates a CodeSpan struct from a map.
## Examples
iex> Kreuzberg.CodeSpan.from_map(%{"start_byte" => 0, "end_byte" => 42, "start_line" => 1, "start_column" => 0, "end_line" => 3, "end_column" => 5})
%Kreuzberg.CodeSpan{start_byte: 0, end_byte: 42, start_line: 1, start_column: 0, end_line: 3, end_column: 5}
"""
@spec from_map(map()) :: t()
def from_map(data) when is_map(data) do
%__MODULE__{
start_byte: Map.get(data, "start_byte", Map.get(data, :start_byte, 0)),
end_byte: Map.get(data, "end_byte", Map.get(data, :end_byte, 0)),
start_line: Map.get(data, "start_line", Map.get(data, :start_line, 0)),
start_column: Map.get(data, "start_column", Map.get(data, :start_column, 0)),
end_line: Map.get(data, "end_line", Map.get(data, :end_line, 0)),
end_column: Map.get(data, "end_column", Map.get(data, :end_column, 0))
}
end
@doc """
Converts a CodeSpan struct to a map.
"""
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = span) do
%{
"start_byte" => span.start_byte,
"end_byte" => span.end_byte,
"start_line" => span.start_line,
"start_column" => span.start_column,
"end_line" => span.end_line,
"end_column" => span.end_column
}
end
end