{"id":17130704,"url":"https://github.com/jechol/rail","last_synced_at":"2025-04-13T06:32:13.248Z","repository":{"id":65225497,"uuid":"567150991","full_name":"jechol/rail","owner":"jechol","description":"Railway oriented programming toolkit for Elixir","archived":false,"fork":false,"pushed_at":"2023-12-15T20:28:37.000Z","size":87,"stargazers_count":12,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-10T06:09:14.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jechol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-17T07:13:40.000Z","updated_at":"2024-03-21T16:55:29.000Z","dependencies_parsed_at":"2023-01-15T16:45:37.258Z","dependency_job_id":null,"html_url":"https://github.com/jechol/rail","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Frail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Frail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Frail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jechol%2Frail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jechol","download_url":"https://codeload.github.com/jechol/rail/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674674,"owners_count":21143760,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T19:13:00.516Z","updated_at":"2025-04-13T06:32:12.918Z","avatar_url":"https://github.com/jechol.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rail\n\n[![test](https://github.com/jechol/rail/actions/workflows/test.yml/badge.svg)](https://github.com/jechol/rail/actions/workflows/test.yml)\n[![Coverage Status](https://coveralls.io/repos/github/jechol/rail/badge.svg?branch=main)](https://coveralls.io/github/jechol/rail?branch=main)\n[![Hex.pm](https://img.shields.io/hexpm/v/rail)](https://hex.pm/packages/rail)\n[![GitHub](https://img.shields.io/github/license/jechol/rail)](https://github.com/jechol/rail/blob/main/LICENSE)\n\n`Rail` is a helper macros for \"Railway oriented programming\".\n\nIt helps you handle error cases at almost no cost with `rail`, `\u003e\u003e\u003e`, and `def` macro.\n\nIf you are not comfortable with \"Railway oriented programming\", see [Railway oriented programming](https://www.youtube.com/watch?v=fYo3LN9Vf_M)\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:rail, \"~\u003e 1.1.0\"}\n  ]\nend\n```\n\n## Usage\n\n### Basic usage\n\n`use Rail` introduces new syntax `left \u003c- right`,\n\n- which bind `value` to left when right is `{:ok, value}` or `value`\n- or skips entire code block when right is `{:error, _}` or `:error`.\n\n```elixir\n\ndefmodule Target do\n  use Rail\n\n  def div(num, denom) do\n    denom \u003c- check_denom(denom)\n    num / denom\n  end\n\n  def check_denom(0) do\n    {:error, :div_by_zero}\n  end\n\n  def check_denom(n) do\n    # same with {:ok, n}\n    n\n  end\nend\n\niex\u003e Calc.div(10, 2)\n5.0\niex\u003e Calc.div(10, 0)\n{:error, :div_by_zero}\n```\n\n`rail/1` is available inside other code blocks.\n\n```elixir\n\niex\u003e rail do\n...    x \u003c- {:ok, 1}\n...    y \u003c- {:ok, 2}\n...\n...    x + y\n...  end\n3\n```\n\n`left \u003e\u003e\u003e right` is similar to `|\u003e`, but\n\n- applies right function only when left is `value` or `{:ok, value}`.\n- support both function and call expression, which makes it compatible with `|\u003e`.\n\n```elixir\niex\u003e 1 \u003e\u003e\u003e fn v -\u003e Integer.to_string(v) end\n\"1\"\niex\u003e {:ok, 1} \u003e\u003e\u003e fn v -\u003e Integer.to_string(v) end\n\"1\"\niex\u003e {:ok, 1} \u003e\u003e\u003e Integer.to_string()\n\"1\"\niex\u003e {:error, :div_by_zero} \u003e\u003e\u003e Integer.to_string()\n{:error, :div_by_zero}\n```\n\nFor more examples, see `Rail`\n\n## LICENSE\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjechol%2Frail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjechol%2Frail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjechol%2Frail/lists"}