{"id":32171097,"url":"https://github.com/imnotavirus/std_result","last_synced_at":"2025-10-21T17:41:44.781Z","repository":{"id":222024828,"uuid":"755226374","full_name":"ImNotAVirus/std_result","owner":"ImNotAVirus","description":"Yet another way to standardize function returns # Rust inspiration","archived":false,"fork":false,"pushed_at":"2024-02-17T10:52:05.000Z","size":48,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-02T05:27:51.111Z","etag":null,"topics":["elixir","functions","standardization"],"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/ImNotAVirus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-09T17:30:27.000Z","updated_at":"2024-02-20T11:34:12.000Z","dependencies_parsed_at":"2024-02-13T20:23:15.117Z","dependency_job_id":null,"html_url":"https://github.com/ImNotAVirus/std_result","commit_stats":null,"previous_names":["imnotavirus/result_std","imnotavirus/std_result"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ImNotAVirus/std_result","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImNotAVirus%2Fstd_result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImNotAVirus%2Fstd_result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImNotAVirus%2Fstd_result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImNotAVirus%2Fstd_result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ImNotAVirus","download_url":"https://codeload.github.com/ImNotAVirus/std_result/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ImNotAVirus%2Fstd_result/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280303731,"owners_count":26307791,"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","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["elixir","functions","standardization"],"created_at":"2025-10-21T17:41:39.405Z","updated_at":"2025-10-21T17:41:44.773Z","avatar_url":"https://github.com/ImNotAVirus.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StdResult\n\n\u003c!-- MDOC !--\u003e\n\n[![Hex.pm version](https://img.shields.io/hexpm/v/std_result.svg?style=flat)](https://hex.pm/packages/std_result)\n[![Hex.pm license](https://img.shields.io/hexpm/l/std_result.svg?style=flat)](https://hex.pm/packages/std_result)\n[![Build Status](https://github.com/ImNotAVirus/std_result/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/ImNotAVirus/std_result/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/ImNotAVirus/std_result/badge.svg?branch=main)](https://coveralls.io/github/ImNotAVirus/std_result?branch=main)\n\n## Table of Contents\n\n* [Description](#description)\n* [Installation](#installation)\n* [The original issue](#the-original-issue)\n* [Usage](#usage)\n* [Contributing](#contributing)\n\n## Description\n\n`StdResult` is a library heavily inspired by Rust's `Result` type for handling \nfunction results in a consistent manner in Elixir.\n\nHandling function results in Elixir can sometimes be inconsistent, with some \nfunctions returning `:ok` or `:error`, while others return tuples like \n`{:ok, term}` or `{:error, reason}`.\n\n`StdResult` provides macros and functions to create and manipulate results,\npromoting a unified approach to error handling.\n\nDetailed documentation can be found at [https://hexdocs.pm/std_result](https://hexdocs.pm/std_result).\n\n## Installation\n\nAdd `std_result` to your list of dependencies in mix.exs:\n\n```elixir\ndef deps do\n  [\n    {:std_result, \"~\u003e 0.1\"}\n  ]\nend\n```\n\nAnd that's all.\n\n## The original issue\n\nLet's take a simple example.  \nLet's say we need to retrieve an environment variable, convert it to an integer \nand check that it's positive. Our function should return `{:ok, value}` or \n`{:error, reason}`.\n\nOne of the most popular solutions is to use `with` which would give something like: \n\n```elixir\nwith {:ok, port_str} \u003c- System.fetch_env(\"PORT\"),\n     port when port \u003e 0 \u003c- String.to_integer(port_str) do\n  {:ok, port}\nelse\n  # Return by System.fetch_env/1\n  :error -\u003e {:error, \"PORT env required\"}\n  # Returned by `port when port \u003e 0`\n  value when is_integer(value) -\u003e {:error, \"PORT must be a positive number, got: #{value}\"}\nend\n```\n\nI think you're beginning to understand what I'm getting at.  \nHere our error handling is very complicated to reread because the returns of the \nfunctions used in the `with` are not normalized.\n\nThis is a simple example, but the more conditions there are, the more confusing \nthe `else` block becomes.\n\nThe simplest solution would be to wrap each function in another, normalizing the \nreturns. But you'd soon find yourself with lots of functions that just normalize \neach other's returns.\n\n`StdResult` overcomes this problem.\n\n## Usage\n\nHere is the same problem as above, but with `StdResult` :\n\n```elixir\nimport StdResult\n\nSystem.fetch_env(\"PORT\")\n# This will transform `:error` into a `:error` tuple\n|\u003e normalize_result()\n# If there is an error, explicit the message\n|\u003e or_result(err(\"PORT env required\"))\n# If no error, parse the string as an integer\n# We could also have used `Integer.parse/1` but for simplicity's sake we won't.\n|\u003e map(\u0026String.to_integer/1)\n# Test if the number is positive\n|\u003e and_then(\u0026(if \u00261 \u003e= 0, do: ok(\u00261), else: err(\"PORT must be a positive number, got: #{\u00261}\")))\n\n# The result will be either:\n# - `{:ok, port}`\n# - `{:error, \"PORT env required\"}`\n# - `{:error, \"PORT must be a positive number, got: \u003cvalue\u003e\"}`\n```\n\n**NOTE**: This lib is not intended to replace `with`, but rather to complement it in certain cases.\n\n# Contributing\n\nContributions are welcome and appreciated. If you have any ideas, suggestions, or bugs to report,\nplease open an issue or a pull request on GitHub.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimnotavirus%2Fstd_result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimnotavirus%2Fstd_result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimnotavirus%2Fstd_result/lists"}