diff --git a/panacea/lib/panacea/pml/analysis.ex b/panacea/lib/panacea/pml/analysis.ex index 356ec721..c67d9006 100644 --- a/panacea/lib/panacea/pml/analysis.ex +++ b/panacea/lib/panacea/pml/analysis.ex @@ -4,11 +4,7 @@ defmodule Panacea.Pml.Analysis do defstruct [:ast, :clashes, :drugs, :unnamed] def run(ast) do - encoded_ast = - ast - |> :erlang.term_to_binary() - |> Base.encode64() - + encoded_ast = Panacea.Pml.Ast.encode(ast) clashes = Clashes.run(ast) drugs = Drugs.run(ast) unnamed = Unnamed.run(ast) diff --git a/panacea/lib/panacea/pml/ast.ex b/panacea/lib/panacea/pml/ast.ex index 2e93e032..aaac1a48 100644 --- a/panacea/lib/panacea/pml/ast.ex +++ b/panacea/lib/panacea/pml/ast.ex @@ -9,6 +9,12 @@ defmodule Panacea.Pml.Ast do do_unquote(ast, 0) end + def encode(ast) do + ast + |> :erlang.term_to_binary() + |> Base.encode64() + end + def decode(ast) do ast |> decode64() diff --git a/panacea/test/controllers/asclepius_controller_test.exs b/panacea/test/controllers/asclepius_controller_test.exs index bc68ac67..93cf2f56 100644 --- a/panacea/test/controllers/asclepius_controller_test.exs +++ b/panacea/test/controllers/asclepius_controller_test.exs @@ -13,9 +13,7 @@ defmodule Panacea.AsclepiusControllerTest do } """ {:ok, ast} = Panacea.Pml.Parser.parse(pml) - ast - |> :erlang.term_to_binary() - |> Base.encode64() + Panacea.Pml.Ast.encode(ast) end describe "AsclepiusController.uris_for_labels/2" do diff --git a/panacea/test/controllers/ast_controller_test.exs b/panacea/test/controllers/ast_controller_test.exs index 064f9dbf..2e4dd239 100644 --- a/panacea/test/controllers/ast_controller_test.exs +++ b/panacea/test/controllers/ast_controller_test.exs @@ -41,11 +41,7 @@ defmodule Panacea.AstControllerTest do |> String.replace_trailing("\n", "") {:ok, ast} = Panacea.Pml.Parser.parse(pml) - - encoded_ast = - ast - |> :erlang.term_to_binary() - |> Base.encode64() + encoded_ast = Panacea.Pml.Ast.encode(ast) conn = get conn, ast_path(conn, :to_pml), %{ast: encoded_ast} diff --git a/panacea/test/controllers/pml_controller_test.exs b/panacea/test/controllers/pml_controller_test.exs index 888b0230..db975951 100644 --- a/panacea/test/controllers/pml_controller_test.exs +++ b/panacea/test/controllers/pml_controller_test.exs @@ -95,12 +95,11 @@ defmodule Panacea.PmlControllerTest do |> File.read!() |> Panacea.Pml.Parser.parse() - received_ast = + {:ok, received_ast} = conn |> response_body() |> Map.get("ast") - |> Base.decode64!() - |> :erlang.binary_to_term() + |> Panacea.Pml.Ast.decode() assert received_ast == ast end diff --git a/panacea/web/controllers/ast_controller.ex b/panacea/web/controllers/ast_controller.ex index 30723554..96ec3785 100644 --- a/panacea/web/controllers/ast_controller.ex +++ b/panacea/web/controllers/ast_controller.ex @@ -1,7 +1,8 @@ defmodule Panacea.AstController do use Panacea.Web, :controller - def to_pml(conn, %{"ast" => encoded_ast}) do encoded_ast + def to_pml(conn, %{"ast" => encoded_ast}) do + encoded_ast |> Panacea.Pml.Ast.decode() |> to_pml() |> respond(conn)