gcode/lib/model/comment/serialise.ex
James Harton ccf4635cca feat(model,serialise): Implement a basic G-Code model and serialiser.
It's not very thorough at the moment, but it should work for now.
2021-01-04 22:14:40 +13:00

16 lines
497 B
Elixir

defimpl Gcode.Model.Serialise, for: Gcode.Model.Comment do
alias Gcode.Model.Comment
use Gcode.Option
use Gcode.Result
@spec serialise(Comment.t()) :: Result.t([String.t()], {:serialise_error, any})
def serialise(%Comment{comment: some(comment)}) do
comment
|> String.split(~r/(\r\n|\r|\n)/)
|> Enum.reject(&(byte_size(&1) == 0))
|> Enum.map(&"(#{&1})")
|> ok()
end
def serialise(%Comment{comment: none()}), do: {:error, {:serialise_error, :empty_comment}}
end