├── test ├── test_helper.exs └── gen_fsm_helpers_test.exs ├── mix.lock ├── .gitignore ├── README.md ├── mix.exs ├── LICENSE ├── config └── config.exs └── lib └── gen_fsm_helpers.ex /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /test/gen_fsm_helpers_test.exs: -------------------------------------------------------------------------------- 1 | defmodule GenFSMHelpersTest do 2 | use ExUnit.Case 3 | doctest GenFSMHelpers 4 | 5 | end 6 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [], [], "hexpm"}, 2 | "ex_doc": {:hex, :ex_doc, "0.16.2", "3b3e210ebcd85a7c76b4e73f85c5640c011d2a0b2f06dcdf5acdb2ae904e5084", [], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}} 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GenFSMHelers 2 | 3 | Helper functions to be used with GenFSM. 4 | 5 | Adds `next_state` and `reply` functions so you don't have to enter the tuples. 6 | 7 | First argument is the state data so they fit will into a pipeline 8 | 9 | ## Installation 10 | 11 | If [available in Hex](https://hex.pm/docs/publish), the package can be installed 12 | by adding `gen_fsm_helpers` to your list of dependencies in `mix.exs`: 13 | 14 | ```elixir 15 | def deps do 16 | [ 17 | {:gen_fsm_helpers, "~> 0.1.0"} 18 | ] 19 | end 20 | ``` 21 | 22 | Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) 23 | and published on [HexDocs](https://hexdocs.pm). Once published, the docs can 24 | be found at [https://hexdocs.pm/gen_fsm_helpers](https://hexdocs.pm/gen_fsm_helpers). 25 | 26 | ## License 27 | 28 | ex_ami is Copyright (c) 2017 Stephen Pallen 29 | 30 | The source code is released under the MIT License. 31 | 32 | Check [LICENSE](LICENSE) for more information. 33 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule GenFSMHelers.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :gen_fsm_helpers, 7 | version: "0.1.0", 8 | elixir: "~> 1.4", 9 | start_permanent: Mix.env == :prod, 10 | package: package(), 11 | docs: [extras: ["README.md"], main: "GenFSMHelpers"], 12 | description: """ 13 | A helper library for GenFSM. 14 | """, 15 | deps: deps() 16 | ] 17 | end 18 | 19 | # Run "mix help compile.app" to learn about applications. 20 | def application do 21 | [ 22 | extra_applications: [:logger] 23 | ] 24 | end 25 | 26 | # Run "mix help deps" to learn about dependencies. 27 | defp deps do 28 | [ 29 | {:ex_doc, ">= 0.0.0", only: :dev} 30 | # {:dep_from_hexpm, "~> 0.3.0"}, 31 | # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}, 32 | ] 33 | end 34 | defp package do 35 | [ maintainers: ["Stephen Pallen"], 36 | licenses: ["MIT"], 37 | links: %{ "Github" => "https://github.com/smpallen99/gen_fsm_helpers"}, 38 | files: ~w(lib README.md mix.exs LICENSE)] 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Stephen Pallen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for 9 | # 3rd-party users, it should be done in your "mix.exs" file. 10 | 11 | # You can configure your application as: 12 | # 13 | # config :gen_fsm_helpers, key: :value 14 | # 15 | # and access this configuration in your application as: 16 | # 17 | # Application.get_env(:gen_fsm_helpers, :key) 18 | # 19 | # You can also configure a 3rd-party app: 20 | # 21 | # config :logger, level: :info 22 | # 23 | 24 | # It is also possible to import configuration files, relative to this 25 | # directory. For example, you can emulate configuration per environment 26 | # by uncommenting the line below and defining dev.exs, test.exs and such. 27 | # Configuration from the imported file will override the ones defined 28 | # here (which is why it is important to import them last). 29 | # 30 | # import_config "#{Mix.env}.exs" 31 | -------------------------------------------------------------------------------- /lib/gen_fsm_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule GenFSMHelpers do 2 | @moduledoc """ 3 | Helper library for GenFSM. 4 | 5 | Add helper functions to eliminate returning those nasty tuples. 6 | 7 | * next_state 8 | * reply 9 | """ 10 | 11 | @doc """ 12 | Return next_state tuple, given a tuple. 13 | 14 | ## Examples 15 | 16 | iex> GenFSMHelpers.next_state({:data, :idle}) 17 | {:next_state, :idle, :data} 18 | 19 | """ 20 | @spec next_state({any, atom}) :: {:next_state, atom, any} 21 | def next_state({state_data, state_name}) do 22 | next_state(state_data, state_name) 23 | end 24 | 25 | @doc """ 26 | Return next_state tuple. 27 | 28 | ## Examples 29 | 30 | iex> GenFSMHelpers.next_state(:data, :active) 31 | {:next_state, :active, :data} 32 | 33 | iex> GenFSMHelpers.next_state({:data, :active}, 5000) 34 | {:next_state, :active, :data, 5000} 35 | """ 36 | @spec next_state({any, atom}, integer) :: {:next_state, atom, any, integer} 37 | def next_state({state_data, state_name}, timeout) do 38 | next_state(state_data, state_name, timeout) 39 | end 40 | 41 | @spec next_state(any, atom) :: {:next_state, atom, any} 42 | def next_state(state_data, state_name) do 43 | {:next_state, state_name, state_data} 44 | end 45 | 46 | @doc """ 47 | Return next_state tuple with timeout. 48 | 49 | ## Examples 50 | 51 | iex> GenFSMHelpers.next_state(%{data: true}, :busy, 1000) 52 | {:next_state, :busy, %{data: true}, 1000} 53 | """ 54 | @spec next_state(any, atom, integer) :: {:next_state, atom, any, integer} 55 | def next_state(state_data, state_name, timeout) do 56 | {:next_state, state_name, state_data, timeout} 57 | end 58 | 59 | @doc """ 60 | Return reply tuple. 61 | 62 | ## Examples 63 | 64 | iex> GenFSMHelpers.reply(%{}, true, :pending) 65 | {:reply, true, :pending, %{}} 66 | """ 67 | @spec reply(any, any, atom) :: {:reply, any, atom, any} 68 | def reply(state_data, response, state_name) do 69 | {:reply, response, state_name, state_data} 70 | end 71 | 72 | @doc """ 73 | Return reply tuple with timeout. 74 | 75 | ## Examples 76 | 77 | iex> GenFSMHelpers.reply(%{}, true, :pending, 5000) 78 | {:reply, true, :pending, %{}, 5000} 79 | """ 80 | @spec reply(any, any, atom, integer) :: {:reply, any, atom, any, integer} 81 | def reply(state_data, response, state_name, timeout) do 82 | {:reply, response, state_name, state_data, timeout} 83 | end 84 | 85 | end 86 | --------------------------------------------------------------------------------