View Source Formatter Rules
The Funx library now exports formatter rules for its Either DSL, allowing projects that depend on Funx to automatically format DSL code without extra parentheses.
Exported Rules
The following Either DSL functions are configured to format without parentheses:
either/2- DSL entry pointbind/1- Chain operations that return Either or result tuplesmap/1- Transform values with plain functionsap/1- Apply function in Either to value in Eithervalidate/1- Collect all errors from validatorsfilter_or_else/2- Filter with predicate, fallback if failsor_else/1- Provide fallback on errormap_left/1- Transform error valuestap- Run a side-effecting function inside the chain without changing the data
Note that flip/0 - Swap Left and Right still requires parentheses.
Usage in Dependent Projects
Step 1: Add to Dependencies
Make sure your mix.exs includes Funx as a dependency:
def deps do
[
{:funx, "~> 0.2"}
]
endStep 2: Update .formatter.exs
In your project's .formatter.exs, add :funx to import_deps:
[
import_deps: [:funx],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]Example
With this configuration, your DSL code will format cleanly:
either user_input do
bind ParseUser
map ValidateEmail
validate [CheckLength, CheckFormat]
bind SaveToDatabase
or_else default_user()
endInstead of:
either(user_input) do
bind(ParseUser)
map(ValidateEmail)
validate([CheckLength, CheckFormat])
bind(SaveToDatabase)
or_else(default_user())
endVerification
To verify the formatter rules are being imported correctly, you can run:
mix format --check-formatted
Your DSL code should format without adding parentheses.