{"id":20492056,"url":"https://github.com/zotonic/dispatch_compiler","last_synced_at":"2025-04-13T17:01:33.857Z","repository":{"id":3964391,"uuid":"45245564","full_name":"zotonic/dispatch_compiler","owner":"zotonic","description":"Compiles dispatch rules to an Erlang module for quick dispatch matching.","archived":false,"fork":false,"pushed_at":"2022-05-31T06:50:18.000Z","size":39,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-27T07:51:33.494Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zotonic.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-10-30T10:50:02.000Z","updated_at":"2023-02-07T15:37:34.000Z","dependencies_parsed_at":"2022-09-10T01:11:38.714Z","dependency_job_id":null,"html_url":"https://github.com/zotonic/dispatch_compiler","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Fdispatch_compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Fdispatch_compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Fdispatch_compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zotonic%2Fdispatch_compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zotonic","download_url":"https://codeload.github.com/zotonic/dispatch_compiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248750077,"owners_count":21155685,"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-11-15T17:27:26.202Z","updated_at":"2025-04-13T17:01:33.819Z","avatar_url":"https://github.com/zotonic.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Test](https://github.com/zotonic/dispatch_compiler/workflows/Test/badge.svg)\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg?logo=apache\u0026logoColor=red)](https://www.apache.org/licenses/LICENSE-2.0)\n\n# dispatch_compiler\n\nCompiles dispatch rules to an Erlang module for quick matching.\n\nThe dispatch compiler takes a list of dispatch rules and creates\nan Erlang module that matches those rules.\n\nThe compiled Erlang module exports a single function: `match/2`.\n\n## Dispatch rules\n\nDispatch rules are lists of tokens with some extra information:\n\n    {name, [\"foo\", \"bar\", id], controller_name, [controller,options]}\n\nThe path parts can be one of the following:\n\n * a string `\"foo\"`. This is translated to a _binary_ and must match literally\n * an atom `id` this binds the variable to the token and is returned\n * the atom `'*'`, this binds to any left tokens, which could be an empty list\n * regular expressions `{id, \"^[0-9]+$\"}`\n * regular expressions with _re_ compile options `{id, \"^[a-z]+$\", [caseless]}`\n\nIt is also possible to define functions to perform runtime checks on the tokens.\n\nFunctions can be defined as:\n\n * A module, function pair: `{var, {foo, bar}}`, this will call `foo:bar(Token, Context)`\n * A module, function, args triple: `{var, {foo, bar, [some, args]}}`, this will call `foo:bar(Token, Context, some, args)`\n * A function with a single arg: `{var, fun(\u003c\u003cC,Rest/binary\u003e\u003e) -\u003e C \u003c $z end}`\n * A function with a two arguments: `{var, fun(\u003c\u003cC,Rest/binary\u003e\u003e, Context) -\u003e C \u003c $z end}`\n\nFunctions must return one of the following\n\n * `true` on a match\n * `false` if not matched\n * `{ok, Term}` to bind the variable to the return `Term`\n\n## Usage\n\nFirst compile the dispatch rules to an Erlang module:\n\n    Rules = [\n        {test, [\"a\", v], foo, []},\n        {wildcard, [\"w\", '*'], foo, []}\n    ],\n    ok = dispatch_compiler:compile_load('mydispatch', Rules).\n    \nNow the compiled module can be used to match (the _undefined_ will be passed as `Context` to any functions in the dispatch rules):\n\n    1\u003e mydispatch:match([\u003c\u003c\"a\"\u003e\u003e, \u003c\u003c\"b\"\u003e\u003e], undefined).\n    {ok, { {test, [\"a\", v], foo, []}, [{v,\u003c\u003c\"b\"\u003e\u003e}]}}\n\nThe return value contains the matched dispatch rule and any bound variables.\nThe first matching rule is returned.\n\nAnother example showing a matching wildcard:\n\n    2\u003e mydispatch:match([\u003c\u003c\"w\"\u003e\u003e, \u003c\u003c\"b\"\u003e\u003e, \u003c\u003c\"c\"\u003e\u003e], undefined).\n    {ok, { {wildcard, [\"w\", '*'], foo, []}, [{'*',[\u003c\u003c\"b\"\u003e\u003e, \u003c\u003c\"c\"\u003e\u003e]}]}}\n\nIf no dispatch rule could be matched, then `fail` is returned:\n\n    3\u003e mydispatch:match([\u003c\u003c\"a\"\u003e\u003e, \u003c\u003c\"b\"\u003e\u003e, \u003c\u003c\"c\"\u003e\u003e], undefined).\n    fail\n\n## Tests\n\nRun `make test` to run the tests.\n\n## Documentation generation\n\n### Edoc\n\n#### Generate public API\n`rebar3 edoc`\n\n#### Generate private API\n`rebar3 as edoc_private edoc`\n\n### ExDoc\n`rebar3 ex_doc --logo doc/img/logo.png --output edoc`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzotonic%2Fdispatch_compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzotonic%2Fdispatch_compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzotonic%2Fdispatch_compiler/lists"}