Metastatic.Languages (Metastatic v0.17.0)

View Source

Single source of truth for supported languages and their adapters.

All language detection, validation, and adapter lookup throughout the codebase should go through this module. Adding support for a new language requires only adding a single entry to @adapters here (assuming the adapter module already exists and implements Metastatic.Adapter).

Compile-Time Derivation

Extension maps, supported language lists, and display strings are all derived at compile time from the @adapters map and each adapter's file_extensions/0 callback, so they stay in sync automatically.

Examples

iex> Metastatic.Languages.supported?(:python)
true

iex> Metastatic.Languages.supported?(:brainfuck)
false

iex> {:ok, :ruby} = Metastatic.Languages.detect_language("app.rb")

iex> {:ok, Metastatic.Adapters.Python} = Metastatic.Languages.get_adapter(:python)

Summary

Functions

Returns the map of all adapters (language atom => module).

Detects language from a file path based on its extension.

Returns the primary file extension for a language.

Returns the adapter module for a supported language.

Parses a language string (e.g. from CLI --language option) into an atom.

Checks whether a language atom is supported.

Returns all supported file extensions.

Returns the list of supported language atoms, sorted alphabetically.

Returns a comma-separated string of supported language names.

Functions

adapters()

@spec adapters() :: %{required(atom()) => module()}

Returns the map of all adapters (language atom => module).

Examples

iex> adapters = Metastatic.Languages.adapters()
iex> Map.has_key?(adapters, :python)
true

detect_language(path)

@spec detect_language(String.t()) :: {:ok, atom()} | {:error, String.t()}

Detects language from a file path based on its extension.

Examples

iex> Metastatic.Languages.detect_language("script.py")
{:ok, :python}

iex> Metastatic.Languages.detect_language("app.rb")
{:ok, :ruby}

iex> Metastatic.Languages.detect_language("unknown.xyz")
{:error, "Cannot detect language from extension: .xyz"}

extension_for_language(language)

@spec extension_for_language(atom()) :: {:ok, String.t()} | {:error, String.t()}

Returns the primary file extension for a language.

Examples

iex> Metastatic.Languages.extension_for_language(:python)
{:ok, ".py"}

iex> Metastatic.Languages.extension_for_language(:unknown)
{:error, "No extension known for language: unknown"}

get_adapter(language)

@spec get_adapter(atom()) :: {:ok, module()} | {:error, String.t()}

Returns the adapter module for a supported language.

Examples

iex> Metastatic.Languages.get_adapter(:python)
{:ok, Metastatic.Adapters.Python}

iex> Metastatic.Languages.get_adapter(:unknown)
{:error, "Unsupported language: unknown. Supported: elixir, erlang, haskell, python, ruby"}

parse_language(lang_str)

@spec parse_language(String.t() | nil) :: {:ok, atom()} | {:error, String.t()} | nil

Parses a language string (e.g. from CLI --language option) into an atom.

Returns nil when given nil (language not specified, auto-detect).

Examples

iex> Metastatic.Languages.parse_language("python")
{:ok, :python}

iex> Metastatic.Languages.parse_language("Ruby")
{:ok, :ruby}

iex> Metastatic.Languages.parse_language(nil)
nil

iex> Metastatic.Languages.parse_language("brainfuck")
{:error, "Invalid language: brainfuck. Supported: elixir, erlang, haskell, python, ruby"}

supported?(language)

@spec supported?(atom()) :: boolean()

Checks whether a language atom is supported.

Examples

iex> Metastatic.Languages.supported?(:python)
true

iex> Metastatic.Languages.supported?(:brainfuck)
false

supported_extensions()

@spec supported_extensions() :: [String.t()]

Returns all supported file extensions.

Examples

iex> ".py" in Metastatic.Languages.supported_extensions()
true

supported_languages()

@spec supported_languages() :: [atom()]

Returns the list of supported language atoms, sorted alphabetically.

Examples

iex> Metastatic.Languages.supported_languages()
[:elixir, :erlang, :haskell, :python, :ruby]

supported_languages_string()

@spec supported_languages_string() :: String.t()

Returns a comma-separated string of supported language names.

Useful for error messages and help text.

Examples

iex> Metastatic.Languages.supported_languages_string()
"elixir, erlang, haskell, python, ruby"