Packages
metastatic
0.7.0
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.2
0.22.1
0.22.0
0.21.3
0.21.2
0.21.1
0.21.0
0.20.3
0.20.2
0.20.1
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Cross-language code meta-model library using unified MetaAST representation. Parse, transform, and translate code across Python, Elixir, Ruby, Erlang, Haskell, and more via a shared three-tuple AST format.
Current section
Files
Jump to
Current section
Files
lib/metastatic/analysis/purity/formatter.ex
defmodule Metastatic.Analysis.Purity.Formatter do
@moduledoc """
Formats purity analysis results for display.
Supports multiple output formats: text, JSON, and detailed.
"""
alias Metastatic.Analysis.Purity.Result
@doc """
Formats a purity result as text (default format).
## Examples
iex> result = Metastatic.Analysis.Purity.Result.pure()
iex> Metastatic.Analysis.Purity.Formatter.format(result, :text)
"PURE"
iex> result = Metastatic.Analysis.Purity.Result.impure([:io], [])
iex> Metastatic.Analysis.Purity.Formatter.format(result, :text)
"IMPURE: I/O operations"
"""
@spec format(Result.t(), atom()) :: String.t()
def format(%Result{pure?: true}, :text), do: "PURE"
def format(%Result{pure?: false, summary: summary}, :text) do
"IMPURE: #{summary |> String.replace("Function is impure due to ", "")}"
end
def format(result, :json) do
Jason.encode!(%{
pure: result.pure?,
effects: result.effects,
confidence: result.confidence,
summary: result.summary,
unknown_calls: result.unknown_calls,
impure_locations: format_locations(result.impure_locations)
})
end
def format(result, :detailed) do
lines = [
"Purity Analysis Result",
"=" <> String.duplicate("=", 40),
"",
"Status: #{if result.pure?, do: "PURE", else: "IMPURE"}",
"Confidence: #{result.confidence}",
""
]
lines =
if result.pure? do
lines ++ ["No side effects detected."]
else
effects_section =
if Enum.any?(result.effects) do
[
"Effects Detected:",
format_effects_list(result.effects),
""
]
else
[]
end
unknown_section =
if Enum.any?(result.unknown_calls) do
[
"Unknown Function Calls:",
format_unknown_list(result.unknown_calls),
""
]
else
[]
end
lines ++ effects_section ++ unknown_section ++ ["Summary: #{result.summary}"]
end
Enum.join(lines, "\n")
end
# Private helpers
defp format_locations(locations) do
Enum.map(locations, fn {:line, line, effect} ->
%{line: line, effect: effect}
end)
end
defp format_effects_list(effects) do
Enum.map_join(effects, "\n", fn effect ->
" - #{effect_name(effect)}"
end)
end
defp format_unknown_list(calls) do
Enum.map_join(calls, "\n", fn call ->
" - #{call}"
end)
end
defp effect_name(:io), do: "I/O operations"
defp effect_name(:mutation), do: "Mutations"
defp effect_name(:random), do: "Random operations"
defp effect_name(:time), do: "Time operations"
defp effect_name(:network), do: "Network operations"
defp effect_name(:database), do: "Database operations"
defp effect_name(:exception), do: "Exception handling"
defp effect_name(:unknown), do: "Unknown operations"
end