line_segment.ex (975B)
1 defmodule Postgrex.Extensions.LineSegment do 2 @moduledoc false 3 import Postgrex.BinaryUtils, warn: false 4 use Postgrex.BinaryExtension, send: "lseg_send" 5 alias Postgrex.Extensions.Point 6 7 def encode(_) do 8 quote location: :keep, generated: true do 9 %Postgrex.LineSegment{point1: p1, point2: p2} -> 10 encoded_p1 = Point.encode_point(p1, Postgrex.LineSegment) 11 encoded_p2 = Point.encode_point(p2, Postgrex.LineSegment) 12 # 2 points -> 16 bytes each 13 [<<32::int32()>>, encoded_p1 | encoded_p2] 14 15 other -> 16 raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(other, Postgrex.Line) 17 end 18 end 19 20 def decode(_) do 21 quote location: :keep do 22 # 2 points -> 16 bytes each 23 <<32::int32(), x1::float64(), y1::float64(), x2::float64(), y2::float64()>> -> 24 p1 = %Postgrex.Point{x: x1, y: y1} 25 p2 = %Postgrex.Point{x: x2, y: y2} 26 %Postgrex.LineSegment{point1: p1, point2: p2} 27 end 28 end 29 end